blob: 4c3d92cf5e4e08a7e690f9408a460c86c1e34d7a [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 Laurent2802b862021-03-01 09:36:16 +0100198 } break;
199 case STRATEGY_ACCESSIBILITY: {
200 // do not route accessibility prompts to a digital output currently configured with a
201 // compressed format as they would likely not be mixed and dropped.
202 for (size_t i = 0; i < outputs.size(); i++) {
203 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
204 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
205 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
206 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
Kuowei Li01a686b2020-10-27 16:54:39 +0800207 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
Eric Laurent2802b862021-03-01 09:36:16 +0100208 }
209 }
210 } break;
211 default:
212 break;
213 }
214}
215
Eric Laurent6cd5f902021-03-23 18:29:58 +0100216product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
217 const SwAudioOutputCollection &outputs) const {
218 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
219 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
220
221 if (isInCall()) {
222 switch (legacyStrategy) {
223 case STRATEGY_ACCESSIBILITY:
224 case STRATEGY_DTMF:
225 case STRATEGY_MEDIA:
226 case STRATEGY_SONIFICATION:
227 case STRATEGY_SONIFICATION_RESPECTFUL:
228 legacyStrategy = STRATEGY_PHONE;
229 break;
230
231 default:
232 return strategy;
233 }
234 } else {
235 switch (legacyStrategy) {
236 case STRATEGY_SONIFICATION_RESPECTFUL:
237 case STRATEGY_SONIFICATION:
238 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
239 legacyStrategy = STRATEGY_PHONE;
240 }
241 break;
242
243 case STRATEGY_ACCESSIBILITY:
244 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
245 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
246 legacyStrategy = STRATEGY_SONIFICATION;
247 }
248 break;
249
250 default:
251 return strategy;
252 }
253 }
254 return getProductStrategyFromLegacy(legacyStrategy);
255}
256
Eric Laurent2802b862021-03-01 09:36:16 +0100257DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
258 DeviceVector availableOutputDevices,
Eric Laurent2802b862021-03-01 09:36:16 +0100259 const SwAudioOutputCollection &outputs) const
260{
261 DeviceVector devices;
262
263 switch (strategy) {
264
265 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
266 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
267 break;
268
Eric Laurent2802b862021-03-01 09:36:16 +0100269 case STRATEGY_PHONE: {
Eric Laurent2517af32020-11-25 15:31:27 +0100270 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
271 if (!devices.isEmpty()) break;
Eric Laurentc0697592021-05-10 11:45:22 +0200272 devices = availableOutputDevices.getFirstDevicesFromTypes(
273 getLastRemovableMediaDevices());
Eric Laurent2517af32020-11-25 15:31:27 +0100274 if (!devices.isEmpty()) break;
Eric Laurent73b6c962022-01-25 15:28:18 +0000275 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
Eric Laurent2517af32020-11-25 15:31:27 +0100276 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100277
278 case STRATEGY_SONIFICATION:
François Gaffie2110e042015-03-24 08:41:51 +0100279 case STRATEGY_ENFORCED_AUDIBLE:
280 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
281 // except:
282 // - when in call where it doesn't default to STRATEGY_PHONE behavior
283 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
284
285 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100286 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700287 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100288 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800289
290 // if SCO headset is connected and we are told to use it, play ringtone over
291 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700292 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700293 DeviceVector devices2;
294 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
295 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
296 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800297 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100298 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800299 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100300 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800301 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700302 if (!devices2.isEmpty()) {
303 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800304 break;
305 }
306 }
307 }
308 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurent2517af32020-11-25 15:31:27 +0100309 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
310 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700311 if (strategy == STRATEGY_SONIFICATION) {
312 devices.replaceDevicesByType(
313 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700314 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700315 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800316 }
jiabinf502d152019-08-07 14:43:33 -0700317 if (!devices2.isEmpty()) {
318 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800319 break;
320 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800321 }
322 }
François Gaffie2110e042015-03-24 08:41:51 +0100323 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700324 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100325
Eric Laurent6cd5f902021-03-23 18:29:58 +0100326 case STRATEGY_DTMF:
François Gaffie2110e042015-03-24 08:41:51 +0100327 case STRATEGY_ACCESSIBILITY:
Eric Laurent6cd5f902021-03-23 18:29:58 +0100328 case STRATEGY_SONIFICATION_RESPECTFUL:
François Gaffie2110e042015-03-24 08:41:51 +0100329 case STRATEGY_REROUTING:
330 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700331 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100332 if (strategy != STRATEGY_SONIFICATION) {
333 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700334 sp<DeviceDescriptor> remoteSubmix;
335 if ((remoteSubmix = availableOutputDevices.getDevice(
336 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
337 AUDIO_FORMAT_DEFAULT)) != nullptr) {
338 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100339 }
340 }
Eric Laurenta9986862020-10-07 08:42:28 -0700341
jiabinf502d152019-08-07 14:43:33 -0700342 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100343 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700344 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100345 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900346 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurenta9986862020-10-07 08:42:28 -0700347 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900348 // Get the last connected device of wired and bluetooth a2dp
349 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
350 getLastRemovableMediaDevices());
351 } else {
352 // Get the last connected device of wired except bluetooth a2dp
353 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
354 getLastRemovableMediaDevices(GROUP_WIRED));
355 }
François Gaffie2110e042015-03-24 08:41:51 +0100356 }
jiabinf502d152019-08-07 14:43:33 -0700357 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100358 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700359 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100360 }
jiabinf502d152019-08-07 14:43:33 -0700361 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100362 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700363 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700364 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100365 }
jiabinf502d152019-08-07 14:43:33 -0700366 if (devices2.isEmpty()) {
Eric Laurent73b6c962022-01-25 15:28:18 +0000367 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100368 }
jiabinf502d152019-08-07 14:43:33 -0700369 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100370 if (strategy == STRATEGY_MEDIA) {
371 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700372 devices3 = availableOutputDevices.getDevicesFromTypes({
Kuowei Li01a686b2020-10-27 16:54:39 +0800373 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
374 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
375 });
François Gaffie2110e042015-03-24 08:41:51 +0100376 }
377
jiabinf502d152019-08-07 14:43:33 -0700378 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100379 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
380 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700381 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100382
383 // If hdmi system audio mode is on, remove speaker out of output list.
384 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100385 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100386 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700387 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100388 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700389
Eric Laurent6cd5f902021-03-23 18:29:58 +0100390 bool mediaActiveLocally =
391 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
392 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
393 || outputs.isActiveLocally(
394 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
395 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Carter Hsu524ee462021-07-20 08:47:31 +0800396
397 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
398 // - for STRATEGY_SONIFICATION and ringtone active:
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700399 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
Eric Laurent6cd5f902021-03-23 18:29:58 +0100400 // - for STRATEGY_SONIFICATION_RESPECTFUL:
401 // if no media is playing on the device, check for mandatory use of "safe" speaker
402 // when media would have played on speaker, and the safe speaker path is available
Carter Hsu524ee462021-07-20 08:47:31 +0800403 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
Eric Laurent6cd5f902021-03-23 18:29:58 +0100404 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
jiabinf502d152019-08-07 14:43:33 -0700405 devices.replaceDevicesByType(
406 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700407 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700408 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700409 }
François Gaffie2110e042015-03-24 08:41:51 +0100410 } break;
411
Eric Laurent21777f82019-12-06 18:12:06 -0800412 case STRATEGY_CALL_ASSISTANT:
413 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
414 break;
415
Eric Laurentd9766932020-08-07 14:01:22 -0700416 case STRATEGY_NONE:
417 // Happens when internal strategies are processed ("rerouting", "patch"...)
418 break;
419
François Gaffie2110e042015-03-24 08:41:51 +0100420 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700421 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100422 break;
423 }
424
jiabinf502d152019-08-07 14:43:33 -0700425 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700426 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700427 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
428 if (defaultOutputDevice != nullptr) {
429 devices.add(defaultOutputDevice);
430 }
431 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700432 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700433 }
jiabinf502d152019-08-07 14:43:33 -0700434
Eric Laurentd9766932020-08-07 14:01:22 -0700435 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800436 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700437 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100438}
439
440
jiabinf502d152019-08-07 14:43:33 -0700441sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100442{
Eric Laurentaf377772019-03-29 14:50:21 -0700443 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
444 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100445 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700446 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700447 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700448 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
449 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700450 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100451
Eric Laurentdc95a252018-04-12 12:46:56 -0700452 // when a call is active, force device selection to match source VOICE_COMMUNICATION
453 // for most other input sources to avoid rerouting call TX audio
454 if (isInCall()) {
455 switch (inputSource) {
456 case AUDIO_SOURCE_DEFAULT:
457 case AUDIO_SOURCE_MIC:
458 case AUDIO_SOURCE_VOICE_RECOGNITION:
459 case AUDIO_SOURCE_UNPROCESSED:
460 case AUDIO_SOURCE_HOTWORD:
461 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800462 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800463 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentdc95a252018-04-12 12:46:56 -0700464 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
465 break;
466 default:
467 break;
468 }
469 }
470
Eric Laurent2517af32020-11-25 15:31:27 +0100471 audio_devices_t commDeviceType =
472 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
473
François Gaffie2110e042015-03-24 08:41:51 +0100474 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100475 case AUDIO_SOURCE_DEFAULT:
476 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700477 device = availableDevices.getDevice(
478 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
479 if (device != nullptr) break;
Eric Laurent2517af32020-11-25 15:31:27 +0100480 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700481 device = availableDevices.getDevice(
482 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
483 if (device != nullptr) break;
484 }
485 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700486 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
487 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
wei wanga7020522020-12-10 21:36:11 -0500488 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700489 break;
François Gaffie2110e042015-03-24 08:41:51 +0100490
491 case AUDIO_SOURCE_VOICE_COMMUNICATION:
492 // Allow only use of devices on primary input if in call and HAL does not support routing
493 // to voice call path.
494 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700495 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
496 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700497 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700498 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100499 }
500
Eric Laurent2517af32020-11-25 15:31:27 +0100501 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100502 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700503 device = availableDevices.getDevice(
504 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
505 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100506 break;
507 }
Eric Laurent2517af32020-11-25 15:31:27 +0100508 }
509 switch (commDeviceType) {
510 case AUDIO_DEVICE_OUT_BLE_HEADSET:
511 device = availableDevices.getDevice(
Grzegorz Kołodziejczyk2e4e3e62021-07-21 15:35:03 +0200512 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100513 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100514 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700515 device = availableDevices.getFirstExistingDevice({
Eric Laurent6435dd72020-12-02 14:42:42 +0100516 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
517 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100518 break;
Eric Laurent2517af32020-11-25 15:31:27 +0100519 default: // FORCE_NONE
520 device = availableDevices.getFirstExistingDevice({
521 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500522 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
523 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurent2517af32020-11-25 15:31:27 +0100524 break;
525
François Gaffie2110e042015-03-24 08:41:51 +0100526 }
527 break;
528
529 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800530 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500531 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
532 device = availableDevices.getDevice(
533 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
534 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700535 }
wei wanga7020522020-12-10 21:36:11 -0500536 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
537 // because sometimes user want to do voice search by bt remote
538 // even if BUILDIN_MIC is available.
539 device = availableDevices.getFirstExistingDevice({
540 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
541 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
542 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
543
544 break;
545 case AUDIO_SOURCE_HOTWORD:
546 // We should not use primary output criteria for Hotword but rather limit
547 // to devices attached to the same HW module as the build in mic
548 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
549 availableDevices = availablePrimaryDevices;
Eric Laurent2517af32020-11-25 15:31:27 +0100550 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700551 device = availableDevices.getDevice(
552 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
553 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100554 }
jiabinf502d152019-08-07 14:43:33 -0700555 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700556 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
557 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
558 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100559 break;
560 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700561 // For a device without built-in mic, adding usb device
562 device = availableDevices.getFirstExistingDevice({
563 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
564 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100565 break;
566 case AUDIO_SOURCE_VOICE_DOWNLINK:
567 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700568 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700569 device = availableDevices.getDevice(
570 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100571 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800572 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700573 device = availableDevices.getFirstExistingDevice({
574 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500575 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
576 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800577 break;
François Gaffie2110e042015-03-24 08:41:51 +0100578 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700579 device = availableDevices.getDevice(
580 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100581 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800582 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700583 device = availableDevices.getDevice(
584 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100585 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800586 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700587 device = availableDevices.getDevice(
588 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800589 break;
Carter Hsua3abb402021-10-26 11:11:20 +0800590 case AUDIO_SOURCE_ULTRASOUND:
591 device = availableDevices.getFirstExistingDevice({
592 AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC});
593 break;
François Gaffie2110e042015-03-24 08:41:51 +0100594 default:
595 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
596 break;
597 }
jiabinf502d152019-08-07 14:43:33 -0700598 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700599 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700600 device = availableDevices.getDevice(
601 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
602 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700603 "getDeviceForInputSource() no default device defined");
604 }
Eric Laurent2517af32020-11-25 15:31:27 +0100605
jiabinf502d152019-08-07 14:43:33 -0700606 ALOGV_IF(device != nullptr,
607 "getDeviceForInputSource()input source %d, device %08x",
608 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100609 return device;
610}
611
François Gaffiedc7553f2018-11-02 10:39:57 +0100612void Engine::updateDeviceSelectionCache()
613{
614 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700615 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100616 auto devices = getDevicesForProductStrategy(strategy->getId());
617 mDevicesForStrategies[strategy->getId()] = devices;
618 strategy->setDeviceTypes(devices.types());
619 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
620 }
621}
622
Eric Laurent2517af32020-11-25 15:31:27 +0100623product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
624 for (const auto& strategyMap : mLegacyStrategyMap) {
625 if (strategyMap.second == legacyStrategy) {
626 return strategyMap.first;
627 }
628 }
629 return PRODUCT_STRATEGY_NONE;
630}
François Gaffiedc7553f2018-11-02 10:39:57 +0100631
Eric Laurent2517af32020-11-25 15:31:27 +0100632audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
633 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
634 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
635 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
636 availableOutputDevices, strategy);
637 if (devices.size() > 0) {
638 return devices[0]->type();
639 }
640 return AUDIO_DEVICE_NONE;
641}
642
643DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
644 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
645 DeviceVector preferredAvailableDevVec = {};
jiabin0a488932020-08-07 17:32:40 -0700646 AudioDeviceTypeAddrVector preferredStrategyDevices;
647 const status_t status = getDevicesForRoleAndStrategy(
648 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700649 if (status == NO_ERROR) {
650 // there is a preferred device, is it available?
Eric Laurent2517af32020-11-25 15:31:27 +0100651 preferredAvailableDevVec =
jiabin0a488932020-08-07 17:32:40 -0700652 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
jiabin61591b12022-01-21 17:06:06 +0000653 if (preferredAvailableDevVec.size() == preferredStrategyDevices.size()) {
jiabin0a488932020-08-07 17:32:40 -0700654 ALOGVV("%s using pref device %s for strategy %u",
655 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
656 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700657 }
658 }
Eric Laurent2517af32020-11-25 15:31:27 +0100659 return preferredAvailableDevVec;
660}
661
Eric Laurent6cd5f902021-03-23 18:29:58 +0100662
Eric Laurent2517af32020-11-25 15:31:27 +0100663DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
Eric Laurent6cd5f902021-03-23 18:29:58 +0100664 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
665
666 // Take context into account to remap product strategy before
667 // checking preferred device for strategy and applying default routing rules
668 strategy = remapStrategyFromContext(strategy, outputs);
669
Eric Laurent2517af32020-11-25 15:31:27 +0100670 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
671 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
672
Eric Laurent6cd5f902021-03-23 18:29:58 +0100673 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
Eric Laurent2802b862021-03-01 09:36:16 +0100674
Eric Laurent6cd5f902021-03-23 18:29:58 +0100675 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
Eric Laurent2802b862021-03-01 09:36:16 +0100676
Eric Laurent2517af32020-11-25 15:31:27 +0100677 // check if this strategy has a preferred device that is available,
678 // if yes, give priority to it.
679 DeviceVector preferredAvailableDevVec =
680 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
681 if (!preferredAvailableDevVec.isEmpty()) {
682 return preferredAvailableDevVec;
683 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700684
jiabinf502d152019-08-07 14:43:33 -0700685 return getDevicesForStrategyInt(legacyStrategy,
686 availableOutputDevices,
Eric Laurent6cd5f902021-03-23 18:29:58 +0100687 outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100688}
689
690DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
691 const sp<DeviceDescriptor> &preferredDevice,
692 bool fromCache) const
693{
694 // First check for explict routing device
695 if (preferredDevice != nullptr) {
696 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
697 return DeviceVector(preferredDevice);
698 }
François Gaffiec005e562018-11-06 15:04:49 +0100699 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700700 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100701 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100702 //
703 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
704 // be by APM?
705 //
François Gaffiec005e562018-11-06 15:04:49 +0100706 // Honor explicit routing requests only if all active clients have a preferred route in which
707 // case the last active client route is used
708 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
709 if (device != nullptr) {
710 return DeviceVector(device);
711 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100712
713 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
714}
715
716DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
717{
718 auto attributes = getAttributesForStreamType(stream);
719 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
720}
721
722sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
yuanjiahsu0735bf32021-03-18 08:12:54 +0800723 uid_t uid,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800724 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100725{
François Gaffiec005e562018-11-06 15:04:49 +0100726 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700727 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100728 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100729 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100730
François Gaffiedc7553f2018-11-02 10:39:57 +0100731 //
François Gaffiec005e562018-11-06 15:04:49 +0100732 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
733 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100734 //
François Gaffiec005e562018-11-06 15:04:49 +0100735 // Honor explicit routing requests only if all active clients have a preferred route in which
736 // case the last active client route is used
737 sp<DeviceDescriptor> device =
738 findPreferredDevice(inputs, attr.source, availableInputDevices);
739 if (device != nullptr) {
740 return device;
741 }
742
yuanjiahsu0735bf32021-03-18 08:12:54 +0800743 device = policyMixes.getDeviceAndMixForInputSource(attr.source,
744 availableInputDevices,
745 uid,
746 mix);
François Gaffiec005e562018-11-06 15:04:49 +0100747 if (device != nullptr) {
748 return device;
749 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100750
jiabinf502d152019-08-07 14:43:33 -0700751 device = getDeviceForInputSource(attr.source);
752 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
753 // Return immediately if the device is null or it is not a remote submix device.
754 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100755 }
jiabinf502d152019-08-07 14:43:33 -0700756
757 // For remote submix device, try to find the device by address.
758 address = "0";
759 std::size_t pos;
760 std::string tags { attr.tags };
761 if ((pos = tags.find("addr=")) != std::string::npos) {
762 address = tags.substr(pos + std::strlen("addr="));
763 }
764 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100765 String8(address.c_str()),
766 AUDIO_FORMAT_DEFAULT);
767}
768
François Gaffie2110e042015-03-24 08:41:51 +0100769} // namespace audio_policy
770} // namespace android
771
772