blob: e2f42da5b3d74de91c131d810de35fe79bd71b37 [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
Mikhail Naganov9e459d72023-05-05 17:36:39 -070038namespace android::audio_policy {
François Gaffie2110e042015-03-24 08:41:51 +010039
François Gaffiedc7553f2018-11-02 10:39:57 +010040struct legacy_strategy_map { const char *name; legacy_strategy id; };
Mikhail Naganovf9003622020-03-10 13:15:08 -070041static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
42 static const std::vector<legacy_strategy_map> legacyStrategy = {
43 { "STRATEGY_NONE", STRATEGY_NONE },
44 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
45 { "STRATEGY_PHONE", STRATEGY_PHONE },
46 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
47 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
48 { "STRATEGY_DTMF", STRATEGY_DTMF },
49 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
50 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
51 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
52 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
53 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
54 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
55 };
56 return legacyStrategy;
57}
François Gaffiedc7553f2018-11-02 10:39:57 +010058
Mikhail Naganov9e459d72023-05-05 17:36:39 -070059status_t Engine::loadFromHalConfigWithFallback(
60 const media::audio::common::AudioHalEngineConfig& aidlConfig) {
61 return loadWithFallback(aidlConfig);
62}
63
Mikhail Naganovabb04782023-05-02 13:56:01 -070064status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
Mikhail Naganov9e459d72023-05-05 17:36:39 -070065 return loadWithFallback(xmlFilePath);
66}
67
68template<typename T>
69status_t Engine::loadWithFallback(const T& configSource) {
70 auto result = EngineBase::loadAudioPolicyEngineConfig(configSource);
François Gaffiedc7553f2018-11-02 10:39:57 +010071 ALOGE_IF(result.nbSkippedElement != 0,
72 "Policy Engine configuration is partially invalid, skipped %zu elements",
73 result.nbSkippedElement);
74
Mikhail Naganovf9003622020-03-10 13:15:08 -070075 auto legacyStrategy = getLegacyStrategy();
76 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010077 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010078 }
Mikhail Naganovabb04782023-05-02 13:56:01 -070079
80 return OK;
François Gaffie2110e042015-03-24 08:41:51 +010081}
82
Mikhail Naganov9e459d72023-05-05 17:36:39 -070083
François Gaffie2110e042015-03-24 08:41:51 +010084status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
85{
86 switch(usage) {
87 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
88 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
89 config != AUDIO_POLICY_FORCE_NONE) {
90 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
91 return BAD_VALUE;
92 }
François Gaffie2110e042015-03-24 08:41:51 +010093 break;
94 case AUDIO_POLICY_FORCE_FOR_MEDIA:
95 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
96 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
97 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
98 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
99 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
100 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
101 return BAD_VALUE;
102 }
François Gaffie2110e042015-03-24 08:41:51 +0100103 break;
104 case AUDIO_POLICY_FORCE_FOR_RECORD:
105 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
106 config != AUDIO_POLICY_FORCE_NONE) {
107 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
108 return BAD_VALUE;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 break;
111 case AUDIO_POLICY_FORCE_FOR_DOCK:
112 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
113 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
114 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
115 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
116 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
117 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
118 }
François Gaffie2110e042015-03-24 08:41:51 +0100119 break;
120 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
121 if (config != AUDIO_POLICY_FORCE_NONE &&
122 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
123 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
124 }
François Gaffie2110e042015-03-24 08:41:51 +0100125 break;
126 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
127 if (config != AUDIO_POLICY_FORCE_NONE &&
128 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800129 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
130 }
Phil Burk09bc4612016-02-24 15:58:15 -0800131 break;
132 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
133 if (config != AUDIO_POLICY_FORCE_NONE &&
134 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700135 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
136 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800137 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
138 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100139 }
François Gaffie2110e042015-03-24 08:41:51 +0100140 break;
Jack He96117ae2018-02-12 20:52:53 -0800141 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
142 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
143 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
144 return BAD_VALUE;
145 }
Jack He96117ae2018-02-12 20:52:53 -0800146 break;
François Gaffie2110e042015-03-24 08:41:51 +0100147 default:
148 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800149 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100150 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100151 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100152}
153
Eric Laurent2802b862021-03-01 09:36:16 +0100154void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
155 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100156 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800157{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100158 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
159
François Gaffie2110e042015-03-24 08:41:51 +0100160 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100161 case STRATEGY_SONIFICATION_RESPECTFUL: {
162 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800163 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700164 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700165 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100166 }
Eric Laurent2802b862021-03-01 09:36:16 +0100167 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100168 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100169 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100170 // Force use of only devices on primary output if:
171 // - in call AND
172 // - cannot route from voice call RX OR
173 // - audio HAL version is < 3.0 and TX device is on the primary HW module
174 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700175 audio_devices_t txDevice = getDeviceForInputSource(
176 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100177 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700178 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700179 DeviceVector availPrimaryInputDevices =
180 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800181
182 // TODO: getPrimaryOutput return only devices from first module in
183 // audio_policy_configuration.xml, hearing aid is not there, but it's
184 // a primary device
185 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700186 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700187 primaryOutput->supportedDevices().types());
188 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700189 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100190
jiabinf502d152019-08-07 14:43:33 -0700191 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100192 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
193 ((availPrimaryInputDevices.getDevice(
194 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
195 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700196 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100197 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200198
Eric Laurent97cec6f2021-05-20 13:52:47 +0200199 }
200 // Do not use A2DP devices when in call but use them when not in call
201 // (e.g for voice mail playback)
202 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200203 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
204 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
205 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100206 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200207 // If connected to a dock, never use the device speaker for calls
208 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
209 .isEmpty()) {
210 availableOutputDevices.remove(
211 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
212 }
Eric Laurent2802b862021-03-01 09:36:16 +0100213 } break;
214 case STRATEGY_ACCESSIBILITY: {
215 // do not route accessibility prompts to a digital output currently configured with a
216 // compressed format as they would likely not be mixed and dropped.
217 for (size_t i = 0; i < outputs.size(); i++) {
218 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
219 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
220 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
221 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800222 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100223 }
224 }
225 } break;
226 default:
227 break;
228 }
229}
230
Eric Laurent6cd5f902021-03-23 18:29:58 +0100231product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
232 const SwAudioOutputCollection &outputs) const {
233 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
234 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
235
236 if (isInCall()) {
237 switch (legacyStrategy) {
238 case STRATEGY_ACCESSIBILITY:
239 case STRATEGY_DTMF:
240 case STRATEGY_MEDIA:
241 case STRATEGY_SONIFICATION:
242 case STRATEGY_SONIFICATION_RESPECTFUL:
243 legacyStrategy = STRATEGY_PHONE;
244 break;
245
246 default:
247 return strategy;
248 }
249 } else {
250 switch (legacyStrategy) {
251 case STRATEGY_SONIFICATION_RESPECTFUL:
252 case STRATEGY_SONIFICATION:
253 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
254 legacyStrategy = STRATEGY_PHONE;
255 }
256 break;
257
258 case STRATEGY_ACCESSIBILITY:
259 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
260 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
261 legacyStrategy = STRATEGY_SONIFICATION;
262 }
263 break;
264
265 default:
266 return strategy;
267 }
268 }
269 return getProductStrategyFromLegacy(legacyStrategy);
270}
271
Eric Laurent2802b862021-03-01 09:36:16 +0100272DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
273 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100274 const SwAudioOutputCollection &outputs) const
275{
276 DeviceVector devices;
277
278 switch (strategy) {
279
280 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
281 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
282 break;
283
Eric Laurent2802b862021-03-01 09:36:16 +0100284 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100285 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
286 if (!devices.isEmpty()) break;
Eric Laurentc0697592021-05-10 11:45:22 +0200287 devices = availableOutputDevices.getFirstDevicesFromTypes(
Eric Laurent34b09d02022-03-24 10:41:36 +0100288 getLastRemovableMediaDevices(GROUP_NONE, {AUDIO_DEVICE_OUT_BLE_HEADSET}));
Eric Laurent2517af32020-11-25 15:31:27 +0100289 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100290 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin76e829d2023-04-04 21:02:36 +0000291 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
292 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100293 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100294
295 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100296 case STRATEGY_ENFORCED_AUDIBLE:
297 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
298 // except:
299 // - when in call where it doesn't default to STRATEGY_PHONE behavior
300 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
301
302 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100303 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700304 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100305 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800306
307 // if SCO headset is connected and we are told to use it, play ringtone over
308 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700309 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700310 DeviceVector devices2;
311 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
312 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
313 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800314 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100315 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800316 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100317 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800318 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700319 if (!devices2.isEmpty()) {
320 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800321 break;
322 }
323 }
324 }
325 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100326 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
327 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700328 if (strategy == STRATEGY_SONIFICATION) {
329 devices.replaceDevicesByType(
330 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700331 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700332 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800333 }
jiabinf502d152019-08-07 14:43:33 -0700334 if (!devices2.isEmpty()) {
335 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800336 break;
337 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800338 }
339 }
François Gaffie2110e042015-03-24 08:41:51 +0100340 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700341 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100342
Eric Laurent6cd5f902021-03-23 18:29:58 +0100343 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100344 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100345 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100346 case STRATEGY_REROUTING:
347 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700348 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100349 if (strategy != STRATEGY_SONIFICATION) {
350 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700351 sp<DeviceDescriptor> remoteSubmix;
352 if ((remoteSubmix = availableOutputDevices.getDevice(
353 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
354 AUDIO_FORMAT_DEFAULT)) != nullptr) {
355 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100356 }
357 }
Eric Laurenta9986862020-10-07 08:42:28 -0700358
jiabinf502d152019-08-07 14:43:33 -0700359 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100360 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700361 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100362 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100363
364 // LE audio broadcast device is only used if:
365 // - No call is active
366 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
367 // OR the LE audio unicast device is not active
368 if (devices2.isEmpty() && !isInCall()
369 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
370 legacy_strategy topActiveStrategy = STRATEGY_NONE;
371 for (const auto &ps : getOrderedProductStrategies()) {
372 if (outputs.isStrategyActive(ps)) {
373 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
374 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
375 break;
376 }
377 }
378
379 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
380 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
381 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
382 devices2 =
383 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
384 }
385 }
386
Baekgyeong Kim08451522019-11-01 20:40:02 +0900387 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700388 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900389 // Get the last connected device of wired and bluetooth a2dp
390 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
391 getLastRemovableMediaDevices());
392 } else {
393 // Get the last connected device of wired except bluetooth a2dp
394 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
395 getLastRemovableMediaDevices(GROUP_WIRED));
396 }
François Gaffie2110e042015-03-24 08:41:51 +0100397 }
jiabinf502d152019-08-07 14:43:33 -0700398 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100399 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700400 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100401 }
jiabinf502d152019-08-07 14:43:33 -0700402 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100403 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700404 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700405 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100406 }
jiabinf502d152019-08-07 14:43:33 -0700407 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100408 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
409 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100410 }
jiabinf502d152019-08-07 14:43:33 -0700411 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100412 if (strategy == STRATEGY_MEDIA) {
413 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700414 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800415 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
416 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
417 });
François Gaffie2110e042015-03-24 08:41:51 +0100418 }
419
jiabinf502d152019-08-07 14:43:33 -0700420 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100421 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
422 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700423 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100424
425 // If hdmi system audio mode is on, remove speaker out of output list.
426 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100427 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100428 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700429 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100430 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700431
Eric Laurent6cd5f902021-03-23 18:29:58 +0100432 bool mediaActiveLocally =
433 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
434 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
435 || outputs.isActiveLocally(
436 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
437 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800438
439 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
440 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700441 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100442 // - for STRATEGY_SONIFICATION_RESPECTFUL:
443 // if no media is playing on the device, check for mandatory use of "safe" speaker
444 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800445 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100446 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700447 devices.replaceDevicesByType(
448 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700449 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700450 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700451 }
François Gaffie2110e042015-03-24 08:41:51 +0100452 } break;
453
Eric Laurent21777f82019-12-06 18:12:06 -0800454 case STRATEGY_CALL_ASSISTANT:
455 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
456 break;
457
Eric Laurentd9766932020-08-07 14:01:22 -0700458 case STRATEGY_NONE:
459 // Happens when internal strategies are processed ("rerouting", "patch"...)
460 break;
461
François Gaffie2110e042015-03-24 08:41:51 +0100462 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700463 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100464 break;
465 }
466
jiabinf502d152019-08-07 14:43:33 -0700467 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700468 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700469 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
470 if (defaultOutputDevice != nullptr) {
471 devices.add(defaultOutputDevice);
472 }
473 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700474 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700475 }
jiabinf502d152019-08-07 14:43:33 -0700476
Eric Laurentd9766932020-08-07 14:01:22 -0700477 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800478 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700479 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100480}
481
482
jiabinf502d152019-08-07 14:43:33 -0700483sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100484{
Eric Laurentaf377772019-03-29 14:50:21 -0700485 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
486 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100487 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700488 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700489 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700490 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
491 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700492 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100493
Eric Laurentdc95a252018-04-12 12:46:56 -0700494 // when a call is active, force device selection to match source VOICE_COMMUNICATION
495 // for most other input sources to avoid rerouting call TX audio
496 if (isInCall()) {
497 switch (inputSource) {
498 case AUDIO_SOURCE_DEFAULT:
499 case AUDIO_SOURCE_MIC:
500 case AUDIO_SOURCE_VOICE_RECOGNITION:
501 case AUDIO_SOURCE_UNPROCESSED:
502 case AUDIO_SOURCE_HOTWORD:
503 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800504 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800505 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700506 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
507 break;
508 default:
509 break;
510 }
511 }
512
Eric Laurent2517af32020-11-25 15:31:27 +0100513 audio_devices_t commDeviceType =
514 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
515
François Gaffie2110e042015-03-24 08:41:51 +0100516 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100517 case AUDIO_SOURCE_DEFAULT:
518 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700519 device = availableDevices.getDevice(
520 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
521 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100522 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700523 device = availableDevices.getDevice(
524 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
525 if (device != nullptr) break;
526 }
527 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200528 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700529 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500530 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700531 break;
François Gaffie2110e042015-03-24 08:41:51 +0100532
533 case AUDIO_SOURCE_VOICE_COMMUNICATION:
534 // Allow only use of devices on primary input if in call and HAL does not support routing
535 // to voice call path.
536 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700537 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
538 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700539 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700540 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100541 }
542
Eric Laurent2517af32020-11-25 15:31:27 +0100543 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100544 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700545 device = availableDevices.getDevice(
546 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
547 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100548 break;
549 }
Eric Laurent2517af32020-11-25 15:31:27 +0100550 }
551 switch (commDeviceType) {
552 case AUDIO_DEVICE_OUT_BLE_HEADSET:
553 device = availableDevices.getDevice(
Grzegorz Kołodziejczyk2e4e3e62021-07-21 15:35:03 +0200554 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100555 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100556 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700557 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100558 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
559 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100560 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100561 default: // FORCE_NONE
562 device = availableDevices.getFirstExistingDevice({
563 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500564 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
565 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100566 break;
567
François Gaffie2110e042015-03-24 08:41:51 +0100568 }
569 break;
570
571 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800572 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500573 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
574 device = availableDevices.getDevice(
575 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
576 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700577 }
wei wanga7020522020-12-10 21:36:11 -0500578 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
579 // because sometimes user want to do voice search by bt remote
580 // even if BUILDIN_MIC is available.
581 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200582 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500583 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
584 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
585
586 break;
587 case AUDIO_SOURCE_HOTWORD:
588 // We should not use primary output criteria for Hotword but rather limit
589 // to devices attached to the same HW module as the build in mic
590 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
591 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100592 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700593 device = availableDevices.getDevice(
594 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
595 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100596 }
jiabinf502d152019-08-07 14:43:33 -0700597 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200598 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700599 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
600 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100601 break;
602 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700603 // For a device without built-in mic, adding usb device
604 device = availableDevices.getFirstExistingDevice({
605 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
606 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100607 break;
608 case AUDIO_SOURCE_VOICE_DOWNLINK:
609 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700610 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700611 device = availableDevices.getDevice(
612 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100613 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800614 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700615 device = availableDevices.getFirstExistingDevice({
616 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500617 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
618 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800619 break;
François Gaffie2110e042015-03-24 08:41:51 +0100620 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700621 device = availableDevices.getDevice(
622 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100623 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800624 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700625 device = availableDevices.getDevice(
626 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100627 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800628 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700629 device = availableDevices.getDevice(
630 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800631 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800632 case AUDIO_SOURCE_ULTRASOUND:
633 device = availableDevices.getFirstExistingDevice({
634 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
635 break;
François Gaffie2110e042015-03-24 08:41:51 +0100636 default:
637 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
638 break;
639 }
jiabinf502d152019-08-07 14:43:33 -0700640 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700641 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700642 device = availableDevices.getDevice(
643 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
644 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700645 "getDeviceForInputSource() no default device defined");
646 }
Eric Laurent2517af32020-11-25 15:31:27 +0100647
jiabinf502d152019-08-07 14:43:33 -0700648 ALOGV_IF(device != nullptr,
649 "getDeviceForInputSource()input source %d, device %08x",
650 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100651 return device;
652}
653
jiabin76e829d2023-04-04 21:02:36 +0000654void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
655 strategy->setDeviceTypes(devices.types());
656 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100657}
658
Eric Laurent2517af32020-11-25 15:31:27 +0100659product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
660 for (const auto& strategyMap : mLegacyStrategyMap) {
661 if (strategyMap.second == legacyStrategy) {
662 return strategyMap.first;
663 }
664 }
665 return PRODUCT_STRATEGY_NONE;
666}
François Gaffiedc7553f2018-11-02 10:39:57 +0100667
Eric Laurent2517af32020-11-25 15:31:27 +0100668audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
669 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
670 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
671 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
672 availableOutputDevices, strategy);
673 if (devices.size() > 0) {
674 return devices[0]->type();
675 }
676 return AUDIO_DEVICE_NONE;
677}
678
679DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
680 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
681 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700682 AudioDeviceTypeAddrVector preferredStrategyDevices;
683 const status_t status = getDevicesForRoleAndStrategy(
684 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700685 if (status == NO_ERROR) {
686 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100687 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700688 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000689 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700690 ALOGVV("%s using pref device %s for strategy %u",
691 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
692 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700693 }
694 }
Eric Laurent2517af32020-11-25 15:31:27 +0100695 return preferredAvailableDevVec;
696}
697
Eric Laurent6cd5f902021-03-23 18:29:58 +0100698
Eric Laurent2517af32020-11-25 15:31:27 +0100699DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100700 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
701
702 // Take context into account to remap product strategy before
703 // checking preferred device for strategy and applying default routing rules
704 strategy = remapStrategyFromContext(strategy, outputs);
705
Eric Laurent2517af32020-11-25 15:31:27 +0100706 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
707 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
708
Eric Laurent6cd5f902021-03-23 18:29:58 +0100709 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100710
Eric Laurent6cd5f902021-03-23 18:29:58 +0100711 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100712
Eric Laurent2517af32020-11-25 15:31:27 +0100713 // check if this strategy has a preferred device that is available,
714 // if yes, give priority to it.
715 DeviceVector preferredAvailableDevVec =
716 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
717 if (!preferredAvailableDevVec.isEmpty()) {
718 return preferredAvailableDevVec;
719 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700720
jiabinf502d152019-08-07 14:43:33 -0700721 return getDevicesForStrategyInt(legacyStrategy,
722 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100723 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100724}
725
726DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
727 const sp<DeviceDescriptor> &preferredDevice,
728 bool fromCache) const
729{
730 // First check for explict routing device
731 if (preferredDevice != nullptr) {
732 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
733 return DeviceVector(preferredDevice);
734 }
François Gaffiec005e562018-11-06 15:04:49 +0100735 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700736 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100737 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100738 //
739 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
740 // be by APM?
741 //
François Gaffiec005e562018-11-06 15:04:49 +0100742 // Honor explicit routing requests only if all active clients have a preferred route in which
743 // case the last active client route is used
744 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
745 if (device != nullptr) {
746 return DeviceVector(device);
747 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100748
749 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
750}
751
752DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
753{
754 auto attributes = getAttributesForStreamType(stream);
755 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
756}
757
758sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800759 uid_t uid,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800760 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100761{
François Gaffiec005e562018-11-06 15:04:49 +0100762 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700763 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100764 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100765 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100766
François Gaffiedc7553f2018-11-02 10:39:57 +0100767 //
François Gaffiec005e562018-11-06 15:04:49 +0100768 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
769 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100770 //
François Gaffiec005e562018-11-06 15:04:49 +0100771 // Honor explicit routing requests only if all active clients have a preferred route in which
772 // case the last active client route is used
773 sp<DeviceDescriptor> device =
774 findPreferredDevice(inputs, attr.source, availableInputDevices);
775 if (device != nullptr) {
776 return device;
777 }
778
Jan Sebechlebskya7ed5272022-09-15 17:57:42 +0200779 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800780 availableInputDevices,
781 uid,
782 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100783 if (device != nullptr) {
784 return device;
785 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100786
jiabinf502d152019-08-07 14:43:33 -0700787 device = getDeviceForInputSource(attr.source);
788 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
789 // Return immediately if the device is null or it is not a remote submix device.
790 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100791 }
jiabinf502d152019-08-07 14:43:33 -0700792
793 // For remote submix device, try to find the device by address.
794 address = "0";
795 std::size_t pos;
796 std::string tags { attr.tags };
797 if ((pos = tags.find("addr=")) != std::string::npos) {
798 address = tags.substr(pos + std::strlen("addr="));
799 }
800 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100801 String8(address.c_str()),
802 AUDIO_FORMAT_DEFAULT);
803}
804
Mikhail Naganov9e459d72023-05-05 17:36:39 -0700805} // namespace android::audio_policy