blob: def2e30edc05c08c4e4fa6bdd3154beb992cb198 [file] [log] [blame]
François Gaffie2110e042015-03-24 08:41:51 +01001/*
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 Hsieh2b487032018-09-13 14:16:02 -070028#include <android-base/macros.h>
François Gaffie2110e042015-03-24 08:41:51 +010029#include <AudioPolicyManagerObserver.h>
jiabine1284852019-09-11 10:15:46 -070030#include <PolicyAudioPort.h>
François Gaffie2110e042015-03-24 08:41:51 +010031#include <IOProfile.h>
François Gaffiec005e562018-11-06 15:04:49 +010032#include <AudioIODescriptorInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010033#include <policy.h>
jiabin9a3361e2019-10-01 09:38:30 -070034#include <media/AudioContainers.h>
François Gaffie2110e042015-03-24 08:41:51 +010035#include <utils/String8.h>
36#include <utils/Log.h>
37
38namespace android
39{
40namespace audio_policy
41{
42
François Gaffiedc7553f2018-11-02 10:39:57 +010043struct legacy_strategy_map { const char *name; legacy_strategy id; };
Mikhail Naganovf9003622020-03-10 13:15:08 -070044static 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 Gaffiedc7553f2018-11-02 10:39:57 +010061
François Gaffie2110e042015-03-24 08:41:51 +010062Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010063{
François Gaffiedc7553f2018-11-02 10:39:57 +010064 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 Naganovf9003622020-03-10 13:15:08 -070069 auto legacyStrategy = getLegacyStrategy();
70 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010071 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010072 }
73}
74
François Gaffie2110e042015-03-24 08:41:51 +010075status_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 Gaffie2110e042015-03-24 08:41:51 +010084 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 Gaffie2110e042015-03-24 08:41:51 +010094 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 Gaffie2110e042015-03-24 08:41:51 +0100101 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 Gaffie2110e042015-03-24 08:41:51 +0100110 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 Gaffie2110e042015-03-24 08:41:51 +0100116 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 Burk09bc4612016-02-24 15:58:15 -0800120 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
121 }
Phil Burk09bc4612016-02-24 15:58:15 -0800122 break;
123 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
124 if (config != AUDIO_POLICY_FORCE_NONE &&
125 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
127 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800128 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
129 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100130 }
François Gaffie2110e042015-03-24 08:41:51 +0100131 break;
Jack He96117ae2018-02-12 20:52:53 -0800132 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 He96117ae2018-02-12 20:52:53 -0800137 break;
François Gaffie2110e042015-03-24 08:41:51 +0100138 default:
139 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800140 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100141 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100142 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100143}
144
jiabinf502d152019-08-07 14:43:33 -0700145DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
146 DeviceVector availableOutputDevices,
147 DeviceVector availableInputDevices,
148 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800149{
jiabinf502d152019-08-07 14:43:33 -0700150 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100151
152 switch (strategy) {
153
154 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin9a3361e2019-10-01 09:38:30 -0700155 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100156 break;
157
158 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700159 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700160 devices = getDevicesForStrategyInt(
161 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100162 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800163 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700164 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100165 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
166 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700167 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100168 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800169 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700170 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700171 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
172 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800173 availableOutputDevices,
jiabinf502d152019-08-07 14:43:33 -0700174 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800175 // 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
jiabinf502d152019-08-07 14:43:33 -0700177 if (!media_active_locally) {
178 devices.replaceDevicesByType(
179 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700180 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700181 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700182 }
François Gaffie2110e042015-03-24 08:41:51 +0100183 }
184 break;
185
186 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100187 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100188 // Force use of only devices on primary output if:
189 // - in call AND
190 // - cannot route from voice call RX OR
191 // - audio HAL version is < 3.0 and TX device is on the primary HW module
192 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700193 audio_devices_t txDevice = getDeviceForInputSource(
194 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100195 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700196 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700197 DeviceVector availPrimaryInputDevices =
198 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800199
200 // TODO: getPrimaryOutput return only devices from first module in
201 // audio_policy_configuration.xml, hearing aid is not there, but it's
202 // a primary device
203 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700204 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700205 primaryOutput->supportedDevices().types());
206 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700207 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100208
jiabinf502d152019-08-07 14:43:33 -0700209 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100210 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
211 ((availPrimaryInputDevices.getDevice(
212 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
213 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700214 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100215 }
216 }
Eric Laurent2517af32020-11-25 15:31:27 +0100217 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
218 if (!devices.isEmpty()) break;
219 devices = availableOutputDevices.getFirstDevicesFromTypes({
220 AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
221 AUDIO_DEVICE_OUT_WIRED_HEADSET,
222 AUDIO_DEVICE_OUT_LINE,
223 AUDIO_DEVICE_OUT_USB_HEADSET,
224 AUDIO_DEVICE_OUT_USB_DEVICE});
225 if (!devices.isEmpty()) break;
226 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
227 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100228
229 case STRATEGY_SONIFICATION:
230
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700231 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100232 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700233 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700234 devices = getDevicesForStrategyInt(
235 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100236 break;
237 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700238 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100239
240 case STRATEGY_ENFORCED_AUDIBLE:
241 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
242 // except:
243 // - when in call where it doesn't default to STRATEGY_PHONE behavior
244 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
245
246 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100247 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700248 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100249 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800250
251 // if SCO headset is connected and we are told to use it, play ringtone over
252 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700253 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700254 DeviceVector devices2;
255 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
256 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
257 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800258 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100259 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800260 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100261 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800262 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700263 if (!devices2.isEmpty()) {
264 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800265 break;
266 }
267 }
268 }
269 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100270 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
271 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700272 if (strategy == STRATEGY_SONIFICATION) {
273 devices.replaceDevicesByType(
274 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700275 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700276 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800277 }
jiabinf502d152019-08-07 14:43:33 -0700278 if (!devices2.isEmpty()) {
279 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800280 break;
281 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800282 }
283 }
François Gaffie2110e042015-03-24 08:41:51 +0100284 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700285 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100286
François Gaffie2110e042015-03-24 08:41:51 +0100287 case STRATEGY_ACCESSIBILITY:
288 if (strategy == STRATEGY_ACCESSIBILITY) {
289 // do not route accessibility prompts to a digital output currently configured with a
290 // compressed format as they would likely not be mixed and dropped.
291 for (size_t i = 0; i < outputs.size(); i++) {
292 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin5740f082019-08-19 15:08:30 -0700293 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin9a3361e2019-10-01 09:38:30 -0700294 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
295 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
296 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100297 }
298 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700299 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
300 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700301 return getDevicesForStrategyInt(
302 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800303 }
304 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700305 return getDevicesForStrategyInt(
306 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800307 }
François Gaffie2110e042015-03-24 08:41:51 +0100308 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800309 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700310 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100311
Eric Laurent28d09f02016-03-08 10:43:05 -0800312 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100313 case STRATEGY_REROUTING:
314 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700315 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100316 if (strategy != STRATEGY_SONIFICATION) {
317 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700318 sp<DeviceDescriptor> remoteSubmix;
319 if ((remoteSubmix = availableOutputDevices.getDevice(
320 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
321 AUDIO_FORMAT_DEFAULT)) != nullptr) {
322 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100323 }
324 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700325 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700326 devices = getDevicesForStrategyInt(
327 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700328 break;
329 }
Eric Laurenta9986862020-10-07 08:42:28 -0700330
jiabinf502d152019-08-07 14:43:33 -0700331 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100332 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700333 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100334 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900335 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700336 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900337 // Get the last connected device of wired and bluetooth a2dp
338 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
339 getLastRemovableMediaDevices());
340 } else {
341 // Get the last connected device of wired except bluetooth a2dp
342 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
343 getLastRemovableMediaDevices(GROUP_WIRED));
344 }
François Gaffie2110e042015-03-24 08:41:51 +0100345 }
jiabinf502d152019-08-07 14:43:33 -0700346 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100347 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700348 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100349 }
jiabinf502d152019-08-07 14:43:33 -0700350 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100351 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700352 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700353 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100354 }
jiabinf502d152019-08-07 14:43:33 -0700355 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700356 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100357 }
jiabinf502d152019-08-07 14:43:33 -0700358 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100359 if (strategy == STRATEGY_MEDIA) {
360 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700361 devices3 = availableOutputDevices.getDevicesFromTypes({
362 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100363 }
364
jiabinf502d152019-08-07 14:43:33 -0700365 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100366 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
367 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700368 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100369
370 // If hdmi system audio mode is on, remove speaker out of output list.
371 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100372 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100373 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700374 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100375 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700376
377 // for STRATEGY_SONIFICATION:
378 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700379 if (strategy == STRATEGY_SONIFICATION) {
380 devices.replaceDevicesByType(
381 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700382 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700383 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700384 }
François Gaffie2110e042015-03-24 08:41:51 +0100385 } break;
386
Eric Laurent21777f82019-12-06 18:12:06 -0800387 case STRATEGY_CALL_ASSISTANT:
388 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
389 break;
390
Eric Laurentd9766932020-08-07 14:01:22 -0700391 case STRATEGY_NONE:
392 // Happens when internal strategies are processed ("rerouting", "patch"...)
393 break;
394
François Gaffie2110e042015-03-24 08:41:51 +0100395 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700396 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100397 break;
398 }
399
jiabinf502d152019-08-07 14:43:33 -0700400 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700401 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700402 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
403 if (defaultOutputDevice != nullptr) {
404 devices.add(defaultOutputDevice);
405 }
406 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700407 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700408 }
jiabinf502d152019-08-07 14:43:33 -0700409
Eric Laurentd9766932020-08-07 14:01:22 -0700410 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800411 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700412 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100413}
414
415
jiabinf502d152019-08-07 14:43:33 -0700416sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100417{
Eric Laurentaf377772019-03-29 14:50:21 -0700418 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
419 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100420 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700421 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700422 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700423 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
424 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700425 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100426
Eric Laurentdc95a252018-04-12 12:46:56 -0700427 // when a call is active, force device selection to match source VOICE_COMMUNICATION
428 // for most other input sources to avoid rerouting call TX audio
429 if (isInCall()) {
430 switch (inputSource) {
431 case AUDIO_SOURCE_DEFAULT:
432 case AUDIO_SOURCE_MIC:
433 case AUDIO_SOURCE_VOICE_RECOGNITION:
434 case AUDIO_SOURCE_UNPROCESSED:
435 case AUDIO_SOURCE_HOTWORD:
436 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800437 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700438 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
439 break;
440 default:
441 break;
442 }
443 }
444
Eric Laurent2517af32020-11-25 15:31:27 +0100445 audio_devices_t commDeviceType =
446 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
447
François Gaffie2110e042015-03-24 08:41:51 +0100448 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100449 case AUDIO_SOURCE_DEFAULT:
450 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700451 device = availableDevices.getDevice(
452 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
453 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100454 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700455 device = availableDevices.getDevice(
456 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
457 if (device != nullptr) break;
458 }
459 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700460 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
461 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
462 AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700463 break;
François Gaffie2110e042015-03-24 08:41:51 +0100464
465 case AUDIO_SOURCE_VOICE_COMMUNICATION:
466 // Allow only use of devices on primary input if in call and HAL does not support routing
467 // to voice call path.
468 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700469 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
470 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700471 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700472 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100473 }
474
Eric Laurent2517af32020-11-25 15:31:27 +0100475 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100476 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700477 device = availableDevices.getDevice(
478 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
479 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100480 break;
481 }
Eric Laurent2517af32020-11-25 15:31:27 +0100482 }
483 switch (commDeviceType) {
484 case AUDIO_DEVICE_OUT_BLE_HEADSET:
485 device = availableDevices.getDevice(
486 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100487 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100488 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700489 device = availableDevices.getFirstExistingDevice({
490 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100491 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100492 default: // FORCE_NONE
493 device = availableDevices.getFirstExistingDevice({
494 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
495 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
496 break;
497
François Gaffie2110e042015-03-24 08:41:51 +0100498 }
499 break;
500
501 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800502 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100503 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700504 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700505 // We should not use primary output criteria for Hotword but rather limit
506 // to devices attached to the same HW module as the build in mic
507 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700508 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700509 }
Eric Laurent2517af32020-11-25 15:31:27 +0100510 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700511 device = availableDevices.getDevice(
512 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
513 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100514 }
jiabinf502d152019-08-07 14:43:33 -0700515 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700516 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
517 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
518 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100519 break;
520 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700521 // For a device without built-in mic, adding usb device
522 device = availableDevices.getFirstExistingDevice({
523 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
524 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100525 break;
526 case AUDIO_SOURCE_VOICE_DOWNLINK:
527 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700528 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700529 device = availableDevices.getDevice(
530 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100531 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800532 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700533 device = availableDevices.getFirstExistingDevice({
534 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
535 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800536 break;
François Gaffie2110e042015-03-24 08:41:51 +0100537 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700538 device = availableDevices.getDevice(
539 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100540 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800541 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700542 device = availableDevices.getDevice(
543 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100544 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800545 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700546 device = availableDevices.getDevice(
547 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800548 break;
François Gaffie2110e042015-03-24 08:41:51 +0100549 default:
550 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
551 break;
552 }
jiabinf502d152019-08-07 14:43:33 -0700553 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700554 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700555 device = availableDevices.getDevice(
556 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
557 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700558 "getDeviceForInputSource() no default device defined");
559 }
Eric Laurent2517af32020-11-25 15:31:27 +0100560
jiabinf502d152019-08-07 14:43:33 -0700561 ALOGV_IF(device != nullptr,
562 "getDeviceForInputSource()input source %d, device %08x",
563 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100564 return device;
565}
566
François Gaffiedc7553f2018-11-02 10:39:57 +0100567void Engine::updateDeviceSelectionCache()
568{
569 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700570 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100571 auto devices = getDevicesForProductStrategy(strategy->getId());
572 mDevicesForStrategies[strategy->getId()] = devices;
573 strategy->setDeviceTypes(devices.types());
574 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
575 }
576}
577
Eric Laurent2517af32020-11-25 15:31:27 +0100578product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
579 for (const auto& strategyMap : mLegacyStrategyMap) {
580 if (strategyMap.second == legacyStrategy) {
581 return strategyMap.first;
582 }
583 }
584 return PRODUCT_STRATEGY_NONE;
585}
François Gaffiedc7553f2018-11-02 10:39:57 +0100586
Eric Laurent2517af32020-11-25 15:31:27 +0100587audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
588 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
589 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
590 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
591 availableOutputDevices, strategy);
592 if (devices.size() > 0) {
593 return devices[0]->type();
594 }
595 return AUDIO_DEVICE_NONE;
596}
597
598DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
599 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
600 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700601 AudioDeviceTypeAddrVector preferredStrategyDevices;
602 const status_t status = getDevicesForRoleAndStrategy(
603 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700604 if (status == NO_ERROR) {
605 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100606 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700607 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
608 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
609 ALOGVV("%s using pref device %s for strategy %u",
610 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
611 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700612 }
613 }
Eric Laurent2517af32020-11-25 15:31:27 +0100614 return preferredAvailableDevVec;
615}
616
617DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
618 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
619 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
620 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
621
622 // When not in call, STRATEGY_PHONE and STRATEGY_DTMF follow STRATEGY_MEDIA
623 if (!isInCall() && (legacyStrategy == STRATEGY_PHONE || legacyStrategy == STRATEGY_DTMF)) {
624 legacyStrategy = STRATEGY_MEDIA;
625 strategy = getProductStrategyFromLegacy(STRATEGY_MEDIA);
626 }
627 // check if this strategy has a preferred device that is available,
628 // if yes, give priority to it.
629 DeviceVector preferredAvailableDevVec =
630 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
631 if (!preferredAvailableDevVec.isEmpty()) {
632 return preferredAvailableDevVec;
633 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700634
635 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
636 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
Eric Laurent2517af32020-11-25 15:31:27 +0100637
jiabinf502d152019-08-07 14:43:33 -0700638 return getDevicesForStrategyInt(legacyStrategy,
639 availableOutputDevices,
640 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100641}
642
643DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
644 const sp<DeviceDescriptor> &preferredDevice,
645 bool fromCache) const
646{
647 // First check for explict routing device
648 if (preferredDevice != nullptr) {
649 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
650 return DeviceVector(preferredDevice);
651 }
François Gaffiec005e562018-11-06 15:04:49 +0100652 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700653 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100654 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100655 //
656 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
657 // be by APM?
658 //
François Gaffiec005e562018-11-06 15:04:49 +0100659 // Honor explicit routing requests only if all active clients have a preferred route in which
660 // case the last active client route is used
661 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
662 if (device != nullptr) {
663 return DeviceVector(device);
664 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100665
666 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
667}
668
669DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
670{
671 auto attributes = getAttributesForStreamType(stream);
672 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
673}
674
675sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800676 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100677{
François Gaffiec005e562018-11-06 15:04:49 +0100678 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700679 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100680 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100681 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100682
François Gaffiedc7553f2018-11-02 10:39:57 +0100683 //
François Gaffiec005e562018-11-06 15:04:49 +0100684 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
685 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100686 //
François Gaffiec005e562018-11-06 15:04:49 +0100687 // Honor explicit routing requests only if all active clients have a preferred route in which
688 // case the last active client route is used
689 sp<DeviceDescriptor> device =
690 findPreferredDevice(inputs, attr.source, availableInputDevices);
691 if (device != nullptr) {
692 return device;
693 }
694
695 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
696 if (device != nullptr) {
697 return device;
698 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100699
jiabinf502d152019-08-07 14:43:33 -0700700 device = getDeviceForInputSource(attr.source);
701 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
702 // Return immediately if the device is null or it is not a remote submix device.
703 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100704 }
jiabinf502d152019-08-07 14:43:33 -0700705
706 // For remote submix device, try to find the device by address.
707 address = "0";
708 std::size_t pos;
709 std::string tags { attr.tags };
710 if ((pos = tags.find("addr=")) != std::string::npos) {
711 address = tags.substr(pos + std::strlen("addr="));
712 }
713 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100714 String8(address.c_str()),
715 AUDIO_FORMAT_DEFAULT);
716}
717
François Gaffie2110e042015-03-24 08:41:51 +0100718} // namespace audio_policy
719} // namespace android
720
721