blob: c7e21036dbe2ac777a0de894c2827e583877d85e [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({
294 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE});
Eric Laurent2517af32020-11-25 15:31:27 +0100295 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100296
297 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100298 case STRATEGY_ENFORCED_AUDIBLE:
299 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
300 // except:
301 // - when in call where it doesn't default to STRATEGY_PHONE behavior
302 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
303
304 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100305 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Jean-Michel Trivi00afde12023-02-01 20:46:32 +0000306 // favor dock over speaker when available
307 devices = availableOutputDevices.getFirstDevicesFromTypes({
308 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100309 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800310
311 // if SCO headset is connected and we are told to use it, play ringtone over
312 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700313 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700314 DeviceVector devices2;
315 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
316 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
317 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800318 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100319 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800320 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100321 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800322 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700323 if (!devices2.isEmpty()) {
324 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800325 break;
326 }
327 }
328 }
329 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100330 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
331 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700332 if (strategy == STRATEGY_SONIFICATION) {
333 devices.replaceDevicesByType(
334 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700335 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700336 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800337 }
jiabinf502d152019-08-07 14:43:33 -0700338 if (!devices2.isEmpty()) {
339 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800340 break;
341 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800342 }
343 }
François Gaffie2110e042015-03-24 08:41:51 +0100344 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700345 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100346
Eric Laurent6cd5f902021-03-23 18:29:58 +0100347 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100348 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100349 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100350 case STRATEGY_REROUTING:
351 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700352 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100353 if (strategy != STRATEGY_SONIFICATION) {
354 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700355 sp<DeviceDescriptor> remoteSubmix;
356 if ((remoteSubmix = availableOutputDevices.getDevice(
357 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
358 AUDIO_FORMAT_DEFAULT)) != nullptr) {
359 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100360 }
361 }
Eric Laurenta9986862020-10-07 08:42:28 -0700362
jiabinf502d152019-08-07 14:43:33 -0700363 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100364 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700365 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100366 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100367
368 // LE audio broadcast device is only used if:
369 // - No call is active
370 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
371 // OR the LE audio unicast device is not active
372 if (devices2.isEmpty() && !isInCall()
373 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
374 legacy_strategy topActiveStrategy = STRATEGY_NONE;
375 for (const auto &ps : getOrderedProductStrategies()) {
376 if (outputs.isStrategyActive(ps)) {
377 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
378 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
379 break;
380 }
381 }
382
383 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
384 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
385 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
386 devices2 =
387 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
388 }
389 }
390
Baekgyeong Kim08451522019-11-01 20:40:02 +0900391 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700392 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900393 // Get the last connected device of wired and bluetooth a2dp
394 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
395 getLastRemovableMediaDevices());
396 } else {
397 // Get the last connected device of wired except bluetooth a2dp
398 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
399 getLastRemovableMediaDevices(GROUP_WIRED));
400 }
François Gaffie2110e042015-03-24 08:41:51 +0100401 }
jiabinf502d152019-08-07 14:43:33 -0700402 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100403 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700404 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100405 }
jiabinf502d152019-08-07 14:43:33 -0700406 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100407 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700408 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700409 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100410 }
jiabinf502d152019-08-07 14:43:33 -0700411 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100412 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
413 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100414 }
jiabinf502d152019-08-07 14:43:33 -0700415 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100416 if (strategy == STRATEGY_MEDIA) {
417 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700418 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800419 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
420 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
421 });
François Gaffie2110e042015-03-24 08:41:51 +0100422 }
423
jiabinf502d152019-08-07 14:43:33 -0700424 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100425 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
426 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700427 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100428
429 // If hdmi system audio mode is on, remove speaker out of output list.
430 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100431 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100432 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700433 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100434 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700435
Eric Laurent6cd5f902021-03-23 18:29:58 +0100436 bool mediaActiveLocally =
437 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
438 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
439 || outputs.isActiveLocally(
440 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
441 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800442
443 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
444 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700445 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100446 // - for STRATEGY_SONIFICATION_RESPECTFUL:
447 // if no media is playing on the device, check for mandatory use of "safe" speaker
448 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800449 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100450 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700451 devices.replaceDevicesByType(
452 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700453 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700454 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700455 }
François Gaffie2110e042015-03-24 08:41:51 +0100456 } break;
457
Eric Laurent21777f82019-12-06 18:12:06 -0800458 case STRATEGY_CALL_ASSISTANT:
459 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
460 break;
461
Eric Laurentd9766932020-08-07 14:01:22 -0700462 case STRATEGY_NONE:
463 // Happens when internal strategies are processed ("rerouting", "patch"...)
464 break;
465
François Gaffie2110e042015-03-24 08:41:51 +0100466 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700467 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100468 break;
469 }
470
jiabinf502d152019-08-07 14:43:33 -0700471 if (devices.isEmpty()) {
jiabind0a8d6f2022-09-19 20:32:01 +0000472 ALOGI("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700473 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
474 if (defaultOutputDevice != nullptr) {
475 devices.add(defaultOutputDevice);
476 }
477 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700478 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700479 }
jiabinf502d152019-08-07 14:43:33 -0700480
Eric Laurentd9766932020-08-07 14:01:22 -0700481 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800482 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700483 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100484}
485
jiabin14662b52023-03-06 21:35:29 +0000486DeviceVector Engine::getPreferredAvailableDevicesForInputSource(
487 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
488 DeviceVector preferredAvailableDevVec = {};
489 AudioDeviceTypeAddrVector preferredDevices;
490 const status_t status = getDevicesForRoleAndCapturePreset(
491 inputSource, DEVICE_ROLE_PREFERRED, preferredDevices);
492 if (status == NO_ERROR) {
493 // Only use preferred devices when they are all available.
494 preferredAvailableDevVec =
495 availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices);
496 if (preferredAvailableDevVec.size() == preferredDevices.size()) {
497 ALOGVV("%s using pref device %s for source %u",
498 __func__, preferredAvailableDevVec.toString().c_str(), inputSource);
499 return preferredAvailableDevVec;
500 }
501 }
502 return preferredAvailableDevVec;
503}
504
505DeviceVector Engine::getDisabledDevicesForInputSource(
506 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
507 DeviceVector disabledDevices = {};
508 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
509 const status_t status = getDevicesForRoleAndCapturePreset(
510 inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
511 if (status == NO_ERROR) {
512 disabledDevices =
513 availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
514 }
515 return disabledDevices;
516}
François Gaffie2110e042015-03-24 08:41:51 +0100517
jiabinf502d152019-08-07 14:43:33 -0700518sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100519{
Eric Laurentaf377772019-03-29 14:50:21 -0700520 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
521 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100522 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700523 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700524 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700525 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
526 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700527 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100528
Eric Laurentdc95a252018-04-12 12:46:56 -0700529 // when a call is active, force device selection to match source VOICE_COMMUNICATION
530 // for most other input sources to avoid rerouting call TX audio
531 if (isInCall()) {
532 switch (inputSource) {
533 case AUDIO_SOURCE_DEFAULT:
534 case AUDIO_SOURCE_MIC:
535 case AUDIO_SOURCE_VOICE_RECOGNITION:
536 case AUDIO_SOURCE_UNPROCESSED:
537 case AUDIO_SOURCE_HOTWORD:
538 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800539 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800540 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700541 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
542 break;
543 default:
544 break;
545 }
546 }
547
jiabin14662b52023-03-06 21:35:29 +0000548 // Use the preferred device for the input source if it is available.
549 DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource(
550 availableDevices, inputSource);
551 if (!preferredInputDevices.isEmpty()) {
552 // Currently, only support single device for input. The public JAVA API also only
553 // support setting single device as preferred device. In that case, returning the
554 // first device is OK here.
555 return preferredInputDevices[0];
556 }
557 // Remove the disabled device for the input source from the available input device list.
558 DeviceVector disabledInputDevices = getDisabledDevicesForInputSource(
559 availableDevices, inputSource);
560 availableDevices.remove(disabledInputDevices);
561
Eric Laurent2517af32020-11-25 15:31:27 +0100562 audio_devices_t commDeviceType =
563 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
564
François Gaffie2110e042015-03-24 08:41:51 +0100565 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100566 case AUDIO_SOURCE_DEFAULT:
567 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700568 device = availableDevices.getDevice(
569 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
570 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100571 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700572 device = availableDevices.getDevice(
573 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
574 if (device != nullptr) break;
575 }
576 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200577 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700578 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500579 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700580 break;
François Gaffie2110e042015-03-24 08:41:51 +0100581
582 case AUDIO_SOURCE_VOICE_COMMUNICATION:
583 // Allow only use of devices on primary input if in call and HAL does not support routing
584 // to voice call path.
585 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700586 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
587 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700588 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700589 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100590 }
591
Eric Laurent2517af32020-11-25 15:31:27 +0100592 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100593 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700594 device = availableDevices.getDevice(
595 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
596 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100597 break;
598 }
Eric Laurent2517af32020-11-25 15:31:27 +0100599 }
600 switch (commDeviceType) {
Eric Laurent2517af32020-11-25 15:31:27 +0100601 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700602 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100603 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
604 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100605 break;
Eric Laurentcedd5b52023-03-22 00:03:31 +0000606 case AUDIO_DEVICE_OUT_BLE_HEADSET:
607 device = availableDevices.getDevice(
608 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
609 if (device != nullptr) {
610 break;
611 }
612 ALOGE("%s LE Audio selected for communication but input device not available",
613 __func__);
614 FALLTHROUGH_INTENDED;
Eric Laurent2517af32020-11-25 15:31:27 +0100615 default: // FORCE_NONE
616 device = availableDevices.getFirstExistingDevice({
617 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500618 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
619 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100620 break;
François Gaffie2110e042015-03-24 08:41:51 +0100621 }
622 break;
623
624 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800625 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500626 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
627 device = availableDevices.getDevice(
628 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
629 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700630 }
wei wanga7020522020-12-10 21:36:11 -0500631 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
632 // because sometimes user want to do voice search by bt remote
633 // even if BUILDIN_MIC is available.
634 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200635 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500636 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
637 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
638
639 break;
640 case AUDIO_SOURCE_HOTWORD:
641 // We should not use primary output criteria for Hotword but rather limit
642 // to devices attached to the same HW module as the build in mic
643 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
644 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100645 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700646 device = availableDevices.getDevice(
647 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
648 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100649 }
jiabinf502d152019-08-07 14:43:33 -0700650 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200651 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700652 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
653 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100654 break;
655 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700656 // For a device without built-in mic, adding usb device
657 device = availableDevices.getFirstExistingDevice({
658 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
659 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100660 break;
661 case AUDIO_SOURCE_VOICE_DOWNLINK:
662 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700663 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700664 device = availableDevices.getDevice(
665 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100666 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800667 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700668 device = availableDevices.getFirstExistingDevice({
669 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500670 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
671 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800672 break;
François Gaffie2110e042015-03-24 08:41:51 +0100673 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700674 device = availableDevices.getDevice(
675 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100676 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800677 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700678 device = availableDevices.getDevice(
679 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100680 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800681 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700682 device = availableDevices.getDevice(
683 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800684 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800685 case AUDIO_SOURCE_ULTRASOUND:
686 device = availableDevices.getFirstExistingDevice({
687 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
688 break;
François Gaffie2110e042015-03-24 08:41:51 +0100689 default:
690 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
691 break;
692 }
jiabinf502d152019-08-07 14:43:33 -0700693 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700694 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700695 device = availableDevices.getDevice(
696 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
697 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700698 "getDeviceForInputSource() no default device defined");
699 }
Eric Laurent2517af32020-11-25 15:31:27 +0100700
jiabinf502d152019-08-07 14:43:33 -0700701 ALOGV_IF(device != nullptr,
702 "getDeviceForInputSource()input source %d, device %08x",
703 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100704 return device;
705}
706
François Gaffiedc7553f2018-11-02 10:39:57 +0100707void Engine::updateDeviceSelectionCache()
708{
709 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700710 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100711 auto devices = getDevicesForProductStrategy(strategy->getId());
712 mDevicesForStrategies[strategy->getId()] = devices;
713 strategy->setDeviceTypes(devices.types());
714 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
715 }
716}
717
Eric Laurent2517af32020-11-25 15:31:27 +0100718product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
719 for (const auto& strategyMap : mLegacyStrategyMap) {
720 if (strategyMap.second == legacyStrategy) {
721 return strategyMap.first;
722 }
723 }
724 return PRODUCT_STRATEGY_NONE;
725}
François Gaffiedc7553f2018-11-02 10:39:57 +0100726
Eric Laurent2517af32020-11-25 15:31:27 +0100727audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
728 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
729 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
730 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
731 availableOutputDevices, strategy);
732 if (devices.size() > 0) {
733 return devices[0]->type();
734 }
735 return AUDIO_DEVICE_NONE;
736}
737
738DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
739 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
740 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700741 AudioDeviceTypeAddrVector preferredStrategyDevices;
742 const status_t status = getDevicesForRoleAndStrategy(
743 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700744 if (status == NO_ERROR) {
745 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100746 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700747 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000748 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700749 ALOGVV("%s using pref device %s for strategy %u",
750 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
751 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700752 }
753 }
Eric Laurent2517af32020-11-25 15:31:27 +0100754 return preferredAvailableDevVec;
755}
756
jiabind0a8d6f2022-09-19 20:32:01 +0000757DeviceVector Engine::getDisabledDevicesForProductStrategy(
758 const DeviceVector &availableOutputDevices, product_strategy_t strategy) const {
759 DeviceVector disabledDevices = {};
760 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
761 const status_t status = getDevicesForRoleAndStrategy(
762 strategy, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
763 if (status == NO_ERROR) {
764 disabledDevices =
765 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
766 }
767 return disabledDevices;
768}
Eric Laurent6cd5f902021-03-23 18:29:58 +0100769
Eric Laurent2517af32020-11-25 15:31:27 +0100770DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100771 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
772
773 // Take context into account to remap product strategy before
774 // checking preferred device for strategy and applying default routing rules
775 strategy = remapStrategyFromContext(strategy, outputs);
776
Eric Laurent2517af32020-11-25 15:31:27 +0100777 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
778 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
779
Eric Laurent6cd5f902021-03-23 18:29:58 +0100780 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100781
Eric Laurent6cd5f902021-03-23 18:29:58 +0100782 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100783
Eric Laurent2517af32020-11-25 15:31:27 +0100784 // check if this strategy has a preferred device that is available,
785 // if yes, give priority to it.
786 DeviceVector preferredAvailableDevVec =
787 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
788 if (!preferredAvailableDevVec.isEmpty()) {
789 return preferredAvailableDevVec;
790 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700791
jiabind0a8d6f2022-09-19 20:32:01 +0000792 // Remove all disabled devices from the available device list.
793 DeviceVector disabledDevVec =
794 getDisabledDevicesForProductStrategy(availableOutputDevices, strategy);
795 availableOutputDevices.remove(disabledDevVec);
796
jiabinf502d152019-08-07 14:43:33 -0700797 return getDevicesForStrategyInt(legacyStrategy,
798 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100799 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100800}
801
802DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
803 const sp<DeviceDescriptor> &preferredDevice,
804 bool fromCache) const
805{
806 // First check for explict routing device
807 if (preferredDevice != nullptr) {
808 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
809 return DeviceVector(preferredDevice);
810 }
François Gaffiec005e562018-11-06 15:04:49 +0100811 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700812 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100813 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100814 //
815 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
816 // be by APM?
817 //
François Gaffiec005e562018-11-06 15:04:49 +0100818 // Honor explicit routing requests only if all active clients have a preferred route in which
819 // case the last active client route is used
820 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
821 if (device != nullptr) {
822 return DeviceVector(device);
823 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100824
825 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
826}
827
828DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
829{
830 auto attributes = getAttributesForStreamType(stream);
831 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
832}
833
834sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800835 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200836 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800837 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100838{
François Gaffiec005e562018-11-06 15:04:49 +0100839 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700840 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100841 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100842 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100843
François Gaffiedc7553f2018-11-02 10:39:57 +0100844 //
François Gaffiec005e562018-11-06 15:04:49 +0100845 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
846 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100847 //
François Gaffiec005e562018-11-06 15:04:49 +0100848 // Honor explicit routing requests only if all active clients have a preferred route in which
849 // case the last active client route is used
850 sp<DeviceDescriptor> device =
851 findPreferredDevice(inputs, attr.source, availableInputDevices);
852 if (device != nullptr) {
853 return device;
854 }
855
Jan Sebechlebsky28ed9452022-09-15 17:57:42 +0200856 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800857 availableInputDevices,
858 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200859 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800860 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100861 if (device != nullptr) {
862 return device;
863 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100864
jiabinf502d152019-08-07 14:43:33 -0700865 device = getDeviceForInputSource(attr.source);
866 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
867 // Return immediately if the device is null or it is not a remote submix device.
868 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100869 }
jiabinf502d152019-08-07 14:43:33 -0700870
871 // For remote submix device, try to find the device by address.
872 address = "0";
873 std::size_t pos;
874 std::string tags { attr.tags };
875 if ((pos = tags.find("addr=")) != std::string::npos) {
876 address = tags.substr(pos + std::strlen("addr="));
877 }
878 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100879 String8(address.c_str()),
880 AUDIO_FORMAT_DEFAULT);
881}
882
François Gaffie2110e042015-03-24 08:41:51 +0100883} // namespace audio_policy
884} // namespace android
885
886