blob: 380bf6bd9df485c12fddacb85b500595a1c9420f [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
Eric Laurent2802b862021-03-01 09:36:16 +0100145void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
146 DeviceVector& availableOutputDevices,
147 const DeviceVector availableInputDevices,
148 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800149{
François Gaffie2110e042015-03-24 08:41:51 +0100150 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100151 case STRATEGY_SONIFICATION_RESPECTFUL: {
152 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800153 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700154 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700155 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100156 }
Eric Laurent2802b862021-03-01 09:36:16 +0100157 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100158 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100159 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100160 // Force use of only devices on primary output if:
161 // - in call AND
162 // - cannot route from voice call RX OR
163 // - audio HAL version is < 3.0 and TX device is on the primary HW module
164 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700165 audio_devices_t txDevice = getDeviceForInputSource(
166 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100167 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700168 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700169 DeviceVector availPrimaryInputDevices =
170 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800171
172 // TODO: getPrimaryOutput return only devices from first module in
173 // audio_policy_configuration.xml, hearing aid is not there, but it's
174 // a primary device
175 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700176 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700177 primaryOutput->supportedDevices().types());
178 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700179 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100180
jiabinf502d152019-08-07 14:43:33 -0700181 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100182 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
183 ((availPrimaryInputDevices.getDevice(
184 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
185 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700186 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100187 }
188 }
Eric Laurent2802b862021-03-01 09:36:16 +0100189 } break;
190 case STRATEGY_ACCESSIBILITY: {
191 // do not route accessibility prompts to a digital output currently configured with a
192 // compressed format as they would likely not be mixed and dropped.
193 for (size_t i = 0; i < outputs.size(); i++) {
194 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
195 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
196 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
197 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
198 AUDIO_DEVICE_OUT_HDMI_ARC}));
199 }
200 }
201 } break;
202 default:
203 break;
204 }
205}
206
207DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
208 DeviceVector availableOutputDevices,
209 DeviceVector availableInputDevices,
210 const SwAudioOutputCollection &outputs) const
211{
212 DeviceVector devices;
213
214 switch (strategy) {
215
216 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
217 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
218 break;
219
220 case STRATEGY_SONIFICATION_RESPECTFUL:
221 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
222 devices = getDevicesForStrategyInt(
223 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
224 } else {
225 bool media_active_locally =
226 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
227 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
228 || outputs.isActiveLocally(
229 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
230 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
231 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
232 availableOutputDevices,
233 availableInputDevices, outputs);
234 // if no media is playing on the device, check for mandatory use of "safe" speaker
235 // when media would have played on speaker, and the safe speaker path is available
236 if (!media_active_locally) {
237 devices.replaceDevicesByType(
238 AUDIO_DEVICE_OUT_SPEAKER,
239 availableOutputDevices.getDevicesFromType(
240 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
241 }
242 }
243 break;
244
245 case STRATEGY_DTMF:
246 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100247 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
248 if (!devices.isEmpty()) break;
249 devices = availableOutputDevices.getFirstDevicesFromTypes({
250 AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
251 AUDIO_DEVICE_OUT_WIRED_HEADSET,
252 AUDIO_DEVICE_OUT_LINE,
253 AUDIO_DEVICE_OUT_USB_HEADSET,
254 AUDIO_DEVICE_OUT_USB_DEVICE});
255 if (!devices.isEmpty()) break;
256 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
257 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100258
259 case STRATEGY_SONIFICATION:
260
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700261 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100262 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700263 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700264 devices = getDevicesForStrategyInt(
265 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100266 break;
267 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700268 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100269
270 case STRATEGY_ENFORCED_AUDIBLE:
271 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
272 // except:
273 // - when in call where it doesn't default to STRATEGY_PHONE behavior
274 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
275
276 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100277 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700278 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100279 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800280
281 // if SCO headset is connected and we are told to use it, play ringtone over
282 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700283 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700284 DeviceVector devices2;
285 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
286 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
287 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800288 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100289 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800290 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100291 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800292 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700293 if (!devices2.isEmpty()) {
294 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800295 break;
296 }
297 }
298 }
299 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100300 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
301 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700302 if (strategy == STRATEGY_SONIFICATION) {
303 devices.replaceDevicesByType(
304 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700305 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700306 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800307 }
jiabinf502d152019-08-07 14:43:33 -0700308 if (!devices2.isEmpty()) {
309 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800310 break;
311 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800312 }
313 }
François Gaffie2110e042015-03-24 08:41:51 +0100314 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700315 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100316
François Gaffie2110e042015-03-24 08:41:51 +0100317 case STRATEGY_ACCESSIBILITY:
318 if (strategy == STRATEGY_ACCESSIBILITY) {
Eric Laurent83d17c22019-04-02 17:10:01 -0700319 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
320 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700321 return getDevicesForStrategyInt(
322 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800323 }
324 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700325 return getDevicesForStrategyInt(
326 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800327 }
François Gaffie2110e042015-03-24 08:41:51 +0100328 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800329 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700330 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100331
Eric Laurent28d09f02016-03-08 10:43:05 -0800332 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100333 case STRATEGY_REROUTING:
334 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700335 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100336 if (strategy != STRATEGY_SONIFICATION) {
337 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700338 sp<DeviceDescriptor> remoteSubmix;
339 if ((remoteSubmix = availableOutputDevices.getDevice(
340 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
341 AUDIO_FORMAT_DEFAULT)) != nullptr) {
342 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100343 }
344 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700345 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700346 devices = getDevicesForStrategyInt(
347 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700348 break;
349 }
Eric Laurenta9986862020-10-07 08:42:28 -0700350
jiabinf502d152019-08-07 14:43:33 -0700351 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100352 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700353 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100354 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900355 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700356 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900357 // Get the last connected device of wired and bluetooth a2dp
358 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
359 getLastRemovableMediaDevices());
360 } else {
361 // Get the last connected device of wired except bluetooth a2dp
362 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
363 getLastRemovableMediaDevices(GROUP_WIRED));
364 }
François Gaffie2110e042015-03-24 08:41:51 +0100365 }
jiabinf502d152019-08-07 14:43:33 -0700366 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100367 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700368 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100369 }
jiabinf502d152019-08-07 14:43:33 -0700370 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100371 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700372 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700373 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100374 }
jiabinf502d152019-08-07 14:43:33 -0700375 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700376 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100377 }
jiabinf502d152019-08-07 14:43:33 -0700378 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100379 if (strategy == STRATEGY_MEDIA) {
380 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700381 devices3 = availableOutputDevices.getDevicesFromTypes({
382 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100383 }
384
jiabinf502d152019-08-07 14:43:33 -0700385 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100386 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
387 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700388 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100389
390 // If hdmi system audio mode is on, remove speaker out of output list.
391 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100392 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100393 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700394 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100395 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700396
397 // for STRATEGY_SONIFICATION:
398 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700399 if (strategy == STRATEGY_SONIFICATION) {
400 devices.replaceDevicesByType(
401 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700402 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700403 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700404 }
François Gaffie2110e042015-03-24 08:41:51 +0100405 } break;
406
Eric Laurent21777f82019-12-06 18:12:06 -0800407 case STRATEGY_CALL_ASSISTANT:
408 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
409 break;
410
Eric Laurentd9766932020-08-07 14:01:22 -0700411 case STRATEGY_NONE:
412 // Happens when internal strategies are processed ("rerouting", "patch"...)
413 break;
414
François Gaffie2110e042015-03-24 08:41:51 +0100415 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700416 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100417 break;
418 }
419
jiabinf502d152019-08-07 14:43:33 -0700420 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700421 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700422 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
423 if (defaultOutputDevice != nullptr) {
424 devices.add(defaultOutputDevice);
425 }
426 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700427 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700428 }
jiabinf502d152019-08-07 14:43:33 -0700429
Eric Laurentd9766932020-08-07 14:01:22 -0700430 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800431 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700432 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100433}
434
435
jiabinf502d152019-08-07 14:43:33 -0700436sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100437{
Eric Laurentaf377772019-03-29 14:50:21 -0700438 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
439 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100440 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700441 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700442 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700443 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
444 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700445 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100446
Eric Laurentdc95a252018-04-12 12:46:56 -0700447 // when a call is active, force device selection to match source VOICE_COMMUNICATION
448 // for most other input sources to avoid rerouting call TX audio
449 if (isInCall()) {
450 switch (inputSource) {
451 case AUDIO_SOURCE_DEFAULT:
452 case AUDIO_SOURCE_MIC:
453 case AUDIO_SOURCE_VOICE_RECOGNITION:
454 case AUDIO_SOURCE_UNPROCESSED:
455 case AUDIO_SOURCE_HOTWORD:
456 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800457 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700458 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
459 break;
460 default:
461 break;
462 }
463 }
464
Eric Laurent2517af32020-11-25 15:31:27 +0100465 audio_devices_t commDeviceType =
466 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
467
François Gaffie2110e042015-03-24 08:41:51 +0100468 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100469 case AUDIO_SOURCE_DEFAULT:
470 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700471 device = availableDevices.getDevice(
472 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
473 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100474 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700475 device = availableDevices.getDevice(
476 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
477 if (device != nullptr) break;
478 }
479 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700480 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
481 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500482 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700483 break;
François Gaffie2110e042015-03-24 08:41:51 +0100484
485 case AUDIO_SOURCE_VOICE_COMMUNICATION:
486 // Allow only use of devices on primary input if in call and HAL does not support routing
487 // to voice call path.
488 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700489 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
490 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700491 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700492 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100493 }
494
Eric Laurent2517af32020-11-25 15:31:27 +0100495 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100496 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700497 device = availableDevices.getDevice(
498 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
499 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100500 break;
501 }
Eric Laurent2517af32020-11-25 15:31:27 +0100502 }
503 switch (commDeviceType) {
504 case AUDIO_DEVICE_OUT_BLE_HEADSET:
505 device = availableDevices.getDevice(
506 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100507 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100508 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700509 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100510 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
511 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100512 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100513 default: // FORCE_NONE
514 device = availableDevices.getFirstExistingDevice({
515 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500516 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
517 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100518 break;
519
François Gaffie2110e042015-03-24 08:41:51 +0100520 }
521 break;
522
523 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800524 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500525 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
526 device = availableDevices.getDevice(
527 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
528 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700529 }
wei wanga7020522020-12-10 21:36:11 -0500530 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
531 // because sometimes user want to do voice search by bt remote
532 // even if BUILDIN_MIC is available.
533 device = availableDevices.getFirstExistingDevice({
534 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
535 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
536 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
537
538 break;
539 case AUDIO_SOURCE_HOTWORD:
540 // We should not use primary output criteria for Hotword but rather limit
541 // to devices attached to the same HW module as the build in mic
542 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
543 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100544 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700545 device = availableDevices.getDevice(
546 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
547 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100548 }
jiabinf502d152019-08-07 14:43:33 -0700549 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700550 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
551 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
552 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100553 break;
554 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700555 // For a device without built-in mic, adding usb device
556 device = availableDevices.getFirstExistingDevice({
557 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
558 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100559 break;
560 case AUDIO_SOURCE_VOICE_DOWNLINK:
561 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700562 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700563 device = availableDevices.getDevice(
564 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100565 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800566 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700567 device = availableDevices.getFirstExistingDevice({
568 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500569 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
570 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800571 break;
François Gaffie2110e042015-03-24 08:41:51 +0100572 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700573 device = availableDevices.getDevice(
574 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100575 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800576 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700577 device = availableDevices.getDevice(
578 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100579 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800580 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700581 device = availableDevices.getDevice(
582 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800583 break;
François Gaffie2110e042015-03-24 08:41:51 +0100584 default:
585 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
586 break;
587 }
jiabinf502d152019-08-07 14:43:33 -0700588 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700589 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700590 device = availableDevices.getDevice(
591 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
592 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700593 "getDeviceForInputSource() no default device defined");
594 }
Eric Laurent2517af32020-11-25 15:31:27 +0100595
jiabinf502d152019-08-07 14:43:33 -0700596 ALOGV_IF(device != nullptr,
597 "getDeviceForInputSource()input source %d, device %08x",
598 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100599 return device;
600}
601
François Gaffiedc7553f2018-11-02 10:39:57 +0100602void Engine::updateDeviceSelectionCache()
603{
604 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700605 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100606 auto devices = getDevicesForProductStrategy(strategy->getId());
607 mDevicesForStrategies[strategy->getId()] = devices;
608 strategy->setDeviceTypes(devices.types());
609 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
610 }
611}
612
Eric Laurent2517af32020-11-25 15:31:27 +0100613product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
614 for (const auto& strategyMap : mLegacyStrategyMap) {
615 if (strategyMap.second == legacyStrategy) {
616 return strategyMap.first;
617 }
618 }
619 return PRODUCT_STRATEGY_NONE;
620}
François Gaffiedc7553f2018-11-02 10:39:57 +0100621
Eric Laurent2517af32020-11-25 15:31:27 +0100622audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
623 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
624 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
625 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
626 availableOutputDevices, strategy);
627 if (devices.size() > 0) {
628 return devices[0]->type();
629 }
630 return AUDIO_DEVICE_NONE;
631}
632
633DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
634 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
635 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700636 AudioDeviceTypeAddrVector preferredStrategyDevices;
637 const status_t status = getDevicesForRoleAndStrategy(
638 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700639 if (status == NO_ERROR) {
640 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100641 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700642 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
643 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
644 ALOGVV("%s using pref device %s for strategy %u",
645 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
646 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700647 }
648 }
Eric Laurent2517af32020-11-25 15:31:27 +0100649 return preferredAvailableDevVec;
650}
651
652DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
653 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
654 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
655 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
656
Eric Laurent2802b862021-03-01 09:36:16 +0100657 // When not in call, STRATEGY_DTMF follows STRATEGY_MEDIA
658 if (!isInCall() && legacyStrategy == STRATEGY_DTMF) {
Eric Laurent2517af32020-11-25 15:31:27 +0100659 legacyStrategy = STRATEGY_MEDIA;
660 strategy = getProductStrategyFromLegacy(STRATEGY_MEDIA);
661 }
Eric Laurent2802b862021-03-01 09:36:16 +0100662
663 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
664 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
665
666 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices,
667 availableInputDevices, outputs);
668
Eric Laurent2517af32020-11-25 15:31:27 +0100669 // check if this strategy has a preferred device that is available,
670 // if yes, give priority to it.
671 DeviceVector preferredAvailableDevVec =
672 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
673 if (!preferredAvailableDevVec.isEmpty()) {
674 return preferredAvailableDevVec;
675 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700676
jiabinf502d152019-08-07 14:43:33 -0700677 return getDevicesForStrategyInt(legacyStrategy,
678 availableOutputDevices,
679 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100680}
681
682DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
683 const sp<DeviceDescriptor> &preferredDevice,
684 bool fromCache) const
685{
686 // First check for explict routing device
687 if (preferredDevice != nullptr) {
688 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
689 return DeviceVector(preferredDevice);
690 }
François Gaffiec005e562018-11-06 15:04:49 +0100691 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700692 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100693 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100694 //
695 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
696 // be by APM?
697 //
François Gaffiec005e562018-11-06 15:04:49 +0100698 // Honor explicit routing requests only if all active clients have a preferred route in which
699 // case the last active client route is used
700 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
701 if (device != nullptr) {
702 return DeviceVector(device);
703 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100704
705 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
706}
707
708DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
709{
710 auto attributes = getAttributesForStreamType(stream);
711 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
712}
713
714sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800715 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100716{
François Gaffiec005e562018-11-06 15:04:49 +0100717 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700718 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100719 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100720 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100721
François Gaffiedc7553f2018-11-02 10:39:57 +0100722 //
François Gaffiec005e562018-11-06 15:04:49 +0100723 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
724 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100725 //
François Gaffiec005e562018-11-06 15:04:49 +0100726 // Honor explicit routing requests only if all active clients have a preferred route in which
727 // case the last active client route is used
728 sp<DeviceDescriptor> device =
729 findPreferredDevice(inputs, attr.source, availableInputDevices);
730 if (device != nullptr) {
731 return device;
732 }
733
734 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
735 if (device != nullptr) {
736 return device;
737 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100738
jiabinf502d152019-08-07 14:43:33 -0700739 device = getDeviceForInputSource(attr.source);
740 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
741 // Return immediately if the device is null or it is not a remote submix device.
742 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100743 }
jiabinf502d152019-08-07 14:43:33 -0700744
745 // For remote submix device, try to find the device by address.
746 address = "0";
747 std::size_t pos;
748 std::string tags { attr.tags };
749 if ((pos = tags.find("addr=")) != std::string::npos) {
750 address = tags.substr(pos + std::strlen("addr="));
751 }
752 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100753 String8(address.c_str()),
754 AUDIO_FORMAT_DEFAULT);
755}
756
François Gaffie2110e042015-03-24 08:41:51 +0100757} // namespace audio_policy
758} // namespace android
759
760