blob: e06bbb3a05e90a59495a9cf798d60c4cbea6f398 [file] [log] [blame]
François Gaffie2110e042015-03-24 08:41:51 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "APM::AudioPolicyEngine"
18//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27#include "Engine.h"
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070028#include <android-base/macros.h>
François Gaffie2110e042015-03-24 08:41:51 +010029#include <AudioPolicyManagerObserver.h>
jiabine1284852019-09-11 10:15:46 -070030#include <PolicyAudioPort.h>
François Gaffie2110e042015-03-24 08:41:51 +010031#include <IOProfile.h>
François Gaffiec005e562018-11-06 15:04:49 +010032#include <AudioIODescriptorInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010033#include <policy.h>
jiabin9a3361e2019-10-01 09:38:30 -070034#include <media/AudioContainers.h>
François Gaffie2110e042015-03-24 08:41:51 +010035#include <utils/String8.h>
36#include <utils/Log.h>
37
Mikhail Naganov9e459d72023-05-05 17:36:39 -070038namespace android::audio_policy {
François Gaffie2110e042015-03-24 08:41:51 +010039
François Gaffiedc7553f2018-11-02 10:39:57 +010040struct legacy_strategy_map { const char *name; legacy_strategy id; };
Mikhail Naganovf9003622020-03-10 13:15:08 -070041static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
42 static const std::vector<legacy_strategy_map> legacyStrategy = {
43 { "STRATEGY_NONE", STRATEGY_NONE },
44 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
45 { "STRATEGY_PHONE", STRATEGY_PHONE },
46 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
47 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
48 { "STRATEGY_DTMF", STRATEGY_DTMF },
49 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
50 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
51 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
52 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
53 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
54 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
55 };
56 return legacyStrategy;
57}
François Gaffiedc7553f2018-11-02 10:39:57 +010058
Mikhail Naganov9e459d72023-05-05 17:36:39 -070059status_t Engine::loadFromHalConfigWithFallback(
60 const media::audio::common::AudioHalEngineConfig& aidlConfig) {
61 return loadWithFallback(aidlConfig);
62}
63
Mikhail Naganovabb04782023-05-02 13:56:01 -070064status_t Engine::loadFromXmlConfigWithFallback(const std::string& xmlFilePath) {
Mikhail Naganov9e459d72023-05-05 17:36:39 -070065 return loadWithFallback(xmlFilePath);
66}
67
68template<typename T>
69status_t Engine::loadWithFallback(const T& configSource) {
70 auto result = EngineBase::loadAudioPolicyEngineConfig(configSource);
François Gaffiedc7553f2018-11-02 10:39:57 +010071 ALOGE_IF(result.nbSkippedElement != 0,
72 "Policy Engine configuration is partially invalid, skipped %zu elements",
73 result.nbSkippedElement);
74
Mikhail Naganovf9003622020-03-10 13:15:08 -070075 auto legacyStrategy = getLegacyStrategy();
76 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010077 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010078 }
Mikhail Naganovabb04782023-05-02 13:56:01 -070079
80 return OK;
François Gaffie2110e042015-03-24 08:41:51 +010081}
82
Mikhail Naganov9e459d72023-05-05 17:36:39 -070083
François Gaffie2110e042015-03-24 08:41:51 +010084status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
85{
86 switch(usage) {
87 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
88 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
89 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +000090 ALOGW("setForceUse() invalid config %d for COMMUNICATION", config);
François Gaffie2110e042015-03-24 08:41:51 +010091 return BAD_VALUE;
92 }
François Gaffie2110e042015-03-24 08:41:51 +010093 break;
94 case AUDIO_POLICY_FORCE_FOR_MEDIA:
95 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
96 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
97 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
98 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
99 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000100 ALOGW("setForceUse() invalid config %d for MEDIA", config);
François Gaffie2110e042015-03-24 08:41:51 +0100101 return BAD_VALUE;
102 }
François Gaffie2110e042015-03-24 08:41:51 +0100103 break;
104 case AUDIO_POLICY_FORCE_FOR_RECORD:
105 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
106 config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000107 ALOGW("setForceUse() invalid config %d for RECORD", config);
François Gaffie2110e042015-03-24 08:41:51 +0100108 return BAD_VALUE;
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 break;
111 case AUDIO_POLICY_FORCE_FOR_DOCK:
112 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
113 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
114 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
115 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
116 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000117 ALOGW("setForceUse() invalid config %d for DOCK", config);
118 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100119 }
François Gaffie2110e042015-03-24 08:41:51 +0100120 break;
121 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
122 if (config != AUDIO_POLICY_FORCE_NONE &&
123 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000124 ALOGW("setForceUse() invalid config %d for SYSTEM", config);
125 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100126 }
François Gaffie2110e042015-03-24 08:41:51 +0100127 break;
128 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
129 if (config != AUDIO_POLICY_FORCE_NONE &&
130 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800131 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000132 return BAD_VALUE;
Phil Burk09bc4612016-02-24 15:58:15 -0800133 }
Phil Burk09bc4612016-02-24 15:58:15 -0800134 break;
135 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
136 if (config != AUDIO_POLICY_FORCE_NONE &&
137 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700138 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
139 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800140 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
141 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100142 }
François Gaffie2110e042015-03-24 08:41:51 +0100143 break;
Jack He96117ae2018-02-12 20:52:53 -0800144 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
145 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000146 ALOGW("setForceUse() invalid config %d for VIBRATE_RINGING", config);
Jack He96117ae2018-02-12 20:52:53 -0800147 return BAD_VALUE;
148 }
Jack He96117ae2018-02-12 20:52:53 -0800149 break;
François Gaffie2110e042015-03-24 08:41:51 +0100150 default:
151 ALOGW("setForceUse() invalid usage %d", usage);
Lorena Torres-Huerta922da312022-07-21 08:07:32 +0000152 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100153 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100154 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100155}
156
Eric Laurent2802b862021-03-01 09:36:16 +0100157void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
158 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100159 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800160{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100161 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
162
François Gaffie2110e042015-03-24 08:41:51 +0100163 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100164 case STRATEGY_SONIFICATION_RESPECTFUL: {
165 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800166 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700167 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700168 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100169 }
Eric Laurent2802b862021-03-01 09:36:16 +0100170 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100171 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100172 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100173 // Force use of only devices on primary output if:
174 // - in call AND
175 // - cannot route from voice call RX OR
176 // - audio HAL version is < 3.0 and TX device is on the primary HW module
177 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
Eric Laurentcedd5b52023-03-22 00:03:31 +0000178 audio_devices_t txDevice = AUDIO_DEVICE_NONE;
179 sp<DeviceDescriptor> txDeviceDesc =
180 getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
181 if (txDeviceDesc != nullptr) {
182 txDevice = txDeviceDesc->type();
183 }
François Gaffie2110e042015-03-24 08:41:51 +0100184 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700185 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700186 DeviceVector availPrimaryInputDevices =
187 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800188
189 // TODO: getPrimaryOutput return only devices from first module in
190 // audio_policy_configuration.xml, hearing aid is not there, but it's
191 // a primary device
192 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700193 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700194 primaryOutput->supportedDevices().types());
195 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700196 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100197
jiabinf502d152019-08-07 14:43:33 -0700198 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100199 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
200 ((availPrimaryInputDevices.getDevice(
201 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
202 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700203 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100204 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200205
Eric Laurent97cec6f2021-05-20 13:52:47 +0200206 }
207 // Do not use A2DP devices when in call but use them when not in call
208 // (e.g for voice mail playback)
209 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200210 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
211 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
212 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100213 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200214 // If connected to a dock, never use the device speaker for calls
215 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
216 .isEmpty()) {
217 availableOutputDevices.remove(
218 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
219 }
Eric Laurent2802b862021-03-01 09:36:16 +0100220 } break;
221 case STRATEGY_ACCESSIBILITY: {
222 // do not route accessibility prompts to a digital output currently configured with a
223 // compressed format as they would likely not be mixed and dropped.
224 for (size_t i = 0; i < outputs.size(); i++) {
225 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
226 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
227 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
228 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800229 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100230 }
231 }
232 } break;
233 default:
234 break;
235 }
236}
237
Eric Laurent6cd5f902021-03-23 18:29:58 +0100238product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
239 const SwAudioOutputCollection &outputs) const {
240 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
241 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
242
243 if (isInCall()) {
244 switch (legacyStrategy) {
245 case STRATEGY_ACCESSIBILITY:
246 case STRATEGY_DTMF:
247 case STRATEGY_MEDIA:
248 case STRATEGY_SONIFICATION:
249 case STRATEGY_SONIFICATION_RESPECTFUL:
250 legacyStrategy = STRATEGY_PHONE;
251 break;
252
253 default:
254 return strategy;
255 }
256 } else {
257 switch (legacyStrategy) {
258 case STRATEGY_SONIFICATION_RESPECTFUL:
259 case STRATEGY_SONIFICATION:
260 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
261 legacyStrategy = STRATEGY_PHONE;
262 }
263 break;
264
265 case STRATEGY_ACCESSIBILITY:
266 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
267 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
268 legacyStrategy = STRATEGY_SONIFICATION;
269 }
270 break;
271
272 default:
273 return strategy;
274 }
275 }
276 return getProductStrategyFromLegacy(legacyStrategy);
277}
278
Eric Laurent2802b862021-03-01 09:36:16 +0100279DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
280 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100281 const SwAudioOutputCollection &outputs) const
282{
283 DeviceVector devices;
284
285 switch (strategy) {
286
287 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
288 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
289 break;
290
Eric Laurent2802b862021-03-01 09:36:16 +0100291 case STRATEGY_PHONE: {
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000292 // TODO(b/243670205): remove this logic that gives preference to last removable devices
293 // once a UX decision has been made
Eric Laurentc0697592021-05-10 11:45:22 +0200294 devices = availableOutputDevices.getFirstDevicesFromTypes(
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000295 getLastRemovableMediaDevices(GROUP_NONE, {
296 // excluding HEARING_AID and BLE_HEADSET because Dialer uses
297 // setCommunicationDevice to select them explicitly
298 AUDIO_DEVICE_OUT_HEARING_AID,
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530299 AUDIO_DEVICE_OUT_BLE_HEADSET,
300 AUDIO_DEVICE_OUT_AUX_DIGITAL
Lorena Torres-Huerta07fd48a2022-08-24 19:18:53 +0000301 }));
Eric Laurent2517af32020-11-25 15:31:27 +0100302 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100303 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin29230182023-04-04 21:02:36 +0000304 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
305 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100306 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100307
308 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100309 case STRATEGY_ENFORCED_AUDIBLE:
310 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
311 // except:
312 // - when in call where it doesn't default to STRATEGY_PHONE behavior
313 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
314
315 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100316 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
Jean-Michel Trivi00afde12023-02-01 20:46:32 +0000317 // favor dock over speaker when available
318 devices = availableOutputDevices.getFirstDevicesFromTypes({
319 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100320 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800321
322 // if SCO headset is connected and we are told to use it, play ringtone over
323 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700324 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700325 DeviceVector devices2;
326 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
327 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
328 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800329 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100330 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800331 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100332 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800333 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700334 if (!devices2.isEmpty()) {
335 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800336 break;
337 }
338 }
339 }
340 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100341 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
342 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700343 if (strategy == STRATEGY_SONIFICATION) {
344 devices.replaceDevicesByType(
345 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700346 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700347 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800348 }
jiabinf502d152019-08-07 14:43:33 -0700349 if (!devices2.isEmpty()) {
350 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800351 break;
352 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800353 }
354 }
François Gaffie2110e042015-03-24 08:41:51 +0100355 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700356 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100357
Eric Laurent6cd5f902021-03-23 18:29:58 +0100358 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100359 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100360 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100361 case STRATEGY_REROUTING:
362 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700363 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100364 if (strategy != STRATEGY_SONIFICATION) {
365 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700366 sp<DeviceDescriptor> remoteSubmix;
367 if ((remoteSubmix = availableOutputDevices.getDevice(
368 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
369 AUDIO_FORMAT_DEFAULT)) != nullptr) {
370 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100371 }
372 }
Eric Laurenta9986862020-10-07 08:42:28 -0700373
jiabinf502d152019-08-07 14:43:33 -0700374 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100375 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700376 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100377 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100378
379 // LE audio broadcast device is only used if:
380 // - No call is active
381 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
382 // OR the LE audio unicast device is not active
383 if (devices2.isEmpty() && !isInCall()
384 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
385 legacy_strategy topActiveStrategy = STRATEGY_NONE;
386 for (const auto &ps : getOrderedProductStrategies()) {
387 if (outputs.isStrategyActive(ps)) {
388 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
389 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
390 break;
391 }
392 }
393
394 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
395 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
396 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
397 devices2 =
398 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
399 }
400 }
401
Baekgyeong Kim08451522019-11-01 20:40:02 +0900402 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530403 std::vector<audio_devices_t> excludedDevices;
404 // no sonification on aux digital (e.g. HDMI)
405 if (strategy == STRATEGY_SONIFICATION) {
406 excludedDevices.push_back(AUDIO_DEVICE_OUT_AUX_DIGITAL);
407 }
Eric Laurenta9986862020-10-07 08:42:28 -0700408 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900409 // Get the last connected device of wired and bluetooth a2dp
410 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530411 getLastRemovableMediaDevices(GROUP_NONE, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900412 } else {
413 // Get the last connected device of wired except bluetooth a2dp
414 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
Jaideep Sharmafd79c292023-10-10 00:11:22 +0530415 getLastRemovableMediaDevices(GROUP_WIRED, excludedDevices));
Baekgyeong Kim08451522019-11-01 20:40:02 +0900416 }
François Gaffie2110e042015-03-24 08:41:51 +0100417 }
jiabinf502d152019-08-07 14:43:33 -0700418 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100419 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700420 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700421 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100422 }
jiabinf502d152019-08-07 14:43:33 -0700423 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100424 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
425 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100426 }
jiabinf502d152019-08-07 14:43:33 -0700427 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100428 if (strategy == STRATEGY_MEDIA) {
429 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700430 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800431 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
432 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
433 });
François Gaffie2110e042015-03-24 08:41:51 +0100434 }
435
jiabinf502d152019-08-07 14:43:33 -0700436 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100437 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
438 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700439 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100440
441 // If hdmi system audio mode is on, remove speaker out of output list.
442 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100443 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100444 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700445 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100446 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700447
Eric Laurent6cd5f902021-03-23 18:29:58 +0100448 bool mediaActiveLocally =
449 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
450 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
451 || outputs.isActiveLocally(
452 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
453 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800454
455 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
456 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700457 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100458 // - for STRATEGY_SONIFICATION_RESPECTFUL:
459 // if no media is playing on the device, check for mandatory use of "safe" speaker
460 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800461 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100462 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700463 devices.replaceDevicesByType(
464 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700465 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700466 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700467 }
François Gaffie2110e042015-03-24 08:41:51 +0100468 } break;
469
Eric Laurent21777f82019-12-06 18:12:06 -0800470 case STRATEGY_CALL_ASSISTANT:
471 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
472 break;
473
Eric Laurentd9766932020-08-07 14:01:22 -0700474 case STRATEGY_NONE:
475 // Happens when internal strategies are processed ("rerouting", "patch"...)
476 break;
477
François Gaffie2110e042015-03-24 08:41:51 +0100478 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700479 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100480 break;
481 }
482
jiabinf502d152019-08-07 14:43:33 -0700483 if (devices.isEmpty()) {
jiabind0a8d6f2022-09-19 20:32:01 +0000484 ALOGI("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700485 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
486 if (defaultOutputDevice != nullptr) {
487 devices.add(defaultOutputDevice);
488 }
489 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700490 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700491 }
jiabinf502d152019-08-07 14:43:33 -0700492
Eric Laurentd9766932020-08-07 14:01:22 -0700493 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800494 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700495 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100496}
497
jiabin14662b52023-03-06 21:35:29 +0000498DeviceVector Engine::getPreferredAvailableDevicesForInputSource(
499 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
500 DeviceVector preferredAvailableDevVec = {};
501 AudioDeviceTypeAddrVector preferredDevices;
502 const status_t status = getDevicesForRoleAndCapturePreset(
503 inputSource, DEVICE_ROLE_PREFERRED, preferredDevices);
504 if (status == NO_ERROR) {
505 // Only use preferred devices when they are all available.
506 preferredAvailableDevVec =
507 availableInputDevices.getDevicesFromDeviceTypeAddrVec(preferredDevices);
508 if (preferredAvailableDevVec.size() == preferredDevices.size()) {
509 ALOGVV("%s using pref device %s for source %u",
510 __func__, preferredAvailableDevVec.toString().c_str(), inputSource);
511 return preferredAvailableDevVec;
512 }
513 }
514 return preferredAvailableDevVec;
515}
516
517DeviceVector Engine::getDisabledDevicesForInputSource(
518 const DeviceVector& availableInputDevices, audio_source_t inputSource) const {
519 DeviceVector disabledDevices = {};
520 AudioDeviceTypeAddrVector disabledDevicesTypeAddr;
521 const status_t status = getDevicesForRoleAndCapturePreset(
522 inputSource, DEVICE_ROLE_DISABLED, disabledDevicesTypeAddr);
523 if (status == NO_ERROR) {
524 disabledDevices =
525 availableInputDevices.getDevicesFromDeviceTypeAddrVec(disabledDevicesTypeAddr);
526 }
527 return disabledDevices;
528}
François Gaffie2110e042015-03-24 08:41:51 +0100529
jiabinf502d152019-08-07 14:43:33 -0700530sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100531{
Eric Laurentaf377772019-03-29 14:50:21 -0700532 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
533 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100534 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700535 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700536 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700537 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
538 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700539 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100540
Eric Laurentdc95a252018-04-12 12:46:56 -0700541 // when a call is active, force device selection to match source VOICE_COMMUNICATION
542 // for most other input sources to avoid rerouting call TX audio
543 if (isInCall()) {
544 switch (inputSource) {
545 case AUDIO_SOURCE_DEFAULT:
546 case AUDIO_SOURCE_MIC:
547 case AUDIO_SOURCE_VOICE_RECOGNITION:
548 case AUDIO_SOURCE_UNPROCESSED:
549 case AUDIO_SOURCE_HOTWORD:
550 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800551 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800552 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700553 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
554 break;
555 default:
556 break;
557 }
558 }
559
jiabin14662b52023-03-06 21:35:29 +0000560 // Use the preferred device for the input source if it is available.
561 DeviceVector preferredInputDevices = getPreferredAvailableDevicesForInputSource(
562 availableDevices, inputSource);
563 if (!preferredInputDevices.isEmpty()) {
564 // Currently, only support single device for input. The public JAVA API also only
565 // support setting single device as preferred device. In that case, returning the
566 // first device is OK here.
567 return preferredInputDevices[0];
568 }
569 // Remove the disabled device for the input source from the available input device list.
570 DeviceVector disabledInputDevices = getDisabledDevicesForInputSource(
571 availableDevices, inputSource);
572 availableDevices.remove(disabledInputDevices);
573
Eric Laurent2517af32020-11-25 15:31:27 +0100574 audio_devices_t commDeviceType =
575 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
576
François Gaffie2110e042015-03-24 08:41:51 +0100577 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100578 case AUDIO_SOURCE_DEFAULT:
579 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700580 device = availableDevices.getDevice(
581 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
582 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100583 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700584 device = availableDevices.getDevice(
585 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
586 if (device != nullptr) break;
587 }
588 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200589 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700590 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500591 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700592 break;
François Gaffie2110e042015-03-24 08:41:51 +0100593
594 case AUDIO_SOURCE_VOICE_COMMUNICATION:
595 // Allow only use of devices on primary input if in call and HAL does not support routing
596 // to voice call path.
597 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700598 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
599 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700600 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700601 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100602 }
603
Eric Laurent2517af32020-11-25 15:31:27 +0100604 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100605 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700606 device = availableDevices.getDevice(
607 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
608 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100609 break;
610 }
Eric Laurent2517af32020-11-25 15:31:27 +0100611 }
612 switch (commDeviceType) {
Eric Laurent2517af32020-11-25 15:31:27 +0100613 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700614 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100615 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
616 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100617 break;
Eric Laurentcedd5b52023-03-22 00:03:31 +0000618 case AUDIO_DEVICE_OUT_BLE_HEADSET:
619 device = availableDevices.getDevice(
620 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
621 if (device != nullptr) {
622 break;
623 }
624 ALOGE("%s LE Audio selected for communication but input device not available",
625 __func__);
626 FALLTHROUGH_INTENDED;
Eric Laurent2517af32020-11-25 15:31:27 +0100627 default: // FORCE_NONE
628 device = availableDevices.getFirstExistingDevice({
629 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500630 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
631 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100632 break;
François Gaffie2110e042015-03-24 08:41:51 +0100633 }
634 break;
635
636 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800637 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500638 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
639 device = availableDevices.getDevice(
640 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
641 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700642 }
wei wanga7020522020-12-10 21:36:11 -0500643 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
644 // because sometimes user want to do voice search by bt remote
645 // even if BUILDIN_MIC is available.
646 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200647 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500648 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
649 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
650
651 break;
652 case AUDIO_SOURCE_HOTWORD:
653 // We should not use primary output criteria for Hotword but rather limit
654 // to devices attached to the same HW module as the build in mic
655 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
656 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100657 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700658 device = availableDevices.getDevice(
659 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
660 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100661 }
jiabinf502d152019-08-07 14:43:33 -0700662 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200663 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700664 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
665 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100666 break;
667 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700668 // For a device without built-in mic, adding usb device
669 device = availableDevices.getFirstExistingDevice({
670 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
671 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100672 break;
673 case AUDIO_SOURCE_VOICE_DOWNLINK:
674 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700675 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700676 device = availableDevices.getDevice(
677 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100678 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800679 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700680 device = availableDevices.getFirstExistingDevice({
681 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500682 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
683 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800684 break;
François Gaffie2110e042015-03-24 08:41:51 +0100685 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700686 device = availableDevices.getDevice(
687 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100688 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800689 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700690 device = availableDevices.getDevice(
691 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100692 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800693 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700694 device = availableDevices.getDevice(
695 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800696 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800697 case AUDIO_SOURCE_ULTRASOUND:
698 device = availableDevices.getFirstExistingDevice({
699 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
700 break;
François Gaffie2110e042015-03-24 08:41:51 +0100701 default:
702 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
703 break;
704 }
jiabinf502d152019-08-07 14:43:33 -0700705 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700706 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700707 device = availableDevices.getDevice(
708 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
709 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700710 "getDeviceForInputSource() no default device defined");
711 }
Eric Laurent2517af32020-11-25 15:31:27 +0100712
jiabinf502d152019-08-07 14:43:33 -0700713 ALOGV_IF(device != nullptr,
714 "getDeviceForInputSource()input source %d, device %08x",
715 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100716 return device;
717}
718
jiabin29230182023-04-04 21:02:36 +0000719void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
720 strategy->setDeviceTypes(devices.types());
721 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100722}
723
Eric Laurent2517af32020-11-25 15:31:27 +0100724product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
725 for (const auto& strategyMap : mLegacyStrategyMap) {
726 if (strategyMap.second == legacyStrategy) {
727 return strategyMap.first;
728 }
729 }
730 return PRODUCT_STRATEGY_NONE;
731}
François Gaffiedc7553f2018-11-02 10:39:57 +0100732
Eric Laurent2517af32020-11-25 15:31:27 +0100733audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
734 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
735 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
736 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
737 availableOutputDevices, strategy);
738 if (devices.size() > 0) {
739 return devices[0]->type();
740 }
741 return AUDIO_DEVICE_NONE;
742}
743
Eric Laurent2517af32020-11-25 15:31:27 +0100744DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100745 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
746
747 // Take context into account to remap product strategy before
748 // checking preferred device for strategy and applying default routing rules
749 strategy = remapStrategyFromContext(strategy, outputs);
750
Eric Laurent2517af32020-11-25 15:31:27 +0100751 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
752 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
753
Eric Laurent6cd5f902021-03-23 18:29:58 +0100754 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100755
Eric Laurent6cd5f902021-03-23 18:29:58 +0100756 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100757
Eric Laurent2517af32020-11-25 15:31:27 +0100758 // check if this strategy has a preferred device that is available,
759 // if yes, give priority to it.
760 DeviceVector preferredAvailableDevVec =
761 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
762 if (!preferredAvailableDevVec.isEmpty()) {
763 return preferredAvailableDevVec;
764 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700765
jiabind0a8d6f2022-09-19 20:32:01 +0000766 // Remove all disabled devices from the available device list.
767 DeviceVector disabledDevVec =
768 getDisabledDevicesForProductStrategy(availableOutputDevices, strategy);
769 availableOutputDevices.remove(disabledDevVec);
770
jiabinf502d152019-08-07 14:43:33 -0700771 return getDevicesForStrategyInt(legacyStrategy,
772 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100773 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100774}
775
776DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
777 const sp<DeviceDescriptor> &preferredDevice,
778 bool fromCache) const
779{
780 // First check for explict routing device
781 if (preferredDevice != nullptr) {
782 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
783 return DeviceVector(preferredDevice);
784 }
François Gaffiec005e562018-11-06 15:04:49 +0100785 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700786 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100787 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100788 //
789 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
790 // be by APM?
791 //
François Gaffiec005e562018-11-06 15:04:49 +0100792 // Honor explicit routing requests only if all active clients have a preferred route in which
793 // case the last active client route is used
794 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
795 if (device != nullptr) {
796 return DeviceVector(device);
797 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100798
799 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
800}
801
802DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
803{
804 auto attributes = getAttributesForStreamType(stream);
805 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
806}
807
808sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800809 uid_t uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200810 audio_session_t session,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800811 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100812{
François Gaffiec005e562018-11-06 15:04:49 +0100813 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700814 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100815 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100816 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100817
François Gaffiedc7553f2018-11-02 10:39:57 +0100818 //
François Gaffiec005e562018-11-06 15:04:49 +0100819 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
820 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100821 //
François Gaffiec005e562018-11-06 15:04:49 +0100822 // Honor explicit routing requests only if all active clients have a preferred route in which
823 // case the last active client route is used
824 sp<DeviceDescriptor> device =
825 findPreferredDevice(inputs, attr.source, availableInputDevices);
826 if (device != nullptr) {
827 return device;
828 }
829
Jan Sebechlebsky28ed9452022-09-15 17:57:42 +0200830 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800831 availableInputDevices,
832 uid,
Jan Sebechlebsky1a80c062022-08-09 15:21:18 +0200833 session,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800834 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100835 if (device != nullptr) {
836 return device;
837 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100838
jiabinf502d152019-08-07 14:43:33 -0700839 device = getDeviceForInputSource(attr.source);
840 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
841 // Return immediately if the device is null or it is not a remote submix device.
842 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100843 }
jiabinf502d152019-08-07 14:43:33 -0700844
845 // For remote submix device, try to find the device by address.
846 address = "0";
847 std::size_t pos;
848 std::string tags { attr.tags };
849 if ((pos = tags.find("addr=")) != std::string::npos) {
850 address = tags.substr(pos + std::strlen("addr="));
851 }
852 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100853 String8(address.c_str()),
854 AUDIO_FORMAT_DEFAULT);
855}
856
Mikhail Naganov9e459d72023-05-05 17:36:39 -0700857} // namespace android::audio_policy