blob: e72249fb12d888444e54a5b592a89b0c58365243 [file] [log] [blame]
François Gaffie2110e042015-03-24 08:41:51 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "APM::AudioPolicyEngine"
18//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27#include "Engine.h"
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070028#include <android-base/macros.h>
François Gaffie2110e042015-03-24 08:41:51 +010029#include <AudioPolicyManagerObserver.h>
jiabine1284852019-09-11 10:15:46 -070030#include <PolicyAudioPort.h>
François Gaffie2110e042015-03-24 08:41:51 +010031#include <IOProfile.h>
François Gaffiec005e562018-11-06 15:04:49 +010032#include <AudioIODescriptorInterface.h>
François Gaffie2110e042015-03-24 08:41:51 +010033#include <policy.h>
jiabin9a3361e2019-10-01 09:38:30 -070034#include <media/AudioContainers.h>
François Gaffie2110e042015-03-24 08:41:51 +010035#include <utils/String8.h>
36#include <utils/Log.h>
37
38namespace android
39{
40namespace audio_policy
41{
42
François Gaffiedc7553f2018-11-02 10:39:57 +010043struct legacy_strategy_map { const char *name; legacy_strategy id; };
Mikhail Naganovf9003622020-03-10 13:15:08 -070044static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
45 static const std::vector<legacy_strategy_map> legacyStrategy = {
46 { "STRATEGY_NONE", STRATEGY_NONE },
47 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
48 { "STRATEGY_PHONE", STRATEGY_PHONE },
49 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
50 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
51 { "STRATEGY_DTMF", STRATEGY_DTMF },
52 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
53 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
54 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
55 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
56 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
57 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
58 };
59 return legacyStrategy;
60}
François Gaffiedc7553f2018-11-02 10:39:57 +010061
François Gaffie2110e042015-03-24 08:41:51 +010062Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010063{
François Gaffiedc7553f2018-11-02 10:39:57 +010064 auto result = EngineBase::loadAudioPolicyEngineConfig();
65 ALOGE_IF(result.nbSkippedElement != 0,
66 "Policy Engine configuration is partially invalid, skipped %zu elements",
67 result.nbSkippedElement);
68
Mikhail Naganovf9003622020-03-10 13:15:08 -070069 auto legacyStrategy = getLegacyStrategy();
70 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010071 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010072 }
73}
74
François Gaffie2110e042015-03-24 08:41:51 +010075status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
76{
77 switch(usage) {
78 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
79 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
80 config != AUDIO_POLICY_FORCE_NONE) {
81 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
82 return BAD_VALUE;
83 }
François Gaffie2110e042015-03-24 08:41:51 +010084 break;
85 case AUDIO_POLICY_FORCE_FOR_MEDIA:
86 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
87 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
88 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
89 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
90 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
91 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
92 return BAD_VALUE;
93 }
François Gaffie2110e042015-03-24 08:41:51 +010094 break;
95 case AUDIO_POLICY_FORCE_FOR_RECORD:
96 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
97 config != AUDIO_POLICY_FORCE_NONE) {
98 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
99 return BAD_VALUE;
100 }
François Gaffie2110e042015-03-24 08:41:51 +0100101 break;
102 case AUDIO_POLICY_FORCE_FOR_DOCK:
103 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
104 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
105 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
106 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
107 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
108 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 break;
111 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
112 if (config != AUDIO_POLICY_FORCE_NONE &&
113 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
114 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
115 }
François Gaffie2110e042015-03-24 08:41:51 +0100116 break;
117 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
118 if (config != AUDIO_POLICY_FORCE_NONE &&
119 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800120 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
121 }
Phil Burk09bc4612016-02-24 15:58:15 -0800122 break;
123 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
124 if (config != AUDIO_POLICY_FORCE_NONE &&
125 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
127 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800128 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
129 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100130 }
François Gaffie2110e042015-03-24 08:41:51 +0100131 break;
Jack He96117ae2018-02-12 20:52:53 -0800132 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
133 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
134 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
135 return BAD_VALUE;
136 }
Jack He96117ae2018-02-12 20:52:53 -0800137 break;
François Gaffie2110e042015-03-24 08:41:51 +0100138 default:
139 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800140 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100141 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100142 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100143}
144
Eric Laurent2802b862021-03-01 09:36:16 +0100145void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
146 DeviceVector& availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100147 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800148{
Eric Laurent6cd5f902021-03-23 18:29:58 +0100149 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
150
François Gaffie2110e042015-03-24 08:41:51 +0100151 switch (strategy) {
Eric Laurent2802b862021-03-01 09:36:16 +0100152 case STRATEGY_SONIFICATION_RESPECTFUL: {
153 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800154 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700155 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700156 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
François Gaffie2110e042015-03-24 08:41:51 +0100157 }
Eric Laurent2802b862021-03-01 09:36:16 +0100158 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100159 case STRATEGY_DTMF:
Eric Laurent2517af32020-11-25 15:31:27 +0100160 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100161 // Force use of only devices on primary output if:
162 // - in call AND
163 // - cannot route from voice call RX OR
164 // - audio HAL version is < 3.0 and TX device is on the primary HW module
165 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700166 audio_devices_t txDevice = getDeviceForInputSource(
167 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100168 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700169 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700170 DeviceVector availPrimaryInputDevices =
171 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800172
173 // TODO: getPrimaryOutput return only devices from first module in
174 // audio_policy_configuration.xml, hearing aid is not there, but it's
175 // a primary device
176 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700177 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700178 primaryOutput->supportedDevices().types());
179 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700180 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100181
jiabinf502d152019-08-07 14:43:33 -0700182 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurent2517af32020-11-25 15:31:27 +0100183 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
184 ((availPrimaryInputDevices.getDevice(
185 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
186 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700187 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100188 }
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200189
Eric Laurent97cec6f2021-05-20 13:52:47 +0200190 }
191 // Do not use A2DP devices when in call but use them when not in call
192 // (e.g for voice mail playback)
193 if (isInCall()) {
Eric Laurent8f2f4e92021-05-17 14:42:27 +0200194 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
195 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
196 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
François Gaffie2110e042015-03-24 08:41:51 +0100197 }
Eric Laurent802ad1d2022-07-01 16:54:43 +0200198 // If connected to a dock, never use the device speaker for calls
199 if (!availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET})
200 .isEmpty()) {
201 availableOutputDevices.remove(
202 availableOutputDevices.getDevicesFromTypes({AUDIO_DEVICE_OUT_SPEAKER}));
203 }
Eric Laurent2802b862021-03-01 09:36:16 +0100204 } break;
205 case STRATEGY_ACCESSIBILITY: {
206 // do not route accessibility prompts to a digital output currently configured with a
207 // compressed format as they would likely not be mixed and dropped.
208 for (size_t i = 0; i < outputs.size(); i++) {
209 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
210 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
211 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
212 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800213 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100214 }
215 }
216 } break;
217 default:
218 break;
219 }
220}
221
Eric Laurent6cd5f902021-03-23 18:29:58 +0100222product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
223 const SwAudioOutputCollection &outputs) const {
224 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
225 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
226
227 if (isInCall()) {
228 switch (legacyStrategy) {
229 case STRATEGY_ACCESSIBILITY:
230 case STRATEGY_DTMF:
231 case STRATEGY_MEDIA:
232 case STRATEGY_SONIFICATION:
233 case STRATEGY_SONIFICATION_RESPECTFUL:
234 legacyStrategy = STRATEGY_PHONE;
235 break;
236
237 default:
238 return strategy;
239 }
240 } else {
241 switch (legacyStrategy) {
242 case STRATEGY_SONIFICATION_RESPECTFUL:
243 case STRATEGY_SONIFICATION:
244 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
245 legacyStrategy = STRATEGY_PHONE;
246 }
247 break;
248
249 case STRATEGY_ACCESSIBILITY:
250 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
251 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
252 legacyStrategy = STRATEGY_SONIFICATION;
253 }
254 break;
255
256 default:
257 return strategy;
258 }
259 }
260 return getProductStrategyFromLegacy(legacyStrategy);
261}
262
Eric Laurent2802b862021-03-01 09:36:16 +0100263DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
264 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100265 const SwAudioOutputCollection &outputs) const
266{
267 DeviceVector devices;
268
269 switch (strategy) {
270
271 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
272 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
273 break;
274
Eric Laurent2802b862021-03-01 09:36:16 +0100275 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100276 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
277 if (!devices.isEmpty()) break;
Eric Laurentc0697592021-05-10 11:45:22 +0200278 devices = availableOutputDevices.getFirstDevicesFromTypes(
Eric Laurent34b09d02022-03-24 10:41:36 +0100279 getLastRemovableMediaDevices(GROUP_NONE, {AUDIO_DEVICE_OUT_BLE_HEADSET}));
Eric Laurent2517af32020-11-25 15:31:27 +0100280 if (!devices.isEmpty()) break;
Eric Laurent454a2cc2022-01-26 11:56:13 +0100281 devices = availableOutputDevices.getFirstDevicesFromTypes({
jiabin76e829d2023-04-04 21:02:36 +0000282 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_EARPIECE,
283 AUDIO_DEVICE_OUT_SPEAKER});
Eric Laurent2517af32020-11-25 15:31:27 +0100284 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100285
286 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100287 case STRATEGY_ENFORCED_AUDIBLE:
288 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
289 // except:
290 // - when in call where it doesn't default to STRATEGY_PHONE behavior
291 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
292
293 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100294 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700295 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100296 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800297
298 // if SCO headset is connected and we are told to use it, play ringtone over
299 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700300 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700301 DeviceVector devices2;
302 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
303 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
304 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800305 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100306 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800307 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100308 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800309 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700310 if (!devices2.isEmpty()) {
311 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800312 break;
313 }
314 }
315 }
316 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100317 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
318 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700319 if (strategy == STRATEGY_SONIFICATION) {
320 devices.replaceDevicesByType(
321 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700322 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700323 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800324 }
jiabinf502d152019-08-07 14:43:33 -0700325 if (!devices2.isEmpty()) {
326 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800327 break;
328 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800329 }
330 }
François Gaffie2110e042015-03-24 08:41:51 +0100331 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700332 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100333
Eric Laurent6cd5f902021-03-23 18:29:58 +0100334 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100335 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100336 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100337 case STRATEGY_REROUTING:
338 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700339 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100340 if (strategy != STRATEGY_SONIFICATION) {
341 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700342 sp<DeviceDescriptor> remoteSubmix;
343 if ((remoteSubmix = availableOutputDevices.getDevice(
344 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
345 AUDIO_FORMAT_DEFAULT)) != nullptr) {
346 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100347 }
348 }
Eric Laurenta9986862020-10-07 08:42:28 -0700349
jiabinf502d152019-08-07 14:43:33 -0700350 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100351 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700352 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100353 }
Eric Laurent96d1dda2022-03-14 17:14:19 +0100354
355 // LE audio broadcast device is only used if:
356 // - No call is active
357 // - either MEDIA or SONIFICATION_RESPECTFUL is the highest priority active strategy
358 // OR the LE audio unicast device is not active
359 if (devices2.isEmpty() && !isInCall()
360 && (strategy == STRATEGY_MEDIA || strategy == STRATEGY_SONIFICATION_RESPECTFUL)) {
361 legacy_strategy topActiveStrategy = STRATEGY_NONE;
362 for (const auto &ps : getOrderedProductStrategies()) {
363 if (outputs.isStrategyActive(ps)) {
364 topActiveStrategy = mLegacyStrategyMap.find(ps) != end(mLegacyStrategyMap) ?
365 mLegacyStrategyMap.at(ps) : STRATEGY_NONE;
366 break;
367 }
368 }
369
370 if (topActiveStrategy == STRATEGY_NONE || topActiveStrategy == STRATEGY_MEDIA
371 || topActiveStrategy == STRATEGY_SONIFICATION_RESPECTFUL
372 || !outputs.isAnyDeviceTypeActive(getAudioDeviceOutLeAudioUnicastSet())) {
373 devices2 =
374 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_BROADCAST);
375 }
376 }
377
Baekgyeong Kim08451522019-11-01 20:40:02 +0900378 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700379 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900380 // Get the last connected device of wired and bluetooth a2dp
381 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
382 getLastRemovableMediaDevices());
383 } else {
384 // Get the last connected device of wired except bluetooth a2dp
385 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
386 getLastRemovableMediaDevices(GROUP_WIRED));
387 }
François Gaffie2110e042015-03-24 08:41:51 +0100388 }
jiabinf502d152019-08-07 14:43:33 -0700389 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100390 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700391 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100392 }
jiabinf502d152019-08-07 14:43:33 -0700393 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100394 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700395 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700396 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100397 }
jiabinf502d152019-08-07 14:43:33 -0700398 if (devices2.isEmpty()) {
Eric Laurent454a2cc2022-01-26 11:56:13 +0100399 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
400 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100401 }
jiabinf502d152019-08-07 14:43:33 -0700402 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100403 if (strategy == STRATEGY_MEDIA) {
404 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700405 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800406 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
407 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
408 });
François Gaffie2110e042015-03-24 08:41:51 +0100409 }
410
jiabinf502d152019-08-07 14:43:33 -0700411 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100412 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
413 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700414 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100415
416 // If hdmi system audio mode is on, remove speaker out of output list.
417 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100418 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100419 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700420 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100421 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700422
Eric Laurent6cd5f902021-03-23 18:29:58 +0100423 bool mediaActiveLocally =
424 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
425 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
426 || outputs.isActiveLocally(
427 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
428 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800429
430 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
431 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700432 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100433 // - for STRATEGY_SONIFICATION_RESPECTFUL:
434 // if no media is playing on the device, check for mandatory use of "safe" speaker
435 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800436 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100437 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700438 devices.replaceDevicesByType(
439 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700440 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700441 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700442 }
François Gaffie2110e042015-03-24 08:41:51 +0100443 } break;
444
Eric Laurent21777f82019-12-06 18:12:06 -0800445 case STRATEGY_CALL_ASSISTANT:
446 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
447 break;
448
Eric Laurentd9766932020-08-07 14:01:22 -0700449 case STRATEGY_NONE:
450 // Happens when internal strategies are processed ("rerouting", "patch"...)
451 break;
452
François Gaffie2110e042015-03-24 08:41:51 +0100453 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700454 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100455 break;
456 }
457
jiabinf502d152019-08-07 14:43:33 -0700458 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700459 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700460 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
461 if (defaultOutputDevice != nullptr) {
462 devices.add(defaultOutputDevice);
463 }
464 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700465 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700466 }
jiabinf502d152019-08-07 14:43:33 -0700467
Eric Laurentd9766932020-08-07 14:01:22 -0700468 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800469 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700470 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100471}
472
473
jiabinf502d152019-08-07 14:43:33 -0700474sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100475{
Eric Laurentaf377772019-03-29 14:50:21 -0700476 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
477 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100478 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700479 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700480 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700481 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
482 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700483 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100484
Eric Laurentdc95a252018-04-12 12:46:56 -0700485 // when a call is active, force device selection to match source VOICE_COMMUNICATION
486 // for most other input sources to avoid rerouting call TX audio
487 if (isInCall()) {
488 switch (inputSource) {
489 case AUDIO_SOURCE_DEFAULT:
490 case AUDIO_SOURCE_MIC:
491 case AUDIO_SOURCE_VOICE_RECOGNITION:
492 case AUDIO_SOURCE_UNPROCESSED:
493 case AUDIO_SOURCE_HOTWORD:
494 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800495 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800496 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700497 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
498 break;
499 default:
500 break;
501 }
502 }
503
Eric Laurent2517af32020-11-25 15:31:27 +0100504 audio_devices_t commDeviceType =
505 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
506
François Gaffie2110e042015-03-24 08:41:51 +0100507 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100508 case AUDIO_SOURCE_DEFAULT:
509 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700510 device = availableDevices.getDevice(
511 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
512 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100513 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700514 device = availableDevices.getDevice(
515 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
516 if (device != nullptr) break;
517 }
518 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200519 AUDIO_DEVICE_IN_WIRED_HEADSET,
Eric Laurenta9986862020-10-07 08:42:28 -0700520 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500521 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700522 break;
François Gaffie2110e042015-03-24 08:41:51 +0100523
524 case AUDIO_SOURCE_VOICE_COMMUNICATION:
525 // Allow only use of devices on primary input if in call and HAL does not support routing
526 // to voice call path.
527 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700528 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
529 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700530 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700531 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100532 }
533
Eric Laurent2517af32020-11-25 15:31:27 +0100534 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100535 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700536 device = availableDevices.getDevice(
537 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
538 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100539 break;
540 }
Eric Laurent2517af32020-11-25 15:31:27 +0100541 }
542 switch (commDeviceType) {
543 case AUDIO_DEVICE_OUT_BLE_HEADSET:
544 device = availableDevices.getDevice(
Grzegorz Kołodziejczyk2e4e3e62021-07-21 15:35:03 +0200545 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100546 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100547 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700548 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100549 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
550 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100551 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100552 default: // FORCE_NONE
553 device = availableDevices.getFirstExistingDevice({
554 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500555 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
556 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100557 break;
558
François Gaffie2110e042015-03-24 08:41:51 +0100559 }
560 break;
561
562 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800563 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500564 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
565 device = availableDevices.getDevice(
566 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
567 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700568 }
wei wanga7020522020-12-10 21:36:11 -0500569 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
570 // because sometimes user want to do voice search by bt remote
571 // even if BUILDIN_MIC is available.
572 device = availableDevices.getFirstExistingDevice({
Eric Laurent93666b22022-05-13 14:42:13 +0200573 AUDIO_DEVICE_IN_WIRED_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500574 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
575 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
576
577 break;
578 case AUDIO_SOURCE_HOTWORD:
579 // We should not use primary output criteria for Hotword but rather limit
580 // to devices attached to the same HW module as the build in mic
581 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
582 availableDevices = availablePrimaryDevices;
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;
François Gaffie2110e042015-03-24 08:41:51 +0100587 }
jiabinf502d152019-08-07 14:43:33 -0700588 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,
591 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100592 break;
593 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700594 // For a device without built-in mic, adding usb device
595 device = availableDevices.getFirstExistingDevice({
596 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
597 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100598 break;
599 case AUDIO_SOURCE_VOICE_DOWNLINK:
600 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700601 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700602 device = availableDevices.getDevice(
603 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100604 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800605 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700606 device = availableDevices.getFirstExistingDevice({
607 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500608 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
609 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800610 break;
François Gaffie2110e042015-03-24 08:41:51 +0100611 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700612 device = availableDevices.getDevice(
613 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100614 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800615 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700616 device = availableDevices.getDevice(
617 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100618 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800619 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700620 device = availableDevices.getDevice(
621 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800622 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800623 case AUDIO_SOURCE_ULTRASOUND:
624 device = availableDevices.getFirstExistingDevice({
625 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
626 break;
François Gaffie2110e042015-03-24 08:41:51 +0100627 default:
628 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
629 break;
630 }
jiabinf502d152019-08-07 14:43:33 -0700631 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700632 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700633 device = availableDevices.getDevice(
634 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
635 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700636 "getDeviceForInputSource() no default device defined");
637 }
Eric Laurent2517af32020-11-25 15:31:27 +0100638
jiabinf502d152019-08-07 14:43:33 -0700639 ALOGV_IF(device != nullptr,
640 "getDeviceForInputSource()input source %d, device %08x",
641 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100642 return device;
643}
644
jiabin76e829d2023-04-04 21:02:36 +0000645void Engine::setStrategyDevices(const sp<ProductStrategy>& strategy, const DeviceVector &devices) {
646 strategy->setDeviceTypes(devices.types());
647 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
François Gaffiedc7553f2018-11-02 10:39:57 +0100648}
649
Eric Laurent2517af32020-11-25 15:31:27 +0100650product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
651 for (const auto& strategyMap : mLegacyStrategyMap) {
652 if (strategyMap.second == legacyStrategy) {
653 return strategyMap.first;
654 }
655 }
656 return PRODUCT_STRATEGY_NONE;
657}
François Gaffiedc7553f2018-11-02 10:39:57 +0100658
Eric Laurent2517af32020-11-25 15:31:27 +0100659audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
660 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
661 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
662 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
663 availableOutputDevices, strategy);
664 if (devices.size() > 0) {
665 return devices[0]->type();
666 }
667 return AUDIO_DEVICE_NONE;
668}
669
670DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
671 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
672 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700673 AudioDeviceTypeAddrVector preferredStrategyDevices;
674 const status_t status = getDevicesForRoleAndStrategy(
675 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700676 if (status == NO_ERROR) {
677 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100678 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700679 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000680 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700681 ALOGVV("%s using pref device %s for strategy %u",
682 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
683 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700684 }
685 }
Eric Laurent2517af32020-11-25 15:31:27 +0100686 return preferredAvailableDevVec;
687}
688
Eric Laurent6cd5f902021-03-23 18:29:58 +0100689
Eric Laurent2517af32020-11-25 15:31:27 +0100690DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100691 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
692
693 // Take context into account to remap product strategy before
694 // checking preferred device for strategy and applying default routing rules
695 strategy = remapStrategyFromContext(strategy, outputs);
696
Eric Laurent2517af32020-11-25 15:31:27 +0100697 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
698 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
699
Eric Laurent6cd5f902021-03-23 18:29:58 +0100700 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100701
Eric Laurent6cd5f902021-03-23 18:29:58 +0100702 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100703
Eric Laurent2517af32020-11-25 15:31:27 +0100704 // check if this strategy has a preferred device that is available,
705 // if yes, give priority to it.
706 DeviceVector preferredAvailableDevVec =
707 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
708 if (!preferredAvailableDevVec.isEmpty()) {
709 return preferredAvailableDevVec;
710 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700711
jiabinf502d152019-08-07 14:43:33 -0700712 return getDevicesForStrategyInt(legacyStrategy,
713 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100714 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100715}
716
717DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
718 const sp<DeviceDescriptor> &preferredDevice,
719 bool fromCache) const
720{
721 // First check for explict routing device
722 if (preferredDevice != nullptr) {
723 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
724 return DeviceVector(preferredDevice);
725 }
François Gaffiec005e562018-11-06 15:04:49 +0100726 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700727 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100728 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100729 //
730 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
731 // be by APM?
732 //
François Gaffiec005e562018-11-06 15:04:49 +0100733 // Honor explicit routing requests only if all active clients have a preferred route in which
734 // case the last active client route is used
735 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
736 if (device != nullptr) {
737 return DeviceVector(device);
738 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100739
740 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
741}
742
743DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
744{
745 auto attributes = getAttributesForStreamType(stream);
746 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
747}
748
749sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800750 uid_t uid,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800751 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100752{
François Gaffiec005e562018-11-06 15:04:49 +0100753 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700754 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100755 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100756 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100757
François Gaffiedc7553f2018-11-02 10:39:57 +0100758 //
François Gaffiec005e562018-11-06 15:04:49 +0100759 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
760 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100761 //
François Gaffiec005e562018-11-06 15:04:49 +0100762 // Honor explicit routing requests only if all active clients have a preferred route in which
763 // case the last active client route is used
764 sp<DeviceDescriptor> device =
765 findPreferredDevice(inputs, attr.source, availableInputDevices);
766 if (device != nullptr) {
767 return device;
768 }
769
Jan Sebechlebskya7ed5272022-09-15 17:57:42 +0200770 device = policyMixes.getDeviceAndMixForInputSource(attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800771 availableInputDevices,
772 uid,
773 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100774 if (device != nullptr) {
775 return device;
776 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100777
jiabinf502d152019-08-07 14:43:33 -0700778 device = getDeviceForInputSource(attr.source);
779 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
780 // Return immediately if the device is null or it is not a remote submix device.
781 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100782 }
jiabinf502d152019-08-07 14:43:33 -0700783
784 // For remote submix device, try to find the device by address.
785 address = "0";
786 std::size_t pos;
787 std::string tags { attr.tags };
788 if ((pos = tags.find("addr=")) != std::string::npos) {
789 address = tags.substr(pos + std::strlen("addr="));
790 }
791 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100792 String8(address.c_str()),
793 AUDIO_FORMAT_DEFAULT);
794}
795
François Gaffie2110e042015-03-24 08:41:51 +0100796} // namespace audio_policy
797} // namespace android
798
799