blob: ea56486c9bd77a9d5bb8878727bdfc7b0a27c305 [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) {
Eric Laurentcedd5b52023-03-22 00:03:31 +0000169 audio_devices_t txDevice = AUDIO_DEVICE_NONE;
170 sp<DeviceDescriptor> txDeviceDesc =
171 getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
172 if (txDeviceDesc != nullptr) {
173 txDevice = txDeviceDesc->type();
174 }
François Gaffie2110e042015-03-24 08:41:51 +0100175 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700176 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700177 DeviceVector availPrimaryInputDevices =
178 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800179
180 // TODO: getPrimaryOutput return only devices from first module in
181 // audio_policy_configuration.xml, hearing aid is not there, but it's
182 // a primary device
183 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700184 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700185 primaryOutput->supportedDevices().types());
186 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700187 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100188
jiabinf502d152019-08-07 14:43:33 -0700189 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100190 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
191 ((availPrimaryInputDevices.getDevice(
192 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
193 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700194 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100195 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200196
Eric Laurent97cec6f2021-05-20 13:52:47 +0200197 }
198 // Do not use A2DP devices when in call but use them when not in call
199 // (e.g for voice mail playback)
200 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200201 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
202 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
203 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100204 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200205 // If connected to a dock, never use the device speaker for calls
206 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
207 .isEmpty()) {
208 availableOutputDevices.remove(
209 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
210 }
Eric Laurent2802b862021-03-01 09:36:16 +0100211 } break;
212 case STRATEGY_ACCESSIBILITY: {
213 // do not route accessibility prompts to a digital output currently configured with a
214 // compressed format as they would likely not be mixed and dropped.
215 for (size_t i = 0; i < outputs.size(); i++) {
216 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
217 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
218 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
219 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800220 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100221 }
222 }
223 } break;
224 default:
225 break;
226 }
227}
228
Eric Laurent6cd5f902021-03-23 18:29:58 +0100229product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
230 const SwAudioOutputCollection &outputs) const {
231 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
232 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
233
234 if (isInCall()) {
235 switch (legacyStrategy) {
236 case STRATEGY_ACCESSIBILITY:
237 case STRATEGY_DTMF:
238 case STRATEGY_MEDIA:
239 case STRATEGY_SONIFICATION:
240 case STRATEGY_SONIFICATION_RESPECTFUL:
241 legacyStrategy = STRATEGY_PHONE;
242 break;
243
244 default:
245 return strategy;
246 }
247 } else {
248 switch (legacyStrategy) {
249 case STRATEGY_SONIFICATION_RESPECTFUL:
250 case STRATEGY_SONIFICATION:
251 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
252 legacyStrategy = STRATEGY_PHONE;
253 }
254 break;
255
256 case STRATEGY_ACCESSIBILITY:
257 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
258 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
259 legacyStrategy = STRATEGY_SONIFICATION;
260 }
261 break;
262
263 default:
264 return strategy;
265 }
266 }
267 return getProductStrategyFromLegacy(legacyStrategy);
268}
269
Eric Laurent2802b862021-03-01 09:36:16 +0100270DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
271 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100272 const SwAudioOutputCollection &outputs) const
273{
274 DeviceVector devices;
275
276 switch (strategy) {
277
278 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
279 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
280 break;
281
Eric Laurent2802b862021-03-01 09:36:16 +0100282 case STRATEGY_PHONE: {
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000283 // TODO(b/243670205): remove this logic that gives preference to last removable devices
284 // once a UX decision has been made
Eric Laurentc0697592021-05-10 11:45:22 +0200285 devices = availableOutputDevices.getFirstDevicesFromTypes(
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000286 getLastRemovableMediaDevices(GROUP_NONE, {
287 // excluding HEARING_AID and BLE_HEADSET because Dialer uses
288 // setCommunicationDevice to select them explicitly
289 AUDIO_DEVICE_OUT_HEARING_AID,
290 AUDIO_DEVICE_OUT_BLE_HEADSET
291 }));
Eric Laurent2517af32020-11-25 15:31:27 +0100292 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100293 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin29230182023-04-04 21:02:36 +0000294 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
295 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100296 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100297
298 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100299 case STRATEGY_ENFORCED_AUDIBLE:
300 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
301 // except:
302 // - when in call where it doesn't default to STRATEGY_PHONE behavior
303 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
304
305 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100306 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Jean-Michel Trivi00afde12023-02-01 20:46:32 +0000307 // favor dock over speaker when available
308 devices = availableOutputDevices.getFirstDevicesFromTypes({
309 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100310 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800311
312 // if SCO headset is connected and we are told to use it, play ringtone over
313 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700314 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700315 DeviceVector devices2;
316 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
317 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
318 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800319 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100320 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800321 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100322 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800323 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700324 if (!devices2.isEmpty()) {
325 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800326 break;
327 }
328 }
329 }
330 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100331 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
332 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700333 if (strategy == STRATEGY_SONIFICATION) {
334 devices.replaceDevicesByType(
335 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700336 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700337 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800338 }
jiabinf502d152019-08-07 14:43:33 -0700339 if (!devices2.isEmpty()) {
340 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800341 break;
342 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800343 }
344 }
François Gaffie2110e042015-03-24 08:41:51 +0100345 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700346 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100347
Eric Laurent6cd5f902021-03-23 18:29:58 +0100348 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100349 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100350 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100351 case STRATEGY_REROUTING:
352 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700353 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100354 if (strategy != STRATEGY_SONIFICATION) {
355 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700356 sp<DeviceDescriptor> remoteSubmix;
357 if ((remoteSubmix = availableOutputDevices.getDevice(
358 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
359 AUDIO_FORMAT_DEFAULT)) != nullptr) {
360 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100361 }
362 }
Eric Laurenta9986862020-10-07 08:42:28 -0700363
jiabinf502d152019-08-07 14:43:33 -0700364 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100365 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700366 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100367 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100368
369 // LE audio broadcast device is only used if:
370 // - No call is active
371 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
372 // OR the LE audio unicast device is not active
373 if (devices2.isEmpty() && !isInCall()
374 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
375 legacy_strategy topActiveStrategy = STRATEGY_NONE;
376 for (const auto &ps : getOrderedProductStrategies()) {
377 if (outputs.isStrategyActive(ps)) {
378 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
379 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
380 break;
381 }
382 }
383
384 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
385 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
386 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
387 devices2 =
388 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
389 }
390 }
391
Baekgyeong Kim08451522019-11-01 20:40:02 +0900392 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700393 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900394 // Get the last connected device of wired and bluetooth a2dp
395 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
396 getLastRemovableMediaDevices());
397 } else {
398 // Get the last connected device of wired except bluetooth a2dp
399 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
400 getLastRemovableMediaDevices(GROUP_WIRED));
401 }
François Gaffie2110e042015-03-24 08:41:51 +0100402 }
jiabinf502d152019-08-07 14:43:33 -0700403 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100404 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700405 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100406 }
jiabinf502d152019-08-07 14:43:33 -0700407 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100408 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700409 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700410 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100411 }
jiabinf502d152019-08-07 14:43:33 -0700412 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100413 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
414 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100415 }
jiabinf502d152019-08-07 14:43:33 -0700416 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100417 if (strategy == STRATEGY_MEDIA) {
418 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700419 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800420 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
421 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
422 });
François Gaffie2110e042015-03-24 08:41:51 +0100423 }
424
jiabinf502d152019-08-07 14:43:33 -0700425 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100426 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
427 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700428 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100429
430 // If hdmi system audio mode is on, remove speaker out of output list.
431 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100432 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100433 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700434 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100435 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700436
Eric Laurent6cd5f902021-03-23 18:29:58 +0100437 bool mediaActiveLocally =
438 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
439 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
440 || outputs.isActiveLocally(
441 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
442 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800443
444 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
445 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700446 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100447 // - for STRATEGY_SONIFICATION_RESPECTFUL:
448 // if no media is playing on the device, check for mandatory use of "safe" speaker
449 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800450 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100451 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700452 devices.replaceDevicesByType(
453 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700454 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700455 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700456 }
François Gaffie2110e042015-03-24 08:41:51 +0100457 } break;
458
Eric Laurent21777f82019-12-06 18:12:06 -0800459 case STRATEGY_CALL_ASSISTANT:
460 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
461 break;
462
Eric Laurentd9766932020-08-07 14:01:22 -0700463 case STRATEGY_NONE:
464 // Happens when internal strategies are processed ("rerouting", "patch"...)
465 break;
466
François Gaffie2110e042015-03-24 08:41:51 +0100467 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700468 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100469 break;
470 }
471
jiabinf502d152019-08-07 14:43:33 -0700472 if (devices.isEmpty()) {
jiabind0a8d6f2022-09-19 20:32:01 +0000473 ALOGI("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700474 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
475 if (defaultOutputDevice != nullptr) {
476 devices.add(defaultOutputDevice);
477 }
478 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700479 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700480 }
jiabinf502d152019-08-07 14:43:33 -0700481
Eric Laurentd9766932020-08-07 14:01:22 -0700482 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800483 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700484 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100485}
486
jiabin14662b52023-03-06 21:35:29 +0000487DeviceVector Engine::getPreferredAvailableDevicesForInputSource(
488 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
489 DeviceVector preferredAvailableDevVec = {};
490 AudioDeviceTypeAddrVector preferredDevices;
491 const status_t status = getDevicesForRoleAndCapturePreset(
492 inputSource, DEVICE_ROLE_PREFERRED, preferredDevices);
493 if (status == NO_ERROR) {
494 // Only use preferred devices when they are all available.
495 preferredAvailableDevVec =
496 availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices);
497 if (preferredAvailableDevVec.size() == preferredDevices.size()) {
498 ALOGVV("%s using pref device %s for source %u",
499 __func__, preferredAvailableDevVec.toString().c_str(), inputSource);
500 return preferredAvailableDevVec;
501 }
502 }
503 return preferredAvailableDevVec;
504}
505
506DeviceVector Engine::getDisabledDevicesForInputSource(
507 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
508 DeviceVector disabledDevices = {};
509 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
510 const status_t status = getDevicesForRoleAndCapturePreset(
511 inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
512 if (status == NO_ERROR) {
513 disabledDevices =
514 availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
515 }
516 return disabledDevices;
517}
François Gaffie2110e042015-03-24 08:41:51 +0100518
jiabinf502d152019-08-07 14:43:33 -0700519sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100520{
Eric Laurentaf377772019-03-29 14:50:21 -0700521 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
522 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100523 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700524 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700525 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700526 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
527 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700528 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100529
Eric Laurentdc95a252018-04-12 12:46:56 -0700530 // when a call is active, force device selection to match source VOICE_COMMUNICATION
531 // for most other input sources to avoid rerouting call TX audio
532 if (isInCall()) {
533 switch (inputSource) {
534 case AUDIO_SOURCE_DEFAULT:
535 case AUDIO_SOURCE_MIC:
536 case AUDIO_SOURCE_VOICE_RECOGNITION:
537 case AUDIO_SOURCE_UNPROCESSED:
538 case AUDIO_SOURCE_HOTWORD:
539 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800540 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800541 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700542 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
543 break;
544 default:
545 break;
546 }
547 }
548
jiabin14662b52023-03-06 21:35:29 +0000549 // Use the preferred device for the input source if it is available.
550 DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource(
551 availableDevices, inputSource);
552 if (!preferredInputDevices.isEmpty()) {
553 // Currently, only support single device for input. The public JAVA API also only
554 // support setting single device as preferred device. In that case, returning the
555 // first device is OK here.
556 return preferredInputDevices[0];
557 }
558 // Remove the disabled device for the input source from the available input device list.
559 DeviceVector disabledInputDevices = getDisabledDevicesForInputSource(
560 availableDevices, inputSource);
561 availableDevices.remove(disabledInputDevices);
562
Eric Laurent2517af32020-11-25 15:31:27 +0100563 audio_devices_t commDeviceType =
564 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
565
François Gaffie2110e042015-03-24 08:41:51 +0100566 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100567 case AUDIO_SOURCE_DEFAULT:
568 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700569 device = availableDevices.getDevice(
570 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
571 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100572 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700573 device = availableDevices.getDevice(
574 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
575 if (device != nullptr) break;
576 }
577 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200578 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700579 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500580 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700581 break;
François Gaffie2110e042015-03-24 08:41:51 +0100582
583 case AUDIO_SOURCE_VOICE_COMMUNICATION:
584 // Allow only use of devices on primary input if in call and HAL does not support routing
585 // to voice call path.
586 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700587 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
588 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700589 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700590 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100591 }
592
Eric Laurent2517af32020-11-25 15:31:27 +0100593 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100594 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700595 device = availableDevices.getDevice(
596 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
597 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100598 break;
599 }
Eric Laurent2517af32020-11-25 15:31:27 +0100600 }
601 switch (commDeviceType) {
Eric Laurent2517af32020-11-25 15:31:27 +0100602 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700603 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100604 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
605 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100606 break;
Eric Laurentcedd5b52023-03-22 00:03:31 +0000607 case AUDIO_DEVICE_OUT_BLE_HEADSET:
608 device = availableDevices.getDevice(
609 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
610 if (device != nullptr) {
611 break;
612 }
613 ALOGE("%s LE Audio selected for communication but input device not available",
614 __func__);
615 FALLTHROUGH_INTENDED;
Eric Laurent2517af32020-11-25 15:31:27 +0100616 default: // FORCE_NONE
617 device = availableDevices.getFirstExistingDevice({
618 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500619 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
620 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100621 break;
François Gaffie2110e042015-03-24 08:41:51 +0100622 }
623 break;
624
625 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800626 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500627 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
628 device = availableDevices.getDevice(
629 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
630 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700631 }
wei wanga7020522020-12-10 21:36:11 -0500632 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
633 // because sometimes user want to do voice search by bt remote
634 // even if BUILDIN_MIC is available.
635 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200636 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500637 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
638 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
639
640 break;
641 case AUDIO_SOURCE_HOTWORD:
642 // We should not use primary output criteria for Hotword but rather limit
643 // to devices attached to the same HW module as the build in mic
644 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
645 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100646 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700647 device = availableDevices.getDevice(
648 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
649 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100650 }
jiabinf502d152019-08-07 14:43:33 -0700651 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200652 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700653 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
654 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100655 break;
656 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700657 // For a device without built-in mic, adding usb device
658 device = availableDevices.getFirstExistingDevice({
659 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
660 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100661 break;
662 case AUDIO_SOURCE_VOICE_DOWNLINK:
663 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700664 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700665 device = availableDevices.getDevice(
666 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100667 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800668 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700669 device = availableDevices.getFirstExistingDevice({
670 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500671 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
672 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800673 break;
François Gaffie2110e042015-03-24 08:41:51 +0100674 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700675 device = availableDevices.getDevice(
676 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100677 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800678 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700679 device = availableDevices.getDevice(
680 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100681 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800682 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700683 device = availableDevices.getDevice(
684 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800685 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800686 case AUDIO_SOURCE_ULTRASOUND:
687 device = availableDevices.getFirstExistingDevice({
688 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
689 break;
François Gaffie2110e042015-03-24 08:41:51 +0100690 default:
691 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
692 break;
693 }
jiabinf502d152019-08-07 14:43:33 -0700694 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700695 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700696 device = availableDevices.getDevice(
697 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
698 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700699 "getDeviceForInputSource() no default device defined");
700 }
Eric Laurent2517af32020-11-25 15:31:27 +0100701
jiabinf502d152019-08-07 14:43:33 -0700702 ALOGV_IF(device != nullptr,
703 "getDeviceForInputSource()input source %d, device %08x",
704 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100705 return device;
706}
707
jiabin29230182023-04-04 21:02:36 +0000708void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
709 strategy->setDeviceTypes(devices.types());
710 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100711}
712
Eric Laurent2517af32020-11-25 15:31:27 +0100713product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
714 for (const auto& strategyMap : mLegacyStrategyMap) {
715 if (strategyMap.second == legacyStrategy) {
716 return strategyMap.first;
717 }
718 }
719 return PRODUCT_STRATEGY_NONE;
720}
François Gaffiedc7553f2018-11-02 10:39:57 +0100721
Eric Laurent2517af32020-11-25 15:31:27 +0100722audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
723 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
724 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
725 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
726 availableOutputDevices, strategy);
727 if (devices.size() > 0) {
728 return devices[0]->type();
729 }
730 return AUDIO_DEVICE_NONE;
731}
732
733DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
734 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
735 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700736 AudioDeviceTypeAddrVector preferredStrategyDevices;
737 const status_t status = getDevicesForRoleAndStrategy(
738 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700739 if (status == NO_ERROR) {
740 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100741 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700742 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000743 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700744 ALOGVV("%s using pref device %s for strategy %u",
745 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
746 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700747 }
748 }
Eric Laurent2517af32020-11-25 15:31:27 +0100749 return preferredAvailableDevVec;
750}
751
jiabind0a8d6f2022-09-19 20:32:01 +0000752DeviceVector Engine::getDisabledDevicesForProductStrategy(
753 const DeviceVector &availableOutputDevices, product_strategy_t strategy) const {
754 DeviceVector disabledDevices = {};
755 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
756 const status_t status = getDevicesForRoleAndStrategy(
757 strategy, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
758 if (status == NO_ERROR) {
759 disabledDevices =
760 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
761 }
762 return disabledDevices;
763}
Eric Laurent6cd5f902021-03-23 18:29:58 +0100764
Eric Laurent2517af32020-11-25 15:31:27 +0100765DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100766 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
767
768 // Take context into account to remap product strategy before
769 // checking preferred device for strategy and applying default routing rules
770 strategy = remapStrategyFromContext(strategy, outputs);
771
Eric Laurent2517af32020-11-25 15:31:27 +0100772 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
773 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
774
Eric Laurent6cd5f902021-03-23 18:29:58 +0100775 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100776
Eric Laurent6cd5f902021-03-23 18:29:58 +0100777 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100778
Eric Laurent2517af32020-11-25 15:31:27 +0100779 // check if this strategy has a preferred device that is available,
780 // if yes, give priority to it.
781 DeviceVector preferredAvailableDevVec =
782 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
783 if (!preferredAvailableDevVec.isEmpty()) {
784 return preferredAvailableDevVec;
785 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700786
jiabind0a8d6f2022-09-19 20:32:01 +0000787 // Remove all disabled devices from the available device list.
788 DeviceVector disabledDevVec =
789 getDisabledDevicesForProductStrategy(availableOutputDevices, strategy);
790 availableOutputDevices.remove(disabledDevVec);
791
jiabinf502d152019-08-07 14:43:33 -0700792 return getDevicesForStrategyInt(legacyStrategy,
793 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100794 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100795}
796
797DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
798 const sp<DeviceDescriptor> &preferredDevice,
799 bool fromCache) const
800{
801 // First check for explict routing device
802 if (preferredDevice != nullptr) {
803 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
804 return DeviceVector(preferredDevice);
805 }
François Gaffiec005e562018-11-06 15:04:49 +0100806 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700807 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100808 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100809 //
810 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
811 // be by APM?
812 //
François Gaffiec005e562018-11-06 15:04:49 +0100813 // Honor explicit routing requests only if all active clients have a preferred route in which
814 // case the last active client route is used
815 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
816 if (device != nullptr) {
817 return DeviceVector(device);
818 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100819
820 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
821}
822
823DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
824{
825 auto attributes = getAttributesForStreamType(stream);
826 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
827}
828
829sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800830 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200831 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800832 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100833{
François Gaffiec005e562018-11-06 15:04:49 +0100834 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700835 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100836 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100837 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100838
François Gaffiedc7553f2018-11-02 10:39:57 +0100839 //
François Gaffiec005e562018-11-06 15:04:49 +0100840 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
841 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100842 //
François Gaffiec005e562018-11-06 15:04:49 +0100843 // Honor explicit routing requests only if all active clients have a preferred route in which
844 // case the last active client route is used
845 sp<DeviceDescriptor> device =
846 findPreferredDevice(inputs, attr.source, availableInputDevices);
847 if (device != nullptr) {
848 return device;
849 }
850
Jan Sebechlebsky28ed9452022-09-15 17:57:42 +0200851 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800852 availableInputDevices,
853 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200854 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800855 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100856 if (device != nullptr) {
857 return device;
858 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100859
jiabinf502d152019-08-07 14:43:33 -0700860 device = getDeviceForInputSource(attr.source);
861 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
862 // Return immediately if the device is null or it is not a remote submix device.
863 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100864 }
jiabinf502d152019-08-07 14:43:33 -0700865
866 // For remote submix device, try to find the device by address.
867 address = "0";
868 std::size_t pos;
869 std::string tags { attr.tags };
870 if ((pos = tags.find("addr=")) != std::string::npos) {
871 address = tags.substr(pos + std::strlen("addr="));
872 }
873 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100874 String8(address.c_str()),
875 AUDIO_FORMAT_DEFAULT);
876}
877
François Gaffie2110e042015-03-24 08:41:51 +0100878} // namespace audio_policy
879} // namespace android
880
881