blob: 7e35cca118dafd96ae03cfd3221feadb5d1e10e9 [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
François Gaffiedc7553f2018-11-02 10:39:57 +010041struct legacy_strategy_map { const char *name; legacy_strategy id; };
Mikhail Naganovf9003622020-03-10 13:15:08 -070042static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
43 static const std::vector<legacy_strategy_map> legacyStrategy = {
44 { "STRATEGY_NONE", STRATEGY_NONE },
45 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
46 { "STRATEGY_PHONE", STRATEGY_PHONE },
47 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
48 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
49 { "STRATEGY_DTMF", STRATEGY_DTMF },
50 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
51 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
52 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
53 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
François Gaffiead3dce92024-03-26 17:20:04 +010054 { "STRATEGY_PATCH", STRATEGY_PATCH }, // boiler to manage stream patch volume
Mikhail Naganovf9003622020-03-10 13:15:08 -070055 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
56 };
57 return legacyStrategy;
58}
François Gaffiedc7553f2018-11-02 10:39:57 +010059
Mikhail Naganov9e459d72023-05-05 17:36:39 -070060status_t Engine::loadFromHalConfigWithFallback(
61 const media::audio::common::AudioHalEngineConfig& aidlConfig) {
62 return loadWithFallback(aidlConfig);
63}
64
Mikhail Naganovabb04782023-05-02 13:56:01 -070065status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
Mikhail Naganov9e459d72023-05-05 17:36:39 -070066 return loadWithFallback(xmlFilePath);
67}
68
69template<typename T>
70status_t Engine::loadWithFallback(const T& configSource) {
71 auto result = EngineBase::loadAudioPolicyEngineConfig(configSource);
François Gaffiedc7553f2018-11-02 10:39:57 +010072 ALOGE_IF(result.nbSkippedElement != 0,
73 "Policy Engine configuration is partially invalid, skipped %zu elements",
74 result.nbSkippedElement);
75
Mikhail Naganovf9003622020-03-10 13:15:08 -070076 auto legacyStrategy = getLegacyStrategy();
77 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010078 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010079 }
Mikhail Naganovabb04782023-05-02 13:56:01 -070080
81 return OK;
François Gaffie2110e042015-03-24 08:41:51 +010082}
83
Mikhail Naganov9e459d72023-05-05 17:36:39 -070084
François Gaffie2110e042015-03-24 08:41:51 +010085status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
86{
87 switch(usage) {
88 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
89 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
90 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000091 ALOGW("setForceUse() invalid config %d for COMMUNICATION", 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_MEDIA:
96 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
97 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
98 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
99 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
100 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000101 ALOGW("setForceUse() invalid config %d for MEDIA", config);
François Gaffie2110e042015-03-24 08:41:51 +0100102 return BAD_VALUE;
103 }
François Gaffie2110e042015-03-24 08:41:51 +0100104 break;
105 case AUDIO_POLICY_FORCE_FOR_RECORD:
106 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
107 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000108 ALOGW("setForceUse() invalid config %d for RECORD", config);
François Gaffie2110e042015-03-24 08:41:51 +0100109 return BAD_VALUE;
110 }
François Gaffie2110e042015-03-24 08:41:51 +0100111 break;
112 case AUDIO_POLICY_FORCE_FOR_DOCK:
113 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
114 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
115 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
116 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
117 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000118 ALOGW("setForceUse() invalid config %d for DOCK", config);
119 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100120 }
François Gaffie2110e042015-03-24 08:41:51 +0100121 break;
122 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
123 if (config != AUDIO_POLICY_FORCE_NONE &&
124 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000125 ALOGW("setForceUse() invalid config %d for SYSTEM", config);
126 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100127 }
François Gaffie2110e042015-03-24 08:41:51 +0100128 break;
129 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
130 if (config != AUDIO_POLICY_FORCE_NONE &&
131 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800132 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000133 return BAD_VALUE;
Phil Burk09bc4612016-02-24 15:58:15 -0800134 }
Phil Burk09bc4612016-02-24 15:58:15 -0800135 break;
136 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
137 if (config != AUDIO_POLICY_FORCE_NONE &&
138 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700139 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
140 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800141 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
142 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100143 }
François Gaffie2110e042015-03-24 08:41:51 +0100144 break;
Jack He96117ae2018-02-12 20:52:53 -0800145 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
chenxin20e9f2d9f2024-06-26 10:51:31 +0800146 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_BT_BLE
147 && config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000148 ALOGW("setForceUse() invalid config %d for VIBRATE_RINGING", config);
Jack He96117ae2018-02-12 20:52:53 -0800149 return BAD_VALUE;
150 }
Jack He96117ae2018-02-12 20:52:53 -0800151 break;
François Gaffie2110e042015-03-24 08:41:51 +0100152 default:
153 ALOGW("setForceUse() invalid usage %d", usage);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000154 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100155 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100156 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100157}
158
Eric Laurentffff1f72024-06-12 19:08:15 +0000159bool Engine::isBtScoActive(DeviceVector& availableOutputDevices,
160 const SwAudioOutputCollection &outputs) const {
161 if (availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
162 return false;
163 }
164 // SCO is active if:
165 // 1) we are in a call and SCO is the preferred device for PHONE strategy
166 if (isInCall() && audio_is_bluetooth_out_sco_device(
167 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE))) {
168 return true;
169 }
170
171 // 2) A strategy for which the preferred device is SCO is active
172 for (const auto &ps : getOrderedProductStrategies()) {
173 if (outputs.isStrategyActive(ps) &&
174 !getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, ps)
175 .getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
176 return true;
177 }
178 }
179 // 3) a ringtone is active and SCO is used for ringing
180 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING))
181 && (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
182 == AUDIO_POLICY_FORCE_BT_SCO)) {
183 return true;
184 }
185 // 4) an active input is routed from SCO
186 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
187 const auto &inputs = getApmObserver()->getInputs();
188 if (inputs.activeInputsCountOnDevices(availableInputDevices.getDevicesFromType(
189 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) > 0) {
190 return true;
191 }
192 return false;
193}
194
Eric Laurent2802b862021-03-01 09:36:16 +0100195void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
196 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100197 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800198{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100199 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
200
Eric Laurentffff1f72024-06-12 19:08:15 +0000201 if (com::android::media::audioserver::use_bt_sco_for_media()) {
202 // remove A2DP and LE Audio devices whenever BT SCO is in use
203 if (isBtScoActive(availableOutputDevices, outputs)) {
204 availableOutputDevices.remove(
205 availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllA2dpSet()));
206 availableOutputDevices.remove(
207 availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet()));
208 }
209 }
210
François Gaffie2110e042015-03-24 08:41:51 +0100211 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100212 case STRATEGY_SONIFICATION_RESPECTFUL: {
213 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800214 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700215 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700216 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100217 }
Eric Laurent2802b862021-03-01 09:36:16 +0100218 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100219 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100220 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100221 // Force use of only devices on primary output if:
222 // - in call AND
223 // - cannot route from voice call RX OR
224 // - audio HAL version is < 3.0 and TX device is on the primary HW module
225 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
François Gaffie2110e042015-03-24 08:41:51 +0100226 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
Eric Laurent5569bf42023-08-30 15:53:55 +0200227 if (primaryOutput != nullptr) {
228 audio_devices_t txDevice = AUDIO_DEVICE_NONE;
229 sp<DeviceDescriptor> txDeviceDesc =
230 getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
231 if (txDeviceDesc != nullptr) {
232 txDevice = txDeviceDesc->type();
233 }
234 DeviceVector availPrimaryInputDevices =
235 availableInputDevices.getDevicesFromHwModule(
236 primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800237
Eric Laurent5569bf42023-08-30 15:53:55 +0200238 // TODO: getPrimaryOutput return only devices from first module in
239 // audio_policy_configuration.xml, hearing aid is not there, but it's
240 // a primary device
241 // FIXME: this is not the right way of solving this problem
242 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
243 primaryOutput->supportedDevices().types());
244 availPrimaryOutputDevices.add(
245 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100246
Eric Laurent5569bf42023-08-30 15:53:55 +0200247 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
248 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr)
249 || ((availPrimaryInputDevices.getDevice(
250 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
251 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
252 availableOutputDevices = availPrimaryOutputDevices;
253 }
254 } else {
255 ALOGE("%s, STRATEGY_PHONE: Primary output not found", __func__);
François Gaffie2110e042015-03-24 08:41:51 +0100256 }
Eric Laurent97cec6f2021-05-20 13:52:47 +0200257 }
258 // Do not use A2DP devices when in call but use them when not in call
259 // (e.g for voice mail playback)
260 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200261 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
262 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
263 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100264 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200265 // If connected to a dock, never use the device speaker for calls
266 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
267 .isEmpty()) {
268 availableOutputDevices.remove(
269 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
270 }
Eric Laurent2802b862021-03-01 09:36:16 +0100271 } break;
272 case STRATEGY_ACCESSIBILITY: {
273 // do not route accessibility prompts to a digital output currently configured with a
274 // compressed format as they would likely not be mixed and dropped.
275 for (size_t i = 0; i < outputs.size(); i++) {
276 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
277 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
278 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
279 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800280 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100281 }
282 }
283 } break;
284 default:
285 break;
286 }
287}
288
Eric Laurent6cd5f902021-03-23 18:29:58 +0100289product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
290 const SwAudioOutputCollection &outputs) const {
291 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
292 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
293
294 if (isInCall()) {
295 switch (legacyStrategy) {
296 case STRATEGY_ACCESSIBILITY:
297 case STRATEGY_DTMF:
298 case STRATEGY_MEDIA:
299 case STRATEGY_SONIFICATION:
300 case STRATEGY_SONIFICATION_RESPECTFUL:
301 legacyStrategy = STRATEGY_PHONE;
302 break;
303
304 default:
305 return strategy;
306 }
307 } else {
308 switch (legacyStrategy) {
309 case STRATEGY_SONIFICATION_RESPECTFUL:
310 case STRATEGY_SONIFICATION:
311 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
312 legacyStrategy = STRATEGY_PHONE;
313 }
314 break;
315
316 case STRATEGY_ACCESSIBILITY:
317 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
318 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
319 legacyStrategy = STRATEGY_SONIFICATION;
320 }
321 break;
322
323 default:
324 return strategy;
325 }
326 }
327 return getProductStrategyFromLegacy(legacyStrategy);
328}
329
Eric Laurent2802b862021-03-01 09:36:16 +0100330DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
331 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100332 const SwAudioOutputCollection &outputs) const
333{
334 DeviceVector devices;
335
336 switch (strategy) {
337
338 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
339 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
340 break;
341
Eric Laurent2802b862021-03-01 09:36:16 +0100342 case STRATEGY_PHONE: {
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000343 // TODO(b/243670205): remove this logic that gives preference to last removable devices
344 // once a UX decision has been made
Eric Laurentc0697592021-05-10 11:45:22 +0200345 devices = availableOutputDevices.getFirstDevicesFromTypes(
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000346 getLastRemovableMediaDevices(GROUP_NONE, {
347 // excluding HEARING_AID and BLE_HEADSET because Dialer uses
348 // setCommunicationDevice to select them explicitly
349 AUDIO_DEVICE_OUT_HEARING_AID,
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530350 AUDIO_DEVICE_OUT_BLE_HEADSET,
351 AUDIO_DEVICE_OUT_AUX_DIGITAL
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000352 }));
Eric Laurent2517af32020-11-25 15:31:27 +0100353 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100354 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin29230182023-04-04 21:02:36 +0000355 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
356 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100357 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100358
359 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100360 case STRATEGY_ENFORCED_AUDIBLE:
361 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
362 // except:
363 // - when in call where it doesn't default to STRATEGY_PHONE behavior
364 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
365
366 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100367 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Jean-Michel Trivi00afde12023-02-01 20:46:32 +0000368 // favor dock over speaker when available
369 devices = availableOutputDevices.getFirstDevicesFromTypes({
370 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100371 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800372
373 // if SCO headset is connected and we are told to use it, play ringtone over
374 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700375 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700376 DeviceVector devices2;
377 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
378 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
379 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800380 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100381 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800382 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100383 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800384 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700385 if (!devices2.isEmpty()) {
386 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800387 break;
388 }
389 }
390 }
391 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100392 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
393 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700394 if (strategy == STRATEGY_SONIFICATION) {
395 devices.replaceDevicesByType(
396 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700397 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700398 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800399 }
jiabinf502d152019-08-07 14:43:33 -0700400 if (!devices2.isEmpty()) {
401 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800402 break;
403 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800404 }
405 }
chenxin20e9f2d9f2024-06-26 10:51:31 +0800406
407 // if LEA headset is connected and we are told to use it, play ringtone over
408 // speaker and BT LEA
409 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllBleSet()).isEmpty()) {
410 DeviceVector devices2;
411 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
412 AUDIO_DEVICE_OUT_BLE_HEADSET, AUDIO_DEVICE_OUT_BLE_SPEAKER});
413 // Use ONLY Bluetooth LEA output when ringing in vibration mode
414 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
415 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
416 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
417 == AUDIO_POLICY_FORCE_BT_BLE) {
418 if (!devices2.isEmpty()) {
419 devices = devices2;
420 break;
421 }
422 }
423 }
424 // Use both Bluetooth LEA and phone default output when ringing in normal mode
425 if (audio_is_ble_out_device(getPreferredDeviceTypeForLegacyStrategy(
426 availableOutputDevices, STRATEGY_PHONE))) {
427 if (strategy == STRATEGY_SONIFICATION) {
428 devices.replaceDevicesByType(
429 AUDIO_DEVICE_OUT_SPEAKER,
430 availableOutputDevices.getDevicesFromType(
431 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
432 }
433 if (!devices2.isEmpty()) {
434 devices.add(devices2);
435 break;
436 }
437 }
438 }
439
François Gaffie2110e042015-03-24 08:41:51 +0100440 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700441 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100442
Eric Laurent6cd5f902021-03-23 18:29:58 +0100443 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100444 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100445 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100446 case STRATEGY_REROUTING:
447 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700448 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100449 if (strategy != STRATEGY_SONIFICATION) {
450 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700451 sp<DeviceDescriptor> remoteSubmix;
452 if ((remoteSubmix = availableOutputDevices.getDevice(
453 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
454 AUDIO_FORMAT_DEFAULT)) != nullptr) {
455 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100456 }
457 }
Eric Laurenta9986862020-10-07 08:42:28 -0700458
jiabinf502d152019-08-07 14:43:33 -0700459 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100460 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700461 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100462 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100463
464 // LE audio broadcast device is only used if:
465 // - No call is active
466 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
467 // OR the LE audio unicast device is not active
468 if (devices2.isEmpty() && !isInCall()
469 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
470 legacy_strategy topActiveStrategy = STRATEGY_NONE;
471 for (const auto &ps : getOrderedProductStrategies()) {
472 if (outputs.isStrategyActive(ps)) {
473 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
474 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
475 break;
476 }
477 }
478
479 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
480 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
481 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
482 devices2 =
483 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
484 }
485 }
486
Baekgyeong Kim08451522019-11-01 20:40:02 +0900487 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530488 std::vector<audio_devices_t> excludedDevices;
489 // no sonification on aux digital (e.g. HDMI)
490 if (strategy == STRATEGY_SONIFICATION) {
491 excludedDevices.push_back(AUDIO_DEVICE_OUT_AUX_DIGITAL);
492 }
Eric Laurenta9986862020-10-07 08:42:28 -0700493 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900494 // Get the last connected device of wired and bluetooth a2dp
495 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530496 getLastRemovableMediaDevices(GROUP_NONE, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900497 } else {
498 // Get the last connected device of wired except bluetooth a2dp
499 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530500 getLastRemovableMediaDevices(GROUP_WIRED, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900501 }
François Gaffie2110e042015-03-24 08:41:51 +0100502 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000503
504 if (com::android::media::audioserver::use_bt_sco_for_media()) {
505 if (devices2.isEmpty() && isBtScoActive(availableOutputDevices, outputs)) {
506 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
507 { AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT,
508 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
509 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
510 }
511 }
512
jiabinf502d152019-08-07 14:43:33 -0700513 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100514 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700515 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700516 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100517 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000518
jiabinf502d152019-08-07 14:43:33 -0700519 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100520 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
521 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100522 }
Eric Laurentffff1f72024-06-12 19:08:15 +0000523
jiabinf502d152019-08-07 14:43:33 -0700524 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100525 if (strategy == STRATEGY_MEDIA) {
526 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700527 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800528 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
529 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
530 });
François Gaffie2110e042015-03-24 08:41:51 +0100531 }
532
jiabinf502d152019-08-07 14:43:33 -0700533 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100534 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
535 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700536 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100537
538 // If hdmi system audio mode is on, remove speaker out of output list.
539 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100540 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100541 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700542 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100543 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700544
Eric Laurent6cd5f902021-03-23 18:29:58 +0100545 bool mediaActiveLocally =
546 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
547 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
548 || outputs.isActiveLocally(
549 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
550 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800551
552 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
553 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700554 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100555 // - for STRATEGY_SONIFICATION_RESPECTFUL:
556 // if no media is playing on the device, check for mandatory use of "safe" speaker
557 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800558 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100559 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700560 devices.replaceDevicesByType(
561 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700562 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700563 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700564 }
François Gaffie2110e042015-03-24 08:41:51 +0100565 } break;
566
Eric Laurent21777f82019-12-06 18:12:06 -0800567 case STRATEGY_CALL_ASSISTANT:
568 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
569 break;
570
Eric Laurentd9766932020-08-07 14:01:22 -0700571 case STRATEGY_NONE:
572 // Happens when internal strategies are processed ("rerouting", "patch"...)
573 break;
574
François Gaffie2110e042015-03-24 08:41:51 +0100575 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700576 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100577 break;
578 }
579
jiabinf502d152019-08-07 14:43:33 -0700580 if (devices.isEmpty()) {
jiabind0a8d6f2022-09-19 20:32:01 +0000581 ALOGI("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700582 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
583 if (defaultOutputDevice != nullptr) {
584 devices.add(defaultOutputDevice);
585 }
586 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700587 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700588 }
jiabinf502d152019-08-07 14:43:33 -0700589
Eric Laurentd9766932020-08-07 14:01:22 -0700590 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800591 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700592 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100593}
594
jiabin14662b52023-03-06 21:35:29 +0000595DeviceVector Engine::getPreferredAvailableDevicesForInputSource(
596 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
597 DeviceVector preferredAvailableDevVec = {};
598 AudioDeviceTypeAddrVector preferredDevices;
599 const status_t status = getDevicesForRoleAndCapturePreset(
600 inputSource, DEVICE_ROLE_PREFERRED, preferredDevices);
601 if (status == NO_ERROR) {
602 // Only use preferred devices when they are all available.
603 preferredAvailableDevVec =
604 availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices);
605 if (preferredAvailableDevVec.size() == preferredDevices.size()) {
606 ALOGVV("%s using pref device %s for source %u",
607 __func__, preferredAvailableDevVec.toString().c_str(), inputSource);
608 return preferredAvailableDevVec;
609 }
610 }
611 return preferredAvailableDevVec;
612}
613
614DeviceVector Engine::getDisabledDevicesForInputSource(
615 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
616 DeviceVector disabledDevices = {};
617 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
618 const status_t status = getDevicesForRoleAndCapturePreset(
619 inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
620 if (status == NO_ERROR) {
621 disabledDevices =
622 availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
623 }
624 return disabledDevices;
625}
François Gaffie2110e042015-03-24 08:41:51 +0100626
jiabinf502d152019-08-07 14:43:33 -0700627sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100628{
Eric Laurentaf377772019-03-29 14:50:21 -0700629 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
630 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100631 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700632 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700633 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700634 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
635 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700636 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100637
Eric Laurentdc95a252018-04-12 12:46:56 -0700638 // when a call is active, force device selection to match source VOICE_COMMUNICATION
639 // for most other input sources to avoid rerouting call TX audio
640 if (isInCall()) {
641 switch (inputSource) {
642 case AUDIO_SOURCE_DEFAULT:
643 case AUDIO_SOURCE_MIC:
644 case AUDIO_SOURCE_VOICE_RECOGNITION:
645 case AUDIO_SOURCE_UNPROCESSED:
646 case AUDIO_SOURCE_HOTWORD:
647 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800648 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800649 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700650 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
651 break;
652 default:
653 break;
654 }
655 }
656
jiabin14662b52023-03-06 21:35:29 +0000657 // Use the preferred device for the input source if it is available.
658 DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource(
659 availableDevices, inputSource);
660 if (!preferredInputDevices.isEmpty()) {
661 // Currently, only support single device for input. The public JAVA API also only
662 // support setting single device as preferred device. In that case, returning the
663 // first device is OK here.
664 return preferredInputDevices[0];
665 }
666 // Remove the disabled device for the input source from the available input device list.
667 DeviceVector disabledInputDevices = getDisabledDevicesForInputSource(
668 availableDevices, inputSource);
669 availableDevices.remove(disabledInputDevices);
670
Eric Laurent2517af32020-11-25 15:31:27 +0100671 audio_devices_t commDeviceType =
672 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
673
François Gaffie2110e042015-03-24 08:41:51 +0100674 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100675 case AUDIO_SOURCE_DEFAULT:
676 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700677 device = availableDevices.getDevice(
678 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
679 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100680 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700681 device = availableDevices.getDevice(
682 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
683 if (device != nullptr) break;
684 }
685 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200686 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700687 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500688 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700689 break;
François Gaffie2110e042015-03-24 08:41:51 +0100690
691 case AUDIO_SOURCE_VOICE_COMMUNICATION:
692 // Allow only use of devices on primary input if in call and HAL does not support routing
693 // to voice call path.
694 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700695 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
696 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
Eric Laurent5569bf42023-08-30 15:53:55 +0200697 if (!availablePrimaryDevices.isEmpty()) {
698 availableDevices = availablePrimaryDevices;
699 } else {
700 ALOGE("%s, AUDIO_SOURCE_VOICE_COMMUNICATION: Primary devices not found", __func__);
701 }
François Gaffie2110e042015-03-24 08:41:51 +0100702 }
703
Eric Laurent2517af32020-11-25 15:31:27 +0100704 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100705 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700706 device = availableDevices.getDevice(
707 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
708 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100709 break;
710 }
Eric Laurent2517af32020-11-25 15:31:27 +0100711 }
712 switch (commDeviceType) {
Eric Laurent2517af32020-11-25 15:31:27 +0100713 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700714 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100715 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
716 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100717 break;
Eric Laurentcedd5b52023-03-22 00:03:31 +0000718 case AUDIO_DEVICE_OUT_BLE_HEADSET:
719 device = availableDevices.getDevice(
720 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
721 if (device != nullptr) {
722 break;
723 }
724 ALOGE("%s LE Audio selected for communication but input device not available",
725 __func__);
726 FALLTHROUGH_INTENDED;
Eric Laurent2517af32020-11-25 15:31:27 +0100727 default: // FORCE_NONE
728 device = availableDevices.getFirstExistingDevice({
729 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500730 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
731 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100732 break;
François Gaffie2110e042015-03-24 08:41:51 +0100733 }
734 break;
735
736 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800737 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500738 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
739 device = availableDevices.getDevice(
740 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
741 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700742 }
wei wanga7020522020-12-10 21:36:11 -0500743 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
744 // because sometimes user want to do voice search by bt remote
745 // even if BUILDIN_MIC is available.
746 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200747 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500748 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
749 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
750
751 break;
752 case AUDIO_SOURCE_HOTWORD:
753 // We should not use primary output criteria for Hotword but rather limit
754 // to devices attached to the same HW module as the build in mic
Eric Laurent5569bf42023-08-30 15:53:55 +0200755 if (!availablePrimaryDevices.isEmpty()) {
756 availableDevices = availablePrimaryDevices;
757 } else {
758 ALOGE("%s, AUDIO_SOURCE_HOTWORD: Primary devices not found", __func__);
759 }
Eric Laurent2517af32020-11-25 15:31:27 +0100760 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700761 device = availableDevices.getDevice(
762 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
763 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100764 }
jiabinf502d152019-08-07 14:43:33 -0700765 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200766 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700767 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
768 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100769 break;
770 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700771 // For a device without built-in mic, adding usb device
772 device = availableDevices.getFirstExistingDevice({
773 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
774 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100775 break;
776 case AUDIO_SOURCE_VOICE_DOWNLINK:
777 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700778 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700779 device = availableDevices.getDevice(
780 AUDIO_DEVICE_IN_VOICE_CALL, 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_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700783 device = availableDevices.getFirstExistingDevice({
784 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500785 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
786 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800787 break;
François Gaffie2110e042015-03-24 08:41:51 +0100788 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700789 device = availableDevices.getDevice(
790 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100791 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800792 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700793 device = availableDevices.getDevice(
794 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100795 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800796 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700797 device = availableDevices.getDevice(
798 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800799 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800800 case AUDIO_SOURCE_ULTRASOUND:
801 device = availableDevices.getFirstExistingDevice({
802 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
803 break;
François Gaffie2110e042015-03-24 08:41:51 +0100804 default:
805 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
806 break;
807 }
jiabinf502d152019-08-07 14:43:33 -0700808 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700809 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700810 device = availableDevices.getDevice(
811 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
812 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700813 "getDeviceForInputSource() no default device defined");
814 }
Eric Laurent2517af32020-11-25 15:31:27 +0100815
jiabinf502d152019-08-07 14:43:33 -0700816 ALOGV_IF(device != nullptr,
817 "getDeviceForInputSource()input source %d, device %08x",
818 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100819 return device;
820}
821
jiabin29230182023-04-04 21:02:36 +0000822void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
823 strategy->setDeviceTypes(devices.types());
824 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100825}
826
Eric Laurent2517af32020-11-25 15:31:27 +0100827product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
828 for (const auto& strategyMap : mLegacyStrategyMap) {
829 if (strategyMap.second == legacyStrategy) {
830 return strategyMap.first;
831 }
832 }
833 return PRODUCT_STRATEGY_NONE;
834}
François Gaffiedc7553f2018-11-02 10:39:57 +0100835
Eric Laurent2517af32020-11-25 15:31:27 +0100836audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
837 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
838 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
839 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
840 availableOutputDevices, strategy);
841 if (devices.size() > 0) {
842 return devices[0]->type();
843 }
844 return AUDIO_DEVICE_NONE;
845}
846
Eric Laurent2517af32020-11-25 15:31:27 +0100847DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100848 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
849
850 // Take context into account to remap product strategy before
851 // checking preferred device for strategy and applying default routing rules
852 strategy = remapStrategyFromContext(strategy, outputs);
853
Eric Laurent2517af32020-11-25 15:31:27 +0100854 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
855 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
856
Eric Laurent6cd5f902021-03-23 18:29:58 +0100857 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100858
Eric Laurent6cd5f902021-03-23 18:29:58 +0100859 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100860
Eric Laurent2517af32020-11-25 15:31:27 +0100861 // check if this strategy has a preferred device that is available,
862 // if yes, give priority to it.
863 DeviceVector preferredAvailableDevVec =
864 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
865 if (!preferredAvailableDevVec.isEmpty()) {
866 return preferredAvailableDevVec;
867 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700868
jiabind0a8d6f2022-09-19 20:32:01 +0000869 // Remove all disabled devices from the available device list.
870 DeviceVector disabledDevVec =
871 getDisabledDevicesForProductStrategy(availableOutputDevices, strategy);
872 availableOutputDevices.remove(disabledDevVec);
873
jiabinf502d152019-08-07 14:43:33 -0700874 return getDevicesForStrategyInt(legacyStrategy,
875 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100876 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100877}
878
879DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
880 const sp<DeviceDescriptor> &preferredDevice,
881 bool fromCache) const
882{
883 // First check for explict routing device
884 if (preferredDevice != nullptr) {
885 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
886 return DeviceVector(preferredDevice);
887 }
François Gaffiec005e562018-11-06 15:04:49 +0100888 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700889 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100890 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100891 //
892 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
893 // be by APM?
894 //
François Gaffiec005e562018-11-06 15:04:49 +0100895 // Honor explicit routing requests only if all active clients have a preferred route in which
896 // case the last active client route is used
897 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
898 if (device != nullptr) {
899 return DeviceVector(device);
900 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100901
902 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
903}
904
905DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
906{
907 auto attributes = getAttributesForStreamType(stream);
908 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
909}
910
911sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800912 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200913 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800914 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100915{
François Gaffiec005e562018-11-06 15:04:49 +0100916 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700917 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100918 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100919 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100920
François Gaffiedc7553f2018-11-02 10:39:57 +0100921 //
François Gaffiec005e562018-11-06 15:04:49 +0100922 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
923 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100924 //
François Gaffiec005e562018-11-06 15:04:49 +0100925 // Honor explicit routing requests only if all active clients have a preferred route in which
926 // case the last active client route is used
927 sp<DeviceDescriptor> device =
928 findPreferredDevice(inputs, attr.source, availableInputDevices);
929 if (device != nullptr) {
930 return device;
931 }
932
Jan Sebechlebsky28ed9452022-09-15 17:57:42 +0200933 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800934 availableInputDevices,
935 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200936 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800937 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100938 if (device != nullptr) {
939 return device;
940 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100941
jiabinf502d152019-08-07 14:43:33 -0700942 device = getDeviceForInputSource(attr.source);
943 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
944 // Return immediately if the device is null or it is not a remote submix device.
945 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100946 }
jiabinf502d152019-08-07 14:43:33 -0700947
948 // For remote submix device, try to find the device by address.
949 address = "0";
950 std::size_t pos;
951 std::string tags { attr.tags };
952 if ((pos = tags.find("addr=")) != std::string::npos) {
953 address = tags.substr(pos + std::strlen("addr="));
954 }
955 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100956 String8(address.c_str()),
957 AUDIO_FORMAT_DEFAULT);
958}
959
Mikhail Naganov9e459d72023-05-05 17:36:39 -0700960} // namespace android::audio_policy