blob: 0fa0e0e426eef86801ebae9af49745b7245aeda8 [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
Mikhail Naganovabb04782023-05-02 13:56:01 -070062status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
63 auto result = EngineBase::loadAudioPolicyEngineConfig(xmlFilePath);
François Gaffiedc7553f2018-11-02 10:39:57 +010064 ALOGE_IF(result.nbSkippedElement != 0,
65 "Policy Engine configuration is partially invalid, skipped %zu elements",
66 result.nbSkippedElement);
67
Mikhail Naganovf9003622020-03-10 13:15:08 -070068 auto legacyStrategy = getLegacyStrategy();
69 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010070 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010071 }
Mikhail Naganovabb04782023-05-02 13:56:01 -070072
73 return OK;
François Gaffie2110e042015-03-24 08:41:51 +010074}
75
François Gaffie2110e042015-03-24 08:41:51 +010076status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
77{
78 switch(usage) {
79 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
80 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
81 config != AUDIO_POLICY_FORCE_NONE) {
82 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
83 return BAD_VALUE;
84 }
François Gaffie2110e042015-03-24 08:41:51 +010085 break;
86 case AUDIO_POLICY_FORCE_FOR_MEDIA:
87 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
88 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
89 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
90 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
91 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
92 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
93 return BAD_VALUE;
94 }
François Gaffie2110e042015-03-24 08:41:51 +010095 break;
96 case AUDIO_POLICY_FORCE_FOR_RECORD:
97 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
98 config != AUDIO_POLICY_FORCE_NONE) {
99 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
100 return BAD_VALUE;
101 }
François Gaffie2110e042015-03-24 08:41:51 +0100102 break;
103 case AUDIO_POLICY_FORCE_FOR_DOCK:
104 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
105 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
106 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
107 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
108 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
109 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
110 }
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) {
115 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
116 }
François Gaffie2110e042015-03-24 08:41:51 +0100117 break;
118 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
119 if (config != AUDIO_POLICY_FORCE_NONE &&
120 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800121 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
122 }
Phil Burk09bc4612016-02-24 15:58:15 -0800123 break;
124 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
125 if (config != AUDIO_POLICY_FORCE_NONE &&
126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700127 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
128 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800129 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
130 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100131 }
François Gaffie2110e042015-03-24 08:41:51 +0100132 break;
Jack He96117ae2018-02-12 20:52:53 -0800133 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
134 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
135 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
136 return BAD_VALUE;
137 }
Jack He96117ae2018-02-12 20:52:53 -0800138 break;
François Gaffie2110e042015-03-24 08:41:51 +0100139 default:
140 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800141 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100142 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100143 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100144}
145
Eric Laurent2802b862021-03-01 09:36:16 +0100146void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
147 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100148 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800149{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100150 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
151
François Gaffie2110e042015-03-24 08:41:51 +0100152 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100153 case STRATEGY_SONIFICATION_RESPECTFUL: {
154 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800155 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700156 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700157 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100158 }
Eric Laurent2802b862021-03-01 09:36:16 +0100159 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100160 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100161 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100162 // Force use of only devices on primary output if:
163 // - in call AND
164 // - cannot route from voice call RX OR
165 // - audio HAL version is < 3.0 and TX device is on the primary HW module
166 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700167 audio_devices_t txDevice = getDeviceForInputSource(
168 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100169 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700170 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700171 DeviceVector availPrimaryInputDevices =
172 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800173
174 // TODO: getPrimaryOutput return only devices from first module in
175 // audio_policy_configuration.xml, hearing aid is not there, but it's
176 // a primary device
177 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700178 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700179 primaryOutput->supportedDevices().types());
180 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700181 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100182
jiabinf502d152019-08-07 14:43:33 -0700183 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100184 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
185 ((availPrimaryInputDevices.getDevice(
186 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
187 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700188 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100189 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200190
Eric Laurent97cec6f2021-05-20 13:52:47 +0200191 }
192 // Do not use A2DP devices when in call but use them when not in call
193 // (e.g for voice mail playback)
194 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200195 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
196 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
197 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100198 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200199 // If connected to a dock, never use the device speaker for calls
200 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
201 .isEmpty()) {
202 availableOutputDevices.remove(
203 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
204 }
Eric Laurent2802b862021-03-01 09:36:16 +0100205 } break;
206 case STRATEGY_ACCESSIBILITY: {
207 // do not route accessibility prompts to a digital output currently configured with a
208 // compressed format as they would likely not be mixed and dropped.
209 for (size_t i = 0; i < outputs.size(); i++) {
210 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
211 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
212 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
213 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800214 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100215 }
216 }
217 } break;
218 default:
219 break;
220 }
221}
222
Eric Laurent6cd5f902021-03-23 18:29:58 +0100223product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
224 const SwAudioOutputCollection &outputs) const {
225 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
226 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
227
228 if (isInCall()) {
229 switch (legacyStrategy) {
230 case STRATEGY_ACCESSIBILITY:
231 case STRATEGY_DTMF:
232 case STRATEGY_MEDIA:
233 case STRATEGY_SONIFICATION:
234 case STRATEGY_SONIFICATION_RESPECTFUL:
235 legacyStrategy = STRATEGY_PHONE;
236 break;
237
238 default:
239 return strategy;
240 }
241 } else {
242 switch (legacyStrategy) {
243 case STRATEGY_SONIFICATION_RESPECTFUL:
244 case STRATEGY_SONIFICATION:
245 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
246 legacyStrategy = STRATEGY_PHONE;
247 }
248 break;
249
250 case STRATEGY_ACCESSIBILITY:
251 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
252 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
253 legacyStrategy = STRATEGY_SONIFICATION;
254 }
255 break;
256
257 default:
258 return strategy;
259 }
260 }
261 return getProductStrategyFromLegacy(legacyStrategy);
262}
263
Eric Laurent2802b862021-03-01 09:36:16 +0100264DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
265 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100266 const SwAudioOutputCollection &outputs) const
267{
268 DeviceVector devices;
269
270 switch (strategy) {
271
272 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
273 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
274 break;
275
Eric Laurent2802b862021-03-01 09:36:16 +0100276 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100277 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
278 if (!devices.isEmpty()) break;
Eric Laurentc0697592021-05-10 11:45:22 +0200279 devices = availableOutputDevices.getFirstDevicesFromTypes(
Eric Laurent34b09d02022-03-24 10:41:36 +0100280 getLastRemovableMediaDevices(GROUP_NONE, {AUDIO_DEVICE_OUT_BLE_HEADSET}));
Eric Laurent2517af32020-11-25 15:31:27 +0100281 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100282 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin76e829d2023-04-04 21:02:36 +0000283 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
284 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100285 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100286
287 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100288 case STRATEGY_ENFORCED_AUDIBLE:
289 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
290 // except:
291 // - when in call where it doesn't default to STRATEGY_PHONE behavior
292 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
293
294 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100295 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700296 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100297 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800298
299 // if SCO headset is connected and we are told to use it, play ringtone over
300 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700301 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700302 DeviceVector devices2;
303 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
304 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
305 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800306 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100307 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800308 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100309 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800310 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700311 if (!devices2.isEmpty()) {
312 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800313 break;
314 }
315 }
316 }
317 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100318 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
319 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700320 if (strategy == STRATEGY_SONIFICATION) {
321 devices.replaceDevicesByType(
322 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700323 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700324 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800325 }
jiabinf502d152019-08-07 14:43:33 -0700326 if (!devices2.isEmpty()) {
327 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800328 break;
329 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800330 }
331 }
François Gaffie2110e042015-03-24 08:41:51 +0100332 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700333 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100334
Eric Laurent6cd5f902021-03-23 18:29:58 +0100335 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100336 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100337 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100338 case STRATEGY_REROUTING:
339 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700340 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100341 if (strategy != STRATEGY_SONIFICATION) {
342 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700343 sp<DeviceDescriptor> remoteSubmix;
344 if ((remoteSubmix = availableOutputDevices.getDevice(
345 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
346 AUDIO_FORMAT_DEFAULT)) != nullptr) {
347 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100348 }
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 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100355
356 // LE audio broadcast device is only used if:
357 // - No call is active
358 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
359 // OR the LE audio unicast device is not active
360 if (devices2.isEmpty() && !isInCall()
361 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
362 legacy_strategy topActiveStrategy = STRATEGY_NONE;
363 for (const auto &ps : getOrderedProductStrategies()) {
364 if (outputs.isStrategyActive(ps)) {
365 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
366 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
367 break;
368 }
369 }
370
371 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
372 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
373 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
374 devices2 =
375 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
376 }
377 }
378
Baekgyeong Kim08451522019-11-01 20:40:02 +0900379 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700380 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900381 // Get the last connected device of wired and bluetooth a2dp
382 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
383 getLastRemovableMediaDevices());
384 } else {
385 // Get the last connected device of wired except bluetooth a2dp
386 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
387 getLastRemovableMediaDevices(GROUP_WIRED));
388 }
François Gaffie2110e042015-03-24 08:41:51 +0100389 }
jiabinf502d152019-08-07 14:43:33 -0700390 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100391 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700392 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100393 }
jiabinf502d152019-08-07 14:43:33 -0700394 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100395 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700396 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700397 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100398 }
jiabinf502d152019-08-07 14:43:33 -0700399 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100400 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
401 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100402 }
jiabinf502d152019-08-07 14:43:33 -0700403 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100404 if (strategy == STRATEGY_MEDIA) {
405 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700406 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800407 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
408 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
409 });
François Gaffie2110e042015-03-24 08:41:51 +0100410 }
411
jiabinf502d152019-08-07 14:43:33 -0700412 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100413 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
414 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700415 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100416
417 // If hdmi system audio mode is on, remove speaker out of output list.
418 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100419 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100420 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700421 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100422 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700423
Eric Laurent6cd5f902021-03-23 18:29:58 +0100424 bool mediaActiveLocally =
425 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
426 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
427 || outputs.isActiveLocally(
428 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
429 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800430
431 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
432 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700433 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100434 // - for STRATEGY_SONIFICATION_RESPECTFUL:
435 // if no media is playing on the device, check for mandatory use of "safe" speaker
436 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800437 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100438 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700439 devices.replaceDevicesByType(
440 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700441 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700442 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700443 }
François Gaffie2110e042015-03-24 08:41:51 +0100444 } break;
445
Eric Laurent21777f82019-12-06 18:12:06 -0800446 case STRATEGY_CALL_ASSISTANT:
447 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
448 break;
449
Eric Laurentd9766932020-08-07 14:01:22 -0700450 case STRATEGY_NONE:
451 // Happens when internal strategies are processed ("rerouting", "patch"...)
452 break;
453
François Gaffie2110e042015-03-24 08:41:51 +0100454 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700455 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100456 break;
457 }
458
jiabinf502d152019-08-07 14:43:33 -0700459 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700460 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700461 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
462 if (defaultOutputDevice != nullptr) {
463 devices.add(defaultOutputDevice);
464 }
465 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700466 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700467 }
jiabinf502d152019-08-07 14:43:33 -0700468
Eric Laurentd9766932020-08-07 14:01:22 -0700469 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800470 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700471 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100472}
473
474
jiabinf502d152019-08-07 14:43:33 -0700475sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100476{
Eric Laurentaf377772019-03-29 14:50:21 -0700477 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
478 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100479 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700480 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700481 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700482 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
483 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700484 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100485
Eric Laurentdc95a252018-04-12 12:46:56 -0700486 // when a call is active, force device selection to match source VOICE_COMMUNICATION
487 // for most other input sources to avoid rerouting call TX audio
488 if (isInCall()) {
489 switch (inputSource) {
490 case AUDIO_SOURCE_DEFAULT:
491 case AUDIO_SOURCE_MIC:
492 case AUDIO_SOURCE_VOICE_RECOGNITION:
493 case AUDIO_SOURCE_UNPROCESSED:
494 case AUDIO_SOURCE_HOTWORD:
495 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800496 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800497 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700498 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
499 break;
500 default:
501 break;
502 }
503 }
504
Eric Laurent2517af32020-11-25 15:31:27 +0100505 audio_devices_t commDeviceType =
506 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
507
François Gaffie2110e042015-03-24 08:41:51 +0100508 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100509 case AUDIO_SOURCE_DEFAULT:
510 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700511 device = availableDevices.getDevice(
512 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
513 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100514 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700515 device = availableDevices.getDevice(
516 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
517 if (device != nullptr) break;
518 }
519 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200520 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700521 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500522 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700523 break;
François Gaffie2110e042015-03-24 08:41:51 +0100524
525 case AUDIO_SOURCE_VOICE_COMMUNICATION:
526 // Allow only use of devices on primary input if in call and HAL does not support routing
527 // to voice call path.
528 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700529 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
530 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700531 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700532 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100533 }
534
Eric Laurent2517af32020-11-25 15:31:27 +0100535 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100536 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700537 device = availableDevices.getDevice(
538 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
539 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100540 break;
541 }
Eric Laurent2517af32020-11-25 15:31:27 +0100542 }
543 switch (commDeviceType) {
544 case AUDIO_DEVICE_OUT_BLE_HEADSET:
545 device = availableDevices.getDevice(
Grzegorz Kołodziejczyk2e4e3e62021-07-21 15:35:03 +0200546 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100547 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100548 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700549 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100550 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
551 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100552 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100553 default: // FORCE_NONE
554 device = availableDevices.getFirstExistingDevice({
555 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500556 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
557 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100558 break;
559
François Gaffie2110e042015-03-24 08:41:51 +0100560 }
561 break;
562
563 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800564 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500565 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
566 device = availableDevices.getDevice(
567 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
568 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700569 }
wei wanga7020522020-12-10 21:36:11 -0500570 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
571 // because sometimes user want to do voice search by bt remote
572 // even if BUILDIN_MIC is available.
573 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200574 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500575 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
576 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
577
578 break;
579 case AUDIO_SOURCE_HOTWORD:
580 // We should not use primary output criteria for Hotword but rather limit
581 // to devices attached to the same HW module as the build in mic
582 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
583 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100584 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700585 device = availableDevices.getDevice(
586 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
587 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100588 }
jiabinf502d152019-08-07 14:43:33 -0700589 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200590 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700591 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
592 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100593 break;
594 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700595 // For a device without built-in mic, adding usb device
596 device = availableDevices.getFirstExistingDevice({
597 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
598 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100599 break;
600 case AUDIO_SOURCE_VOICE_DOWNLINK:
601 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700602 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700603 device = availableDevices.getDevice(
604 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100605 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800606 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700607 device = availableDevices.getFirstExistingDevice({
608 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500609 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
610 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800611 break;
François Gaffie2110e042015-03-24 08:41:51 +0100612 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700613 device = availableDevices.getDevice(
614 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100615 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800616 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700617 device = availableDevices.getDevice(
618 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100619 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800620 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700621 device = availableDevices.getDevice(
622 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800623 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800624 case AUDIO_SOURCE_ULTRASOUND:
625 device = availableDevices.getFirstExistingDevice({
626 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
627 break;
François Gaffie2110e042015-03-24 08:41:51 +0100628 default:
629 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
630 break;
631 }
jiabinf502d152019-08-07 14:43:33 -0700632 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700633 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700634 device = availableDevices.getDevice(
635 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
636 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700637 "getDeviceForInputSource() no default device defined");
638 }
Eric Laurent2517af32020-11-25 15:31:27 +0100639
jiabinf502d152019-08-07 14:43:33 -0700640 ALOGV_IF(device != nullptr,
641 "getDeviceForInputSource()input source %d, device %08x",
642 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100643 return device;
644}
645
jiabin76e829d2023-04-04 21:02:36 +0000646void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
647 strategy->setDeviceTypes(devices.types());
648 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100649}
650
Eric Laurent2517af32020-11-25 15:31:27 +0100651product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
652 for (const auto& strategyMap : mLegacyStrategyMap) {
653 if (strategyMap.second == legacyStrategy) {
654 return strategyMap.first;
655 }
656 }
657 return PRODUCT_STRATEGY_NONE;
658}
François Gaffiedc7553f2018-11-02 10:39:57 +0100659
Eric Laurent2517af32020-11-25 15:31:27 +0100660audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
661 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
662 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
663 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
664 availableOutputDevices, strategy);
665 if (devices.size() > 0) {
666 return devices[0]->type();
667 }
668 return AUDIO_DEVICE_NONE;
669}
670
671DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
672 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
673 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700674 AudioDeviceTypeAddrVector preferredStrategyDevices;
675 const status_t status = getDevicesForRoleAndStrategy(
676 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700677 if (status == NO_ERROR) {
678 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100679 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700680 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000681 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700682 ALOGVV("%s using pref device %s for strategy %u",
683 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
684 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700685 }
686 }
Eric Laurent2517af32020-11-25 15:31:27 +0100687 return preferredAvailableDevVec;
688}
689
Eric Laurent6cd5f902021-03-23 18:29:58 +0100690
Eric Laurent2517af32020-11-25 15:31:27 +0100691DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100692 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
693
694 // Take context into account to remap product strategy before
695 // checking preferred device for strategy and applying default routing rules
696 strategy = remapStrategyFromContext(strategy, outputs);
697
Eric Laurent2517af32020-11-25 15:31:27 +0100698 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
699 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
700
Eric Laurent6cd5f902021-03-23 18:29:58 +0100701 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100702
Eric Laurent6cd5f902021-03-23 18:29:58 +0100703 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100704
Eric Laurent2517af32020-11-25 15:31:27 +0100705 // check if this strategy has a preferred device that is available,
706 // if yes, give priority to it.
707 DeviceVector preferredAvailableDevVec =
708 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
709 if (!preferredAvailableDevVec.isEmpty()) {
710 return preferredAvailableDevVec;
711 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700712
jiabinf502d152019-08-07 14:43:33 -0700713 return getDevicesForStrategyInt(legacyStrategy,
714 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100715 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100716}
717
718DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
719 const sp<DeviceDescriptor> &preferredDevice,
720 bool fromCache) const
721{
722 // First check for explict routing device
723 if (preferredDevice != nullptr) {
724 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
725 return DeviceVector(preferredDevice);
726 }
François Gaffiec005e562018-11-06 15:04:49 +0100727 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700728 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100729 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100730 //
731 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
732 // be by APM?
733 //
François Gaffiec005e562018-11-06 15:04:49 +0100734 // Honor explicit routing requests only if all active clients have a preferred route in which
735 // case the last active client route is used
736 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
737 if (device != nullptr) {
738 return DeviceVector(device);
739 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100740
741 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
742}
743
744DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
745{
746 auto attributes = getAttributesForStreamType(stream);
747 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
748}
749
750sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800751 uid_t uid,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800752 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100753{
François Gaffiec005e562018-11-06 15:04:49 +0100754 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700755 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100756 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100757 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100758
François Gaffiedc7553f2018-11-02 10:39:57 +0100759 //
François Gaffiec005e562018-11-06 15:04:49 +0100760 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
761 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100762 //
François Gaffiec005e562018-11-06 15:04:49 +0100763 // Honor explicit routing requests only if all active clients have a preferred route in which
764 // case the last active client route is used
765 sp<DeviceDescriptor> device =
766 findPreferredDevice(inputs, attr.source, availableInputDevices);
767 if (device != nullptr) {
768 return device;
769 }
770
Jan Sebechlebskya7ed5272022-09-15 17:57:42 +0200771 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800772 availableInputDevices,
773 uid,
774 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100775 if (device != nullptr) {
776 return device;
777 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100778
jiabinf502d152019-08-07 14:43:33 -0700779 device = getDeviceForInputSource(attr.source);
780 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
781 // Return immediately if the device is null or it is not a remote submix device.
782 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100783 }
jiabinf502d152019-08-07 14:43:33 -0700784
785 // For remote submix device, try to find the device by address.
786 address = "0";
787 std::size_t pos;
788 std::string tags { attr.tags };
789 if ((pos = tags.find("addr=")) != std::string::npos) {
790 address = tags.substr(pos + std::strlen("addr="));
791 }
792 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100793 String8(address.c_str()),
794 AUDIO_FORMAT_DEFAULT);
795}
796
François Gaffie2110e042015-03-24 08:41:51 +0100797} // namespace audio_policy
798} // namespace android