blob: d67e6c4d28f5d755541ce208aa0950f49d506eae [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) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000081 ALOGW("setForceUse() invalid config %d for COMMUNICATION", config);
François Gaffie2110e042015-03-24 08:41:51 +010082 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 ) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000091 ALOGW("setForceUse() invalid config %d for MEDIA", config);
François Gaffie2110e042015-03-24 08:41:51 +010092 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) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000098 ALOGW("setForceUse() invalid config %d for RECORD", config);
François Gaffie2110e042015-03-24 08:41:51 +010099 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) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000108 ALOGW("setForceUse() invalid config %d for DOCK", config);
109 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100110 }
François Gaffie2110e042015-03-24 08:41:51 +0100111 break;
112 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
113 if (config != AUDIO_POLICY_FORCE_NONE &&
114 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000115 ALOGW("setForceUse() invalid config %d for SYSTEM", config);
116 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100117 }
François Gaffie2110e042015-03-24 08:41:51 +0100118 break;
119 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
120 if (config != AUDIO_POLICY_FORCE_NONE &&
121 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800122 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000123 return BAD_VALUE;
Phil Burk09bc4612016-02-24 15:58:15 -0800124 }
Phil Burk09bc4612016-02-24 15:58:15 -0800125 break;
126 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
127 if (config != AUDIO_POLICY_FORCE_NONE &&
128 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700129 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
130 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800131 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
132 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100133 }
François Gaffie2110e042015-03-24 08:41:51 +0100134 break;
Jack He96117ae2018-02-12 20:52:53 -0800135 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
136 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000137 ALOGW("setForceUse() invalid config %d for VIBRATE_RINGING", config);
Jack He96117ae2018-02-12 20:52:53 -0800138 return BAD_VALUE;
139 }
Jack He96117ae2018-02-12 20:52:53 -0800140 break;
François Gaffie2110e042015-03-24 08:41:51 +0100141 default:
142 ALOGW("setForceUse() invalid usage %d", usage);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000143 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100144 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100145 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100146}
147
Eric Laurent2802b862021-03-01 09:36:16 +0100148void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
149 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100150 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800151{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100152 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
153
François Gaffie2110e042015-03-24 08:41:51 +0100154 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100155 case STRATEGY_SONIFICATION_RESPECTFUL: {
156 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800157 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700158 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700159 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100160 }
Eric Laurent2802b862021-03-01 09:36:16 +0100161 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100162 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100163 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100164 // Force use of only devices on primary output if:
165 // - in call AND
166 // - cannot route from voice call RX OR
167 // - audio HAL version is < 3.0 and TX device is on the primary HW module
168 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700169 audio_devices_t txDevice = getDeviceForInputSource(
170 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100171 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700172 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700173 DeviceVector availPrimaryInputDevices =
174 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800175
176 // TODO: getPrimaryOutput return only devices from first module in
177 // audio_policy_configuration.xml, hearing aid is not there, but it's
178 // a primary device
179 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700180 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700181 primaryOutput->supportedDevices().types());
182 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700183 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100184
jiabinf502d152019-08-07 14:43:33 -0700185 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100186 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
187 ((availPrimaryInputDevices.getDevice(
188 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
189 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700190 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100191 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200192
Eric Laurent97cec6f2021-05-20 13:52:47 +0200193 }
194 // Do not use A2DP devices when in call but use them when not in call
195 // (e.g for voice mail playback)
196 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200197 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
198 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
199 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100200 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200201 // If connected to a dock, never use the device speaker for calls
202 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
203 .isEmpty()) {
204 availableOutputDevices.remove(
205 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
206 }
Eric Laurent2802b862021-03-01 09:36:16 +0100207 } break;
208 case STRATEGY_ACCESSIBILITY: {
209 // do not route accessibility prompts to a digital output currently configured with a
210 // compressed format as they would likely not be mixed and dropped.
211 for (size_t i = 0; i < outputs.size(); i++) {
212 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
213 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
214 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
215 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800216 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100217 }
218 }
219 } break;
220 default:
221 break;
222 }
223}
224
Eric Laurent6cd5f902021-03-23 18:29:58 +0100225product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
226 const SwAudioOutputCollection &outputs) const {
227 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
228 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
229
230 if (isInCall()) {
231 switch (legacyStrategy) {
232 case STRATEGY_ACCESSIBILITY:
233 case STRATEGY_DTMF:
234 case STRATEGY_MEDIA:
235 case STRATEGY_SONIFICATION:
236 case STRATEGY_SONIFICATION_RESPECTFUL:
237 legacyStrategy = STRATEGY_PHONE;
238 break;
239
240 default:
241 return strategy;
242 }
243 } else {
244 switch (legacyStrategy) {
245 case STRATEGY_SONIFICATION_RESPECTFUL:
246 case STRATEGY_SONIFICATION:
247 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
248 legacyStrategy = STRATEGY_PHONE;
249 }
250 break;
251
252 case STRATEGY_ACCESSIBILITY:
253 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
254 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
255 legacyStrategy = STRATEGY_SONIFICATION;
256 }
257 break;
258
259 default:
260 return strategy;
261 }
262 }
263 return getProductStrategyFromLegacy(legacyStrategy);
264}
265
Eric Laurent2802b862021-03-01 09:36:16 +0100266DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
267 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100268 const SwAudioOutputCollection &outputs) const
269{
270 DeviceVector devices;
271
272 switch (strategy) {
273
274 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
275 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
276 break;
277
Eric Laurent2802b862021-03-01 09:36:16 +0100278 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100279 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
280 if (!devices.isEmpty()) break;
Eric Laurentc0697592021-05-10 11:45:22 +0200281 devices = availableOutputDevices.getFirstDevicesFromTypes(
Eric Laurent34b09d02022-03-24 10:41:36 +0100282 getLastRemovableMediaDevices(GROUP_NONE, {AUDIO_DEVICE_OUT_BLE_HEADSET}));
Eric Laurent2517af32020-11-25 15:31:27 +0100283 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100284 devices = availableOutputDevices.getFirstDevicesFromTypes({
285 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE});
Eric Laurent2517af32020-11-25 15:31:27 +0100286 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100287
288 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100289 case STRATEGY_ENFORCED_AUDIBLE:
290 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
291 // except:
292 // - when in call where it doesn't default to STRATEGY_PHONE behavior
293 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
294
295 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100296 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700297 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100298 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800299
300 // if SCO headset is connected and we are told to use it, play ringtone over
301 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700302 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700303 DeviceVector devices2;
304 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
305 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
306 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800307 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100308 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800309 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100310 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800311 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700312 if (!devices2.isEmpty()) {
313 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800314 break;
315 }
316 }
317 }
318 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100319 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
320 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700321 if (strategy == STRATEGY_SONIFICATION) {
322 devices.replaceDevicesByType(
323 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700324 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700325 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800326 }
jiabinf502d152019-08-07 14:43:33 -0700327 if (!devices2.isEmpty()) {
328 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800329 break;
330 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800331 }
332 }
François Gaffie2110e042015-03-24 08:41:51 +0100333 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700334 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100335
Eric Laurent6cd5f902021-03-23 18:29:58 +0100336 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100337 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100338 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100339 case STRATEGY_REROUTING:
340 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700341 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100342 if (strategy != STRATEGY_SONIFICATION) {
343 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700344 sp<DeviceDescriptor> remoteSubmix;
345 if ((remoteSubmix = availableOutputDevices.getDevice(
346 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
347 AUDIO_FORMAT_DEFAULT)) != nullptr) {
348 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100349 }
350 }
Eric Laurenta9986862020-10-07 08:42:28 -0700351
jiabinf502d152019-08-07 14:43:33 -0700352 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100353 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700354 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100355 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100356
357 // LE audio broadcast device is only used if:
358 // - No call is active
359 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
360 // OR the LE audio unicast device is not active
361 if (devices2.isEmpty() && !isInCall()
362 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
363 legacy_strategy topActiveStrategy = STRATEGY_NONE;
364 for (const auto &ps : getOrderedProductStrategies()) {
365 if (outputs.isStrategyActive(ps)) {
366 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
367 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
368 break;
369 }
370 }
371
372 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
373 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
374 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
375 devices2 =
376 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
377 }
378 }
379
Baekgyeong Kim08451522019-11-01 20:40:02 +0900380 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700381 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900382 // Get the last connected device of wired and bluetooth a2dp
383 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
384 getLastRemovableMediaDevices());
385 } else {
386 // Get the last connected device of wired except bluetooth a2dp
387 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
388 getLastRemovableMediaDevices(GROUP_WIRED));
389 }
François Gaffie2110e042015-03-24 08:41:51 +0100390 }
jiabinf502d152019-08-07 14:43:33 -0700391 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100392 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700393 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100394 }
jiabinf502d152019-08-07 14:43:33 -0700395 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100396 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700397 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700398 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100399 }
jiabinf502d152019-08-07 14:43:33 -0700400 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100401 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
402 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100403 }
jiabinf502d152019-08-07 14:43:33 -0700404 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100405 if (strategy == STRATEGY_MEDIA) {
406 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700407 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800408 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
409 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
410 });
François Gaffie2110e042015-03-24 08:41:51 +0100411 }
412
jiabinf502d152019-08-07 14:43:33 -0700413 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100414 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
415 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700416 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100417
418 // If hdmi system audio mode is on, remove speaker out of output list.
419 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100420 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100421 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700422 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100423 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700424
Eric Laurent6cd5f902021-03-23 18:29:58 +0100425 bool mediaActiveLocally =
426 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
427 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
428 || outputs.isActiveLocally(
429 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
430 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800431
432 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
433 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700434 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100435 // - for STRATEGY_SONIFICATION_RESPECTFUL:
436 // if no media is playing on the device, check for mandatory use of "safe" speaker
437 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800438 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100439 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700440 devices.replaceDevicesByType(
441 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700442 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700443 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700444 }
François Gaffie2110e042015-03-24 08:41:51 +0100445 } break;
446
Eric Laurent21777f82019-12-06 18:12:06 -0800447 case STRATEGY_CALL_ASSISTANT:
448 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
449 break;
450
Eric Laurentd9766932020-08-07 14:01:22 -0700451 case STRATEGY_NONE:
452 // Happens when internal strategies are processed ("rerouting", "patch"...)
453 break;
454
François Gaffie2110e042015-03-24 08:41:51 +0100455 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700456 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100457 break;
458 }
459
jiabinf502d152019-08-07 14:43:33 -0700460 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700461 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700462 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
463 if (defaultOutputDevice != nullptr) {
464 devices.add(defaultOutputDevice);
465 }
466 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700467 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700468 }
jiabinf502d152019-08-07 14:43:33 -0700469
Eric Laurentd9766932020-08-07 14:01:22 -0700470 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800471 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700472 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100473}
474
475
jiabinf502d152019-08-07 14:43:33 -0700476sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100477{
Eric Laurentaf377772019-03-29 14:50:21 -0700478 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
479 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100480 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700481 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700482 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700483 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
484 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700485 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100486
Eric Laurentdc95a252018-04-12 12:46:56 -0700487 // when a call is active, force device selection to match source VOICE_COMMUNICATION
488 // for most other input sources to avoid rerouting call TX audio
489 if (isInCall()) {
490 switch (inputSource) {
491 case AUDIO_SOURCE_DEFAULT:
492 case AUDIO_SOURCE_MIC:
493 case AUDIO_SOURCE_VOICE_RECOGNITION:
494 case AUDIO_SOURCE_UNPROCESSED:
495 case AUDIO_SOURCE_HOTWORD:
496 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800497 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800498 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700499 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
500 break;
501 default:
502 break;
503 }
504 }
505
Eric Laurent2517af32020-11-25 15:31:27 +0100506 audio_devices_t commDeviceType =
507 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
508
François Gaffie2110e042015-03-24 08:41:51 +0100509 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100510 case AUDIO_SOURCE_DEFAULT:
511 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700512 device = availableDevices.getDevice(
513 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
514 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100515 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700516 device = availableDevices.getDevice(
517 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
518 if (device != nullptr) break;
519 }
520 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200521 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700522 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500523 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700524 break;
François Gaffie2110e042015-03-24 08:41:51 +0100525
526 case AUDIO_SOURCE_VOICE_COMMUNICATION:
527 // Allow only use of devices on primary input if in call and HAL does not support routing
528 // to voice call path.
529 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700530 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
531 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700532 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700533 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100534 }
535
Eric Laurent2517af32020-11-25 15:31:27 +0100536 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100537 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700538 device = availableDevices.getDevice(
539 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
540 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100541 break;
542 }
Eric Laurent2517af32020-11-25 15:31:27 +0100543 }
544 switch (commDeviceType) {
545 case AUDIO_DEVICE_OUT_BLE_HEADSET:
546 device = availableDevices.getDevice(
Grzegorz Kołodziejczyk2e4e3e62021-07-21 15:35:03 +0200547 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100548 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100549 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700550 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100551 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
552 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100553 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100554 default: // FORCE_NONE
555 device = availableDevices.getFirstExistingDevice({
556 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500557 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
558 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100559 break;
560
François Gaffie2110e042015-03-24 08:41:51 +0100561 }
562 break;
563
564 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800565 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500566 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
567 device = availableDevices.getDevice(
568 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
569 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700570 }
wei wanga7020522020-12-10 21:36:11 -0500571 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
572 // because sometimes user want to do voice search by bt remote
573 // even if BUILDIN_MIC is available.
574 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200575 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500576 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
577 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
578
579 break;
580 case AUDIO_SOURCE_HOTWORD:
581 // We should not use primary output criteria for Hotword but rather limit
582 // to devices attached to the same HW module as the build in mic
583 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
584 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100585 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700586 device = availableDevices.getDevice(
587 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
588 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100589 }
jiabinf502d152019-08-07 14:43:33 -0700590 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200591 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700592 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
593 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100594 break;
595 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700596 // For a device without built-in mic, adding usb device
597 device = availableDevices.getFirstExistingDevice({
598 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
599 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100600 break;
601 case AUDIO_SOURCE_VOICE_DOWNLINK:
602 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700603 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700604 device = availableDevices.getDevice(
605 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100606 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800607 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700608 device = availableDevices.getFirstExistingDevice({
609 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500610 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
611 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800612 break;
François Gaffie2110e042015-03-24 08:41:51 +0100613 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700614 device = availableDevices.getDevice(
615 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100616 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800617 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700618 device = availableDevices.getDevice(
619 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100620 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800621 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700622 device = availableDevices.getDevice(
623 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800624 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800625 case AUDIO_SOURCE_ULTRASOUND:
626 device = availableDevices.getFirstExistingDevice({
627 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
628 break;
François Gaffie2110e042015-03-24 08:41:51 +0100629 default:
630 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
631 break;
632 }
jiabinf502d152019-08-07 14:43:33 -0700633 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700634 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700635 device = availableDevices.getDevice(
636 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
637 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700638 "getDeviceForInputSource() no default device defined");
639 }
Eric Laurent2517af32020-11-25 15:31:27 +0100640
jiabinf502d152019-08-07 14:43:33 -0700641 ALOGV_IF(device != nullptr,
642 "getDeviceForInputSource()input source %d, device %08x",
643 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100644 return device;
645}
646
François Gaffiedc7553f2018-11-02 10:39:57 +0100647void Engine::updateDeviceSelectionCache()
648{
649 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700650 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100651 auto devices = getDevicesForProductStrategy(strategy->getId());
652 mDevicesForStrategies[strategy->getId()] = devices;
653 strategy->setDeviceTypes(devices.types());
654 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
655 }
656}
657
Eric Laurent2517af32020-11-25 15:31:27 +0100658product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
659 for (const auto& strategyMap : mLegacyStrategyMap) {
660 if (strategyMap.second == legacyStrategy) {
661 return strategyMap.first;
662 }
663 }
664 return PRODUCT_STRATEGY_NONE;
665}
François Gaffiedc7553f2018-11-02 10:39:57 +0100666
Eric Laurent2517af32020-11-25 15:31:27 +0100667audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
668 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
669 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
670 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
671 availableOutputDevices, strategy);
672 if (devices.size() > 0) {
673 return devices[0]->type();
674 }
675 return AUDIO_DEVICE_NONE;
676}
677
678DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
679 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
680 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700681 AudioDeviceTypeAddrVector preferredStrategyDevices;
682 const status_t status = getDevicesForRoleAndStrategy(
683 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700684 if (status == NO_ERROR) {
685 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100686 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700687 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000688 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700689 ALOGVV("%s using pref device %s for strategy %u",
690 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
691 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700692 }
693 }
Eric Laurent2517af32020-11-25 15:31:27 +0100694 return preferredAvailableDevVec;
695}
696
Eric Laurent6cd5f902021-03-23 18:29:58 +0100697
Eric Laurent2517af32020-11-25 15:31:27 +0100698DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100699 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
700
701 // Take context into account to remap product strategy before
702 // checking preferred device for strategy and applying default routing rules
703 strategy = remapStrategyFromContext(strategy, outputs);
704
Eric Laurent2517af32020-11-25 15:31:27 +0100705 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
706 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
707
Eric Laurent6cd5f902021-03-23 18:29:58 +0100708 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100709
Eric Laurent6cd5f902021-03-23 18:29:58 +0100710 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100711
Eric Laurent2517af32020-11-25 15:31:27 +0100712 // check if this strategy has a preferred device that is available,
713 // if yes, give priority to it.
714 DeviceVector preferredAvailableDevVec =
715 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
716 if (!preferredAvailableDevVec.isEmpty()) {
717 return preferredAvailableDevVec;
718 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700719
jiabinf502d152019-08-07 14:43:33 -0700720 return getDevicesForStrategyInt(legacyStrategy,
721 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100722 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100723}
724
725DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
726 const sp<DeviceDescriptor> &preferredDevice,
727 bool fromCache) const
728{
729 // First check for explict routing device
730 if (preferredDevice != nullptr) {
731 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
732 return DeviceVector(preferredDevice);
733 }
François Gaffiec005e562018-11-06 15:04:49 +0100734 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700735 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100736 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100737 //
738 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
739 // be by APM?
740 //
François Gaffiec005e562018-11-06 15:04:49 +0100741 // Honor explicit routing requests only if all active clients have a preferred route in which
742 // case the last active client route is used
743 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
744 if (device != nullptr) {
745 return DeviceVector(device);
746 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100747
748 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
749}
750
751DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
752{
753 auto attributes = getAttributesForStreamType(stream);
754 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
755}
756
757sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800758 uid_t uid,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800759 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100760{
François Gaffiec005e562018-11-06 15:04:49 +0100761 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700762 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100763 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100764 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100765
François Gaffiedc7553f2018-11-02 10:39:57 +0100766 //
François Gaffiec005e562018-11-06 15:04:49 +0100767 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
768 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100769 //
François Gaffiec005e562018-11-06 15:04:49 +0100770 // Honor explicit routing requests only if all active clients have a preferred route in which
771 // case the last active client route is used
772 sp<DeviceDescriptor> device =
773 findPreferredDevice(inputs, attr.source, availableInputDevices);
774 if (device != nullptr) {
775 return device;
776 }
777
yuanjiahsu0735bf32021-03-18 08:12:54 +0800778 device = policyMixes.getDeviceAndMixForInputSource(attr.source,
779 availableInputDevices,
780 uid,
781 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100782 if (device != nullptr) {
783 return device;
784 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100785
jiabinf502d152019-08-07 14:43:33 -0700786 device = getDeviceForInputSource(attr.source);
787 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
788 // Return immediately if the device is null or it is not a remote submix device.
789 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100790 }
jiabinf502d152019-08-07 14:43:33 -0700791
792 // For remote submix device, try to find the device by address.
793 address = "0";
794 std::size_t pos;
795 std::string tags { attr.tags };
796 if ((pos = tags.find("addr=")) != std::string::npos) {
797 address = tags.substr(pos + std::strlen("addr="));
798 }
799 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100800 String8(address.c_str()),
801 AUDIO_FORMAT_DEFAULT);
802}
803
François Gaffie2110e042015-03-24 08:41:51 +0100804} // namespace audio_policy
805} // namespace android
806
807