blob: 1a903a64d0725825f4631faf5a36de98da1b1272 [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,
Eric Laurent2802b862021-03-01 09:36:16 +0100147 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800148{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100149 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
150
François Gaffie2110e042015-03-24 08:41:51 +0100151 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100152 case STRATEGY_SONIFICATION_RESPECTFUL: {
153 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800154 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700155 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700156 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100157 }
Eric Laurent2802b862021-03-01 09:36:16 +0100158 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100159 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100160 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100161 // Force use of only devices on primary output if:
162 // - in call AND
163 // - cannot route from voice call RX OR
164 // - audio HAL version is < 3.0 and TX device is on the primary HW module
165 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700166 audio_devices_t txDevice = getDeviceForInputSource(
167 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100168 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700169 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700170 DeviceVector availPrimaryInputDevices =
171 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800172
173 // TODO: getPrimaryOutput return only devices from first module in
174 // audio_policy_configuration.xml, hearing aid is not there, but it's
175 // a primary device
176 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700177 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700178 primaryOutput->supportedDevices().types());
179 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700180 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100181
jiabinf502d152019-08-07 14:43:33 -0700182 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100183 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
184 ((availPrimaryInputDevices.getDevice(
185 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
186 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700187 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100188 }
189 }
Eric Laurent2802b862021-03-01 09:36:16 +0100190 } break;
191 case STRATEGY_ACCESSIBILITY: {
192 // do not route accessibility prompts to a digital output currently configured with a
193 // compressed format as they would likely not be mixed and dropped.
194 for (size_t i = 0; i < outputs.size(); i++) {
195 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
196 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
197 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
198 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
199 AUDIO_DEVICE_OUT_HDMI_ARC}));
200 }
201 }
202 } break;
203 default:
204 break;
205 }
206}
207
Eric Laurent6cd5f902021-03-23 18:29:58 +0100208product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
209 const SwAudioOutputCollection &outputs) const {
210 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
211 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
212
213 if (isInCall()) {
214 switch (legacyStrategy) {
215 case STRATEGY_ACCESSIBILITY:
216 case STRATEGY_DTMF:
217 case STRATEGY_MEDIA:
218 case STRATEGY_SONIFICATION:
219 case STRATEGY_SONIFICATION_RESPECTFUL:
220 legacyStrategy = STRATEGY_PHONE;
221 break;
222
223 default:
224 return strategy;
225 }
226 } else {
227 switch (legacyStrategy) {
228 case STRATEGY_SONIFICATION_RESPECTFUL:
229 case STRATEGY_SONIFICATION:
230 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
231 legacyStrategy = STRATEGY_PHONE;
232 }
233 break;
234
235 case STRATEGY_ACCESSIBILITY:
236 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
237 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
238 legacyStrategy = STRATEGY_SONIFICATION;
239 }
240 break;
241
242 default:
243 return strategy;
244 }
245 }
246 return getProductStrategyFromLegacy(legacyStrategy);
247}
248
Eric Laurent2802b862021-03-01 09:36:16 +0100249DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
250 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100251 const SwAudioOutputCollection &outputs) const
252{
253 DeviceVector devices;
254
255 switch (strategy) {
256
257 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
258 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
259 break;
260
Eric Laurent2802b862021-03-01 09:36:16 +0100261 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100262 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
263 if (!devices.isEmpty()) break;
264 devices = availableOutputDevices.getFirstDevicesFromTypes({
265 AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
266 AUDIO_DEVICE_OUT_WIRED_HEADSET,
267 AUDIO_DEVICE_OUT_LINE,
268 AUDIO_DEVICE_OUT_USB_HEADSET,
269 AUDIO_DEVICE_OUT_USB_DEVICE});
270 if (!devices.isEmpty()) break;
271 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
272 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100273
274 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100275 case STRATEGY_ENFORCED_AUDIBLE:
276 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
277 // except:
278 // - when in call where it doesn't default to STRATEGY_PHONE behavior
279 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
280
281 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100282 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700283 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100284 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800285
286 // if SCO headset is connected and we are told to use it, play ringtone over
287 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700288 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700289 DeviceVector devices2;
290 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
291 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
292 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800293 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100294 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800295 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100296 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800297 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700298 if (!devices2.isEmpty()) {
299 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800300 break;
301 }
302 }
303 }
304 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100305 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
306 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700307 if (strategy == STRATEGY_SONIFICATION) {
308 devices.replaceDevicesByType(
309 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700310 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700311 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800312 }
jiabinf502d152019-08-07 14:43:33 -0700313 if (!devices2.isEmpty()) {
314 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800315 break;
316 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800317 }
318 }
François Gaffie2110e042015-03-24 08:41:51 +0100319 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700320 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100321
Eric Laurent6cd5f902021-03-23 18:29:58 +0100322 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100323 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100324 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100325 case STRATEGY_REROUTING:
326 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700327 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100328 if (strategy != STRATEGY_SONIFICATION) {
329 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700330 sp<DeviceDescriptor> remoteSubmix;
331 if ((remoteSubmix = availableOutputDevices.getDevice(
332 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
333 AUDIO_FORMAT_DEFAULT)) != nullptr) {
334 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100335 }
336 }
Eric Laurenta9986862020-10-07 08:42:28 -0700337
jiabinf502d152019-08-07 14:43:33 -0700338 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100339 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700340 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100341 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900342 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700343 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900344 // Get the last connected device of wired and bluetooth a2dp
345 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
346 getLastRemovableMediaDevices());
347 } else {
348 // Get the last connected device of wired except bluetooth a2dp
349 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
350 getLastRemovableMediaDevices(GROUP_WIRED));
351 }
François Gaffie2110e042015-03-24 08:41:51 +0100352 }
jiabinf502d152019-08-07 14:43:33 -0700353 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100354 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700355 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100356 }
jiabinf502d152019-08-07 14:43:33 -0700357 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100358 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700359 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700360 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100361 }
jiabinf502d152019-08-07 14:43:33 -0700362 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700363 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100364 }
jiabinf502d152019-08-07 14:43:33 -0700365 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100366 if (strategy == STRATEGY_MEDIA) {
367 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700368 devices3 = availableOutputDevices.getDevicesFromTypes({
369 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100370 }
371
jiabinf502d152019-08-07 14:43:33 -0700372 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100373 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
374 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700375 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100376
377 // If hdmi system audio mode is on, remove speaker out of output list.
378 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100379 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100380 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700381 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100382 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700383
Eric Laurent6cd5f902021-03-23 18:29:58 +0100384 bool mediaActiveLocally =
385 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
386 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
387 || outputs.isActiveLocally(
388 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
389 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
390 // - for STRATEGY_SONIFICATION:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700391 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100392 // - for STRATEGY_SONIFICATION_RESPECTFUL:
393 // if no media is playing on the device, check for mandatory use of "safe" speaker
394 // when media would have played on speaker, and the safe speaker path is available
395 if (strategy == STRATEGY_SONIFICATION
396 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700397 devices.replaceDevicesByType(
398 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700399 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700400 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700401 }
François Gaffie2110e042015-03-24 08:41:51 +0100402 } break;
403
Eric Laurent21777f82019-12-06 18:12:06 -0800404 case STRATEGY_CALL_ASSISTANT:
405 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
406 break;
407
Eric Laurentd9766932020-08-07 14:01:22 -0700408 case STRATEGY_NONE:
409 // Happens when internal strategies are processed ("rerouting", "patch"...)
410 break;
411
François Gaffie2110e042015-03-24 08:41:51 +0100412 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700413 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100414 break;
415 }
416
jiabinf502d152019-08-07 14:43:33 -0700417 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700418 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700419 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
420 if (defaultOutputDevice != nullptr) {
421 devices.add(defaultOutputDevice);
422 }
423 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700424 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700425 }
jiabinf502d152019-08-07 14:43:33 -0700426
Eric Laurentd9766932020-08-07 14:01:22 -0700427 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800428 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700429 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100430}
431
432
jiabinf502d152019-08-07 14:43:33 -0700433sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100434{
Eric Laurentaf377772019-03-29 14:50:21 -0700435 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
436 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100437 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700438 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700439 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700440 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
441 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700442 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100443
Eric Laurentdc95a252018-04-12 12:46:56 -0700444 // when a call is active, force device selection to match source VOICE_COMMUNICATION
445 // for most other input sources to avoid rerouting call TX audio
446 if (isInCall()) {
447 switch (inputSource) {
448 case AUDIO_SOURCE_DEFAULT:
449 case AUDIO_SOURCE_MIC:
450 case AUDIO_SOURCE_VOICE_RECOGNITION:
451 case AUDIO_SOURCE_UNPROCESSED:
452 case AUDIO_SOURCE_HOTWORD:
453 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800454 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700455 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
456 break;
457 default:
458 break;
459 }
460 }
461
Eric Laurent2517af32020-11-25 15:31:27 +0100462 audio_devices_t commDeviceType =
463 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
464
François Gaffie2110e042015-03-24 08:41:51 +0100465 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100466 case AUDIO_SOURCE_DEFAULT:
467 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700468 device = availableDevices.getDevice(
469 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
470 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100471 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700472 device = availableDevices.getDevice(
473 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
474 if (device != nullptr) break;
475 }
476 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700477 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
478 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500479 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700480 break;
François Gaffie2110e042015-03-24 08:41:51 +0100481
482 case AUDIO_SOURCE_VOICE_COMMUNICATION:
483 // Allow only use of devices on primary input if in call and HAL does not support routing
484 // to voice call path.
485 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700486 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
487 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700488 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700489 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100490 }
491
Eric Laurent2517af32020-11-25 15:31:27 +0100492 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100493 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700494 device = availableDevices.getDevice(
495 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
496 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100497 break;
498 }
Eric Laurent2517af32020-11-25 15:31:27 +0100499 }
500 switch (commDeviceType) {
501 case AUDIO_DEVICE_OUT_BLE_HEADSET:
502 device = availableDevices.getDevice(
503 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100504 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100505 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700506 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100507 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
508 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100509 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100510 default: // FORCE_NONE
511 device = availableDevices.getFirstExistingDevice({
512 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500513 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
514 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100515 break;
516
François Gaffie2110e042015-03-24 08:41:51 +0100517 }
518 break;
519
520 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800521 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500522 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
523 device = availableDevices.getDevice(
524 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
525 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700526 }
wei wanga7020522020-12-10 21:36:11 -0500527 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
528 // because sometimes user want to do voice search by bt remote
529 // even if BUILDIN_MIC is available.
530 device = availableDevices.getFirstExistingDevice({
531 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
532 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
533 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
534
535 break;
536 case AUDIO_SOURCE_HOTWORD:
537 // We should not use primary output criteria for Hotword but rather limit
538 // to devices attached to the same HW module as the build in mic
539 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
540 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100541 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700542 device = availableDevices.getDevice(
543 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
544 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100545 }
jiabinf502d152019-08-07 14:43:33 -0700546 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700547 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
548 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
549 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100550 break;
551 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700552 // For a device without built-in mic, adding usb device
553 device = availableDevices.getFirstExistingDevice({
554 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
555 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100556 break;
557 case AUDIO_SOURCE_VOICE_DOWNLINK:
558 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700559 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700560 device = availableDevices.getDevice(
561 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100562 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800563 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700564 device = availableDevices.getFirstExistingDevice({
565 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500566 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
567 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800568 break;
François Gaffie2110e042015-03-24 08:41:51 +0100569 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700570 device = availableDevices.getDevice(
571 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100572 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800573 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700574 device = availableDevices.getDevice(
575 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100576 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800577 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700578 device = availableDevices.getDevice(
579 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800580 break;
François Gaffie2110e042015-03-24 08:41:51 +0100581 default:
582 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
583 break;
584 }
jiabinf502d152019-08-07 14:43:33 -0700585 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700586 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700587 device = availableDevices.getDevice(
588 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
589 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700590 "getDeviceForInputSource() no default device defined");
591 }
Eric Laurent2517af32020-11-25 15:31:27 +0100592
jiabinf502d152019-08-07 14:43:33 -0700593 ALOGV_IF(device != nullptr,
594 "getDeviceForInputSource()input source %d, device %08x",
595 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100596 return device;
597}
598
François Gaffiedc7553f2018-11-02 10:39:57 +0100599void Engine::updateDeviceSelectionCache()
600{
601 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700602 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100603 auto devices = getDevicesForProductStrategy(strategy->getId());
604 mDevicesForStrategies[strategy->getId()] = devices;
605 strategy->setDeviceTypes(devices.types());
606 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
607 }
608}
609
Eric Laurent2517af32020-11-25 15:31:27 +0100610product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
611 for (const auto& strategyMap : mLegacyStrategyMap) {
612 if (strategyMap.second == legacyStrategy) {
613 return strategyMap.first;
614 }
615 }
616 return PRODUCT_STRATEGY_NONE;
617}
François Gaffiedc7553f2018-11-02 10:39:57 +0100618
Eric Laurent2517af32020-11-25 15:31:27 +0100619audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
620 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
621 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
622 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
623 availableOutputDevices, strategy);
624 if (devices.size() > 0) {
625 return devices[0]->type();
626 }
627 return AUDIO_DEVICE_NONE;
628}
629
630DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
631 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
632 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700633 AudioDeviceTypeAddrVector preferredStrategyDevices;
634 const status_t status = getDevicesForRoleAndStrategy(
635 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700636 if (status == NO_ERROR) {
637 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100638 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700639 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
640 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
641 ALOGVV("%s using pref device %s for strategy %u",
642 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
643 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700644 }
645 }
Eric Laurent2517af32020-11-25 15:31:27 +0100646 return preferredAvailableDevVec;
647}
648
Eric Laurent6cd5f902021-03-23 18:29:58 +0100649
Eric Laurent2517af32020-11-25 15:31:27 +0100650DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100651 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
652
653 // Take context into account to remap product strategy before
654 // checking preferred device for strategy and applying default routing rules
655 strategy = remapStrategyFromContext(strategy, outputs);
656
Eric Laurent2517af32020-11-25 15:31:27 +0100657 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
658 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
659
Eric Laurent6cd5f902021-03-23 18:29:58 +0100660 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100661
Eric Laurent6cd5f902021-03-23 18:29:58 +0100662 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100663
Eric Laurent2517af32020-11-25 15:31:27 +0100664 // check if this strategy has a preferred device that is available,
665 // if yes, give priority to it.
666 DeviceVector preferredAvailableDevVec =
667 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
668 if (!preferredAvailableDevVec.isEmpty()) {
669 return preferredAvailableDevVec;
670 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700671
jiabinf502d152019-08-07 14:43:33 -0700672 return getDevicesForStrategyInt(legacyStrategy,
673 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100674 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100675}
676
677DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
678 const sp<DeviceDescriptor> &preferredDevice,
679 bool fromCache) const
680{
681 // First check for explict routing device
682 if (preferredDevice != nullptr) {
683 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
684 return DeviceVector(preferredDevice);
685 }
François Gaffiec005e562018-11-06 15:04:49 +0100686 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700687 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100688 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100689 //
690 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
691 // be by APM?
692 //
François Gaffiec005e562018-11-06 15:04:49 +0100693 // Honor explicit routing requests only if all active clients have a preferred route in which
694 // case the last active client route is used
695 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
696 if (device != nullptr) {
697 return DeviceVector(device);
698 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100699
700 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
701}
702
703DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
704{
705 auto attributes = getAttributesForStreamType(stream);
706 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
707}
708
709sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800710 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100711{
François Gaffiec005e562018-11-06 15:04:49 +0100712 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700713 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100714 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100715 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100716
François Gaffiedc7553f2018-11-02 10:39:57 +0100717 //
François Gaffiec005e562018-11-06 15:04:49 +0100718 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
719 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100720 //
François Gaffiec005e562018-11-06 15:04:49 +0100721 // Honor explicit routing requests only if all active clients have a preferred route in which
722 // case the last active client route is used
723 sp<DeviceDescriptor> device =
724 findPreferredDevice(inputs, attr.source, availableInputDevices);
725 if (device != nullptr) {
726 return device;
727 }
728
729 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
730 if (device != nullptr) {
731 return device;
732 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100733
jiabinf502d152019-08-07 14:43:33 -0700734 device = getDeviceForInputSource(attr.source);
735 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
736 // Return immediately if the device is null or it is not a remote submix device.
737 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100738 }
jiabinf502d152019-08-07 14:43:33 -0700739
740 // For remote submix device, try to find the device by address.
741 address = "0";
742 std::size_t pos;
743 std::string tags { attr.tags };
744 if ((pos = tags.find("addr=")) != std::string::npos) {
745 address = tags.substr(pos + std::strlen("addr="));
746 }
747 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100748 String8(address.c_str()),
749 AUDIO_FORMAT_DEFAULT);
750}
751
François Gaffie2110e042015-03-24 08:41:51 +0100752} // namespace audio_policy
753} // namespace android
754
755