blob: b495f7259273565cb2b0acbdfa793f650a52594b [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>
Eric Laurentffff1f72024-06-12 19:08:15 +000033#include <com_android_media_audioserver.h>
François Gaffie2110e042015-03-24 08:41:51 +010034#include <policy.h>
jiabin9a3361e2019-10-01 09:38:30 -070035#include <media/AudioContainers.h>
François Gaffie2110e042015-03-24 08:41:51 +010036#include <utils/String8.h>
37#include <utils/Log.h>
38
Mikhail Naganov9e459d72023-05-05 17:36:39 -070039namespace android::audio_policy {
François Gaffie2110e042015-03-24 08:41:51 +010040
Mikhail Naganovf9003622020-03-10 13:15:08 -070041static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
Mikhail Naganovbada1f52024-12-03 16:20:10 -080042 static const std::vector<legacy_strategy_map> legacyStrategy = getLegacyStrategyMap();
Mikhail Naganovf9003622020-03-10 13:15:08 -070043 return legacyStrategy;
44}
François Gaffiedc7553f2018-11-02 10:39:57 +010045
Mikhail Naganov9e459d72023-05-05 17:36:39 -070046status_t Engine::loadFromHalConfigWithFallback(
47 const media::audio::common::AudioHalEngineConfig& aidlConfig) {
48 return loadWithFallback(aidlConfig);
49}
50
Mikhail Naganovabb04782023-05-02 13:56:01 -070051status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
Mikhail Naganov9e459d72023-05-05 17:36:39 -070052 return loadWithFallback(xmlFilePath);
53}
54
55template<typename T>
56status_t Engine::loadWithFallback(const T& configSource) {
Mikhail Naganovbada1f52024-12-03 16:20:10 -080057 auto result = EngineBase::loadAudioPolicyEngineConfig(configSource, false /*isConfigurable*/);
François Gaffiedc7553f2018-11-02 10:39:57 +010058 ALOGE_IF(result.nbSkippedElement != 0,
59 "Policy Engine configuration is partially invalid, skipped %zu elements",
60 result.nbSkippedElement);
61
Mikhail Naganovf9003622020-03-10 13:15:08 -070062 auto legacyStrategy = getLegacyStrategy();
63 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010064 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010065 }
Mikhail Naganovabb04782023-05-02 13:56:01 -070066
67 return OK;
François Gaffie2110e042015-03-24 08:41:51 +010068}
69
Mikhail Naganov9e459d72023-05-05 17:36:39 -070070
François Gaffie2110e042015-03-24 08:41:51 +010071status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
72{
73 switch(usage) {
74 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
75 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
76 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000077 ALOGW("setForceUse() invalid config %d for COMMUNICATION", config);
François Gaffie2110e042015-03-24 08:41:51 +010078 return BAD_VALUE;
79 }
François Gaffie2110e042015-03-24 08:41:51 +010080 break;
81 case AUDIO_POLICY_FORCE_FOR_MEDIA:
82 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
83 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
84 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
85 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
86 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000087 ALOGW("setForceUse() invalid config %d for MEDIA", config);
François Gaffie2110e042015-03-24 08:41:51 +010088 return BAD_VALUE;
89 }
François Gaffie2110e042015-03-24 08:41:51 +010090 break;
91 case AUDIO_POLICY_FORCE_FOR_RECORD:
92 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
93 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000094 ALOGW("setForceUse() invalid config %d for RECORD", config);
François Gaffie2110e042015-03-24 08:41:51 +010095 return BAD_VALUE;
96 }
François Gaffie2110e042015-03-24 08:41:51 +010097 break;
98 case AUDIO_POLICY_FORCE_FOR_DOCK:
99 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
100 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
101 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
102 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
103 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000104 ALOGW("setForceUse() invalid config %d for DOCK", config);
105 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100106 }
François Gaffie2110e042015-03-24 08:41:51 +0100107 break;
108 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
109 if (config != AUDIO_POLICY_FORCE_NONE &&
110 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000111 ALOGW("setForceUse() invalid config %d for SYSTEM", config);
112 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100113 }
François Gaffie2110e042015-03-24 08:41:51 +0100114 break;
115 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
116 if (config != AUDIO_POLICY_FORCE_NONE &&
117 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800118 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000119 return BAD_VALUE;
Phil Burk09bc4612016-02-24 15:58:15 -0800120 }
Phil Burk09bc4612016-02-24 15:58:15 -0800121 break;
122 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
123 if (config != AUDIO_POLICY_FORCE_NONE &&
124 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700125 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800127 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
128 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100129 }
François Gaffie2110e042015-03-24 08:41:51 +0100130 break;
Jack He96117ae2018-02-12 20:52:53 -0800131 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
chenxin20e9f2d9f2024-06-26 10:51:31 +0800132 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_BT_BLE
133 && config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000134 ALOGW("setForceUse() invalid config %d for VIBRATE_RINGING", config);
Jack He96117ae2018-02-12 20:52:53 -0800135 return BAD_VALUE;
136 }
Jack He96117ae2018-02-12 20:52:53 -0800137 break;
François Gaffie2110e042015-03-24 08:41:51 +0100138 default:
139 ALOGW("setForceUse() invalid usage %d", usage);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000140 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100141 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100142 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100143}
144
Eric Laurentffff1f72024-06-12 19:08:15 +0000145bool Engine::isBtScoActive(DeviceVector& availableOutputDevices,
146 const SwAudioOutputCollection &outputs) const {
147 if (availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
148 return false;
149 }
150 // SCO is active if:
151 // 1) we are in a call and SCO is the preferred device for PHONE strategy
152 if (isInCall() && audio_is_bluetooth_out_sco_device(
153 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE))) {
154 return true;
155 }
156
157 // 2) A strategy for which the preferred device is SCO is active
158 for (const auto &ps : getOrderedProductStrategies()) {
159 if (outputs.isStrategyActive(ps) &&
160 !getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, ps)
161 .getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
162 return true;
163 }
164 }
165 // 3) a ringtone is active and SCO is used for ringing
166 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING))
167 && (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
168 == AUDIO_POLICY_FORCE_BT_SCO)) {
169 return true;
170 }
171 // 4) an active input is routed from SCO
172 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
173 const auto &inputs = getApmObserver()->getInputs();
174 if (inputs.activeInputsCountOnDevices(availableInputDevices.getDevicesFromType(
175 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) > 0) {
176 return true;
177 }
178 return false;
179}
180
Eric Laurent2802b862021-03-01 09:36:16 +0100181void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
182 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100183 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800184{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100185 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
186
Eric Laurentffff1f72024-06-12 19:08:15 +0000187 if (com::android::media::audioserver::use_bt_sco_for_media()) {
188 // remove A2DP and LE Audio devices whenever BT SCO is in use
189 if (isBtScoActive(availableOutputDevices, outputs)) {
190 availableOutputDevices.remove(
191 availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllA2dpSet()));
192 availableOutputDevices.remove(
193 availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet()));
194 }
195 }
196
François Gaffie2110e042015-03-24 08:41:51 +0100197 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100198 case STRATEGY_SONIFICATION_RESPECTFUL: {
199 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800200 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700201 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700202 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100203 }
Eric Laurent2802b862021-03-01 09:36:16 +0100204 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100205 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100206 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100207 // Force use of only devices on primary output if:
208 // - in call AND
209 // - cannot route from voice call RX OR
210 // - audio HAL version is < 3.0 and TX device is on the primary HW module
211 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
François Gaffie2110e042015-03-24 08:41:51 +0100212 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
Eric Laurent5569bf42023-08-30 15:53:55 +0200213 if (primaryOutput != nullptr) {
214 audio_devices_t txDevice = AUDIO_DEVICE_NONE;
215 sp<DeviceDescriptor> txDeviceDesc =
216 getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
217 if (txDeviceDesc != nullptr) {
218 txDevice = txDeviceDesc->type();
219 }
220 DeviceVector availPrimaryInputDevices =
221 availableInputDevices.getDevicesFromHwModule(
222 primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800223
Eric Laurent5569bf42023-08-30 15:53:55 +0200224 // TODO: getPrimaryOutput return only devices from first module in
225 // audio_policy_configuration.xml, hearing aid is not there, but it's
226 // a primary device
227 // FIXME: this is not the right way of solving this problem
228 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
229 primaryOutput->supportedDevices().types());
230 availPrimaryOutputDevices.add(
231 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100232
Eric Laurent5569bf42023-08-30 15:53:55 +0200233 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
234 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr)
235 || ((availPrimaryInputDevices.getDevice(
236 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
237 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
238 availableOutputDevices = availPrimaryOutputDevices;
239 }
240 } else {
241 ALOGE("%s, STRATEGY_PHONE: Primary output not found", __func__);
François Gaffie2110e042015-03-24 08:41:51 +0100242 }
Eric Laurent97cec6f2021-05-20 13:52:47 +0200243 }
244 // Do not use A2DP devices when in call but use them when not in call
245 // (e.g for voice mail playback)
246 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200247 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
248 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
249 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100250 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200251 // If connected to a dock, never use the device speaker for calls
252 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
253 .isEmpty()) {
254 availableOutputDevices.remove(
255 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
256 }
Eric Laurent2802b862021-03-01 09:36:16 +0100257 } break;
258 case STRATEGY_ACCESSIBILITY: {
259 // do not route accessibility prompts to a digital output currently configured with a
260 // compressed format as they would likely not be mixed and dropped.
261 for (size_t i = 0; i < outputs.size(); i++) {
262 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
263 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
264 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
265 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800266 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100267 }
268 }
269 } break;
270 default:
271 break;
272 }
273}
274
Eric Laurent6cd5f902021-03-23 18:29:58 +0100275product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
276 const SwAudioOutputCollection &outputs) const {
277 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
278 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
279
280 if (isInCall()) {
281 switch (legacyStrategy) {
282 case STRATEGY_ACCESSIBILITY:
283 case STRATEGY_DTMF:
284 case STRATEGY_MEDIA:
285 case STRATEGY_SONIFICATION:
286 case STRATEGY_SONIFICATION_RESPECTFUL:
287 legacyStrategy = STRATEGY_PHONE;
288 break;
289
290 default:
291 return strategy;
292 }
293 } else {
294 switch (legacyStrategy) {
295 case STRATEGY_SONIFICATION_RESPECTFUL:
296 case STRATEGY_SONIFICATION:
297 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
298 legacyStrategy = STRATEGY_PHONE;
299 }
300 break;
301
302 case STRATEGY_ACCESSIBILITY:
303 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
304 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
305 legacyStrategy = STRATEGY_SONIFICATION;
306 }
307 break;
308
309 default:
310 return strategy;
311 }
312 }
313 return getProductStrategyFromLegacy(legacyStrategy);
314}
315
Eric Laurent2802b862021-03-01 09:36:16 +0100316DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
317 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100318 const SwAudioOutputCollection &outputs) const
319{
320 DeviceVector devices;
321
322 switch (strategy) {
323
324 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
325 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
326 break;
327
Eric Laurent2802b862021-03-01 09:36:16 +0100328 case STRATEGY_PHONE: {
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000329 // TODO(b/243670205): remove this logic that gives preference to last removable devices
330 // once a UX decision has been made
Eric Laurentc0697592021-05-10 11:45:22 +0200331 devices = availableOutputDevices.getFirstDevicesFromTypes(
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000332 getLastRemovableMediaDevices(GROUP_NONE, {
333 // excluding HEARING_AID and BLE_HEADSET because Dialer uses
334 // setCommunicationDevice to select them explicitly
335 AUDIO_DEVICE_OUT_HEARING_AID,
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530336 AUDIO_DEVICE_OUT_BLE_HEADSET,
337 AUDIO_DEVICE_OUT_AUX_DIGITAL
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000338 }));
Eric Laurent2517af32020-11-25 15:31:27 +0100339 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100340 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin29230182023-04-04 21:02:36 +0000341 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
342 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100343 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100344
345 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100346 case STRATEGY_ENFORCED_AUDIBLE:
347 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
348 // except:
349 // - when in call where it doesn't default to STRATEGY_PHONE behavior
350 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
351
352 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100353 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Jean-Michel Trivi00afde12023-02-01 20:46:32 +0000354 // favor dock over speaker when available
355 devices = availableOutputDevices.getFirstDevicesFromTypes({
356 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100357 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800358
359 // if SCO headset is connected and we are told to use it, play ringtone over
360 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700361 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700362 DeviceVector devices2;
363 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
364 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
365 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800366 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100367 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800368 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100369 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800370 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700371 if (!devices2.isEmpty()) {
372 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800373 break;
374 }
375 }
376 }
377 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100378 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
379 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700380 if (strategy == STRATEGY_SONIFICATION) {
381 devices.replaceDevicesByType(
382 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700383 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700384 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800385 }
jiabinf502d152019-08-07 14:43:33 -0700386 if (!devices2.isEmpty()) {
387 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800388 break;
389 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800390 }
391 }
chenxin20e9f2d9f2024-06-26 10:51:31 +0800392
393 // if LEA headset is connected and we are told to use it, play ringtone over
394 // speaker and BT LEA
395 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet()).isEmpty()) {
396 DeviceVector devices2;
397 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
398 AUDIO_DEVICE_OUT_BLE_HEADSET, AUDIO_DEVICE_OUT_BLE_SPEAKER});
399 // Use ONLY Bluetooth LEA output when ringing in vibration mode
400 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
401 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
402 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
403 == AUDIO_POLICY_FORCE_BT_BLE) {
404 if (!devices2.isEmpty()) {
405 devices = devices2;
406 break;
407 }
408 }
409 }
410 // Use both Bluetooth LEA and phone default output when ringing in normal mode
411 if (audio_is_ble_out_device(getPreferredDeviceTypeForLegacyStrategy(
412 availableOutputDevices, STRATEGY_PHONE))) {
413 if (strategy == STRATEGY_SONIFICATION) {
414 devices.replaceDevicesByType(
415 AUDIO_DEVICE_OUT_SPEAKER,
416 availableOutputDevices.getDevicesFromType(
417 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
418 }
419 if (!devices2.isEmpty()) {
420 devices.add(devices2);
421 break;
422 }
423 }
424 }
425
François Gaffie2110e042015-03-24 08:41:51 +0100426 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700427 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100428
Eric Laurent6cd5f902021-03-23 18:29:58 +0100429 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100430 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100431 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100432 case STRATEGY_REROUTING:
433 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700434 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100435 if (strategy != STRATEGY_SONIFICATION) {
436 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700437 sp<DeviceDescriptor> remoteSubmix;
438 if ((remoteSubmix = availableOutputDevices.getDevice(
439 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
440 AUDIO_FORMAT_DEFAULT)) != nullptr) {
441 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100442 }
443 }
Eric Laurenta9986862020-10-07 08:42:28 -0700444
jiabinf502d152019-08-07 14:43:33 -0700445 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100446 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700447 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100448 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100449
450 // LE audio broadcast device is only used if:
451 // - No call is active
452 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
453 // OR the LE audio unicast device is not active
454 if (devices2.isEmpty() && !isInCall()
455 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
456 legacy_strategy topActiveStrategy = STRATEGY_NONE;
457 for (const auto &ps : getOrderedProductStrategies()) {
458 if (outputs.isStrategyActive(ps)) {
459 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
460 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
461 break;
462 }
463 }
464
465 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
466 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
467 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
468 devices2 =
469 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
470 }
471 }
472
Baekgyeong Kim08451522019-11-01 20:40:02 +0900473 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530474 std::vector<audio_devices_t> excludedDevices;
475 // no sonification on aux digital (e.g. HDMI)
476 if (strategy == STRATEGY_SONIFICATION) {
477 excludedDevices.push_back(AUDIO_DEVICE_OUT_AUX_DIGITAL);
478 }
Eric Laurenta9986862020-10-07 08:42:28 -0700479 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900480 // Get the last connected device of wired and bluetooth a2dp
481 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530482 getLastRemovableMediaDevices(GROUP_NONE, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900483 } else {
484 // Get the last connected device of wired except bluetooth a2dp
485 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530486 getLastRemovableMediaDevices(GROUP_WIRED, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900487 }
François Gaffie2110e042015-03-24 08:41:51 +0100488 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000489
490 if (com::android::media::audioserver::use_bt_sco_for_media()) {
491 if (devices2.isEmpty() && isBtScoActive(availableOutputDevices, outputs)) {
492 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
493 { AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,
494 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
495 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
496 }
497 }
498
jiabinf502d152019-08-07 14:43:33 -0700499 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100500 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700501 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700502 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100503 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000504
jiabinf502d152019-08-07 14:43:33 -0700505 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100506 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
507 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100508 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000509
jiabinf502d152019-08-07 14:43:33 -0700510 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100511 if (strategy == STRATEGY_MEDIA) {
512 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700513 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800514 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
515 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
516 });
François Gaffie2110e042015-03-24 08:41:51 +0100517 }
518
jiabinf502d152019-08-07 14:43:33 -0700519 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100520 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
521 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700522 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100523
524 // If hdmi system audio mode is on, remove speaker out of output list.
525 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100526 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100527 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700528 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100529 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700530
Eric Laurent6cd5f902021-03-23 18:29:58 +0100531 bool mediaActiveLocally =
532 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
533 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
534 || outputs.isActiveLocally(
535 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
536 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800537
538 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
539 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700540 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100541 // - for STRATEGY_SONIFICATION_RESPECTFUL:
542 // if no media is playing on the device, check for mandatory use of "safe" speaker
543 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800544 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100545 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700546 devices.replaceDevicesByType(
547 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700548 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700549 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700550 }
François Gaffie2110e042015-03-24 08:41:51 +0100551 } break;
552
Eric Laurent21777f82019-12-06 18:12:06 -0800553 case STRATEGY_CALL_ASSISTANT:
554 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
555 break;
556
Eric Laurentd9766932020-08-07 14:01:22 -0700557 case STRATEGY_NONE:
558 // Happens when internal strategies are processed ("rerouting", "patch"...)
559 break;
560
François Gaffie2110e042015-03-24 08:41:51 +0100561 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700562 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100563 break;
564 }
565
jiabinf502d152019-08-07 14:43:33 -0700566 if (devices.isEmpty()) {
jiabind0a8d6f2022-09-19 20:32:01 +0000567 ALOGI("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700568 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
569 if (defaultOutputDevice != nullptr) {
570 devices.add(defaultOutputDevice);
571 }
572 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700573 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700574 }
jiabinf502d152019-08-07 14:43:33 -0700575
Eric Laurentd9766932020-08-07 14:01:22 -0700576 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800577 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700578 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100579}
580
jiabin14662b52023-03-06 21:35:29 +0000581DeviceVector Engine::getPreferredAvailableDevicesForInputSource(
582 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
583 DeviceVector preferredAvailableDevVec = {};
584 AudioDeviceTypeAddrVector preferredDevices;
585 const status_t status = getDevicesForRoleAndCapturePreset(
586 inputSource, DEVICE_ROLE_PREFERRED, preferredDevices);
587 if (status == NO_ERROR) {
588 // Only use preferred devices when they are all available.
589 preferredAvailableDevVec =
590 availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices);
591 if (preferredAvailableDevVec.size() == preferredDevices.size()) {
592 ALOGVV("%s using pref device %s for source %u",
593 __func__, preferredAvailableDevVec.toString().c_str(), inputSource);
594 return preferredAvailableDevVec;
595 }
596 }
597 return preferredAvailableDevVec;
598}
599
600DeviceVector Engine::getDisabledDevicesForInputSource(
601 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
602 DeviceVector disabledDevices = {};
603 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
604 const status_t status = getDevicesForRoleAndCapturePreset(
605 inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
606 if (status == NO_ERROR) {
607 disabledDevices =
608 availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
609 }
610 return disabledDevices;
611}
François Gaffie2110e042015-03-24 08:41:51 +0100612
jiabinf502d152019-08-07 14:43:33 -0700613sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100614{
Eric Laurentaf377772019-03-29 14:50:21 -0700615 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
616 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100617 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700618 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700619 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700620 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
621 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700622 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100623
Eric Laurentdc95a252018-04-12 12:46:56 -0700624 // when a call is active, force device selection to match source VOICE_COMMUNICATION
625 // for most other input sources to avoid rerouting call TX audio
626 if (isInCall()) {
627 switch (inputSource) {
628 case AUDIO_SOURCE_DEFAULT:
629 case AUDIO_SOURCE_MIC:
630 case AUDIO_SOURCE_VOICE_RECOGNITION:
631 case AUDIO_SOURCE_UNPROCESSED:
632 case AUDIO_SOURCE_HOTWORD:
633 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800634 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800635 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700636 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
637 break;
638 default:
639 break;
640 }
641 }
642
jiabin14662b52023-03-06 21:35:29 +0000643 // Use the preferred device for the input source if it is available.
644 DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource(
645 availableDevices, inputSource);
646 if (!preferredInputDevices.isEmpty()) {
647 // Currently, only support single device for input. The public JAVA API also only
648 // support setting single device as preferred device. In that case, returning the
649 // first device is OK here.
650 return preferredInputDevices[0];
651 }
652 // Remove the disabled device for the input source from the available input device list.
653 DeviceVector disabledInputDevices = getDisabledDevicesForInputSource(
654 availableDevices, inputSource);
655 availableDevices.remove(disabledInputDevices);
656
Eric Laurent2517af32020-11-25 15:31:27 +0100657 audio_devices_t commDeviceType =
658 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
659
François Gaffie2110e042015-03-24 08:41:51 +0100660 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100661 case AUDIO_SOURCE_DEFAULT:
662 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700663 device = availableDevices.getDevice(
664 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
665 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100666 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700667 device = availableDevices.getDevice(
668 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
669 if (device != nullptr) break;
670 }
671 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200672 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700673 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500674 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700675 break;
François Gaffie2110e042015-03-24 08:41:51 +0100676
677 case AUDIO_SOURCE_VOICE_COMMUNICATION:
678 // Allow only use of devices on primary input if in call and HAL does not support routing
679 // to voice call path.
680 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700681 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
682 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
Eric Laurent5569bf42023-08-30 15:53:55 +0200683 if (!availablePrimaryDevices.isEmpty()) {
684 availableDevices = availablePrimaryDevices;
685 } else {
686 ALOGE("%s, AUDIO_SOURCE_VOICE_COMMUNICATION: Primary devices not found", __func__);
687 }
François Gaffie2110e042015-03-24 08:41:51 +0100688 }
689
Eric Laurent2517af32020-11-25 15:31:27 +0100690 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100691 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700692 device = availableDevices.getDevice(
693 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
694 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100695 break;
696 }
Eric Laurent2517af32020-11-25 15:31:27 +0100697 }
698 switch (commDeviceType) {
Eric Laurent2517af32020-11-25 15:31:27 +0100699 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700700 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100701 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
702 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100703 break;
Eric Laurentcedd5b52023-03-22 00:03:31 +0000704 case AUDIO_DEVICE_OUT_BLE_HEADSET:
705 device = availableDevices.getDevice(
706 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
707 if (device != nullptr) {
708 break;
709 }
710 ALOGE("%s LE Audio selected for communication but input device not available",
711 __func__);
712 FALLTHROUGH_INTENDED;
Eric Laurent2517af32020-11-25 15:31:27 +0100713 default: // FORCE_NONE
714 device = availableDevices.getFirstExistingDevice({
715 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500716 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
717 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100718 break;
François Gaffie2110e042015-03-24 08:41:51 +0100719 }
720 break;
721
722 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800723 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500724 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
725 device = availableDevices.getDevice(
726 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
727 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700728 }
wei wanga7020522020-12-10 21:36:11 -0500729 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
730 // because sometimes user want to do voice search by bt remote
731 // even if BUILDIN_MIC is available.
732 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200733 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500734 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
735 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
736
737 break;
738 case AUDIO_SOURCE_HOTWORD:
739 // We should not use primary output criteria for Hotword but rather limit
740 // to devices attached to the same HW module as the build in mic
Eric Laurent5569bf42023-08-30 15:53:55 +0200741 if (!availablePrimaryDevices.isEmpty()) {
742 availableDevices = availablePrimaryDevices;
743 } else {
744 ALOGE("%s, AUDIO_SOURCE_HOTWORD: Primary devices not found", __func__);
745 }
Eric Laurent2517af32020-11-25 15:31:27 +0100746 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700747 device = availableDevices.getDevice(
748 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
749 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100750 }
jiabinf502d152019-08-07 14:43:33 -0700751 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200752 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700753 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
754 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100755 break;
756 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700757 // For a device without built-in mic, adding usb device
758 device = availableDevices.getFirstExistingDevice({
759 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
760 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100761 break;
762 case AUDIO_SOURCE_VOICE_DOWNLINK:
763 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700764 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700765 device = availableDevices.getDevice(
766 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100767 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800768 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700769 device = availableDevices.getFirstExistingDevice({
770 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500771 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
772 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800773 break;
François Gaffie2110e042015-03-24 08:41:51 +0100774 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700775 device = availableDevices.getDevice(
776 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100777 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800778 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700779 device = availableDevices.getDevice(
780 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100781 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800782 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700783 device = availableDevices.getDevice(
784 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800785 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800786 case AUDIO_SOURCE_ULTRASOUND:
787 device = availableDevices.getFirstExistingDevice({
788 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
789 break;
François Gaffie2110e042015-03-24 08:41:51 +0100790 default:
791 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
792 break;
793 }
jiabinf502d152019-08-07 14:43:33 -0700794 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700795 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700796 device = availableDevices.getDevice(
797 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
798 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700799 "getDeviceForInputSource() no default device defined");
800 }
Eric Laurent2517af32020-11-25 15:31:27 +0100801
jiabinf502d152019-08-07 14:43:33 -0700802 ALOGV_IF(device != nullptr,
803 "getDeviceForInputSource()input source %d, device %08x",
804 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100805 return device;
806}
807
jiabin29230182023-04-04 21:02:36 +0000808void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
809 strategy->setDeviceTypes(devices.types());
810 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100811}
812
Eric Laurent2517af32020-11-25 15:31:27 +0100813product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
814 for (const auto& strategyMap : mLegacyStrategyMap) {
815 if (strategyMap.second == legacyStrategy) {
816 return strategyMap.first;
817 }
818 }
819 return PRODUCT_STRATEGY_NONE;
820}
François Gaffiedc7553f2018-11-02 10:39:57 +0100821
Eric Laurent2517af32020-11-25 15:31:27 +0100822audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
823 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
824 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
825 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
826 availableOutputDevices, strategy);
827 if (devices.size() > 0) {
828 return devices[0]->type();
829 }
830 return AUDIO_DEVICE_NONE;
831}
832
Eric Laurent2517af32020-11-25 15:31:27 +0100833DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100834 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
835
836 // Take context into account to remap product strategy before
837 // checking preferred device for strategy and applying default routing rules
838 strategy = remapStrategyFromContext(strategy, outputs);
839
Eric Laurent2517af32020-11-25 15:31:27 +0100840 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
841 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
842
Eric Laurent6cd5f902021-03-23 18:29:58 +0100843 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100844
Eric Laurent6cd5f902021-03-23 18:29:58 +0100845 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100846
Eric Laurent2517af32020-11-25 15:31:27 +0100847 // check if this strategy has a preferred device that is available,
848 // if yes, give priority to it.
849 DeviceVector preferredAvailableDevVec =
850 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
851 if (!preferredAvailableDevVec.isEmpty()) {
852 return preferredAvailableDevVec;
853 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700854
jiabind0a8d6f2022-09-19 20:32:01 +0000855 // Remove all disabled devices from the available device list.
856 DeviceVector disabledDevVec =
857 getDisabledDevicesForProductStrategy(availableOutputDevices, strategy);
858 availableOutputDevices.remove(disabledDevVec);
859
jiabinf502d152019-08-07 14:43:33 -0700860 return getDevicesForStrategyInt(legacyStrategy,
861 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100862 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100863}
864
865DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
866 const sp<DeviceDescriptor> &preferredDevice,
867 bool fromCache) const
868{
869 // First check for explict routing device
870 if (preferredDevice != nullptr) {
871 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
872 return DeviceVector(preferredDevice);
873 }
François Gaffiec005e562018-11-06 15:04:49 +0100874 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700875 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100876 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100877 //
878 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
879 // be by APM?
880 //
François Gaffiec005e562018-11-06 15:04:49 +0100881 // Honor explicit routing requests only if all active clients have a preferred route in which
882 // case the last active client route is used
883 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
884 if (device != nullptr) {
885 return DeviceVector(device);
886 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100887
888 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
889}
890
891DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
892{
893 auto attributes = getAttributesForStreamType(stream);
894 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
895}
896
897sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800898 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200899 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800900 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100901{
François Gaffiec005e562018-11-06 15:04:49 +0100902 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700903 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100904 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100905 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100906
François Gaffiedc7553f2018-11-02 10:39:57 +0100907 //
François Gaffiec005e562018-11-06 15:04:49 +0100908 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
909 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100910 //
François Gaffiec005e562018-11-06 15:04:49 +0100911 // Honor explicit routing requests only if all active clients have a preferred route in which
912 // case the last active client route is used
913 sp<DeviceDescriptor> device =
914 findPreferredDevice(inputs, attr.source, availableInputDevices);
915 if (device != nullptr) {
916 return device;
917 }
918
Jan Sebechlebsky28ed9452022-09-15 17:57:42 +0200919 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800920 availableInputDevices,
921 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200922 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800923 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100924 if (device != nullptr) {
925 return device;
926 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100927
jiabinf502d152019-08-07 14:43:33 -0700928 device = getDeviceForInputSource(attr.source);
929 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
930 // Return immediately if the device is null or it is not a remote submix device.
931 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100932 }
jiabinf502d152019-08-07 14:43:33 -0700933
934 // For remote submix device, try to find the device by address.
935 address = "0";
936 std::size_t pos;
937 std::string tags { attr.tags };
938 if ((pos = tags.find("addr=")) != std::string::npos) {
939 address = tags.substr(pos + std::strlen("addr="));
940 }
941 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100942 String8(address.c_str()),
943 AUDIO_FORMAT_DEFAULT);
944}
945
Mikhail Naganov9e459d72023-05-05 17:36:39 -0700946} // namespace android::audio_policy