blob: 19937ed8ed5f099c7700823142a2658f28ad31d1 [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
jiabinf502d152019-08-07 14:43:33 -0700145DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
146 DeviceVector availableOutputDevices,
147 DeviceVector availableInputDevices,
148 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800149{
jiabinf502d152019-08-07 14:43:33 -0700150 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100151
152 switch (strategy) {
153
154 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin9a3361e2019-10-01 09:38:30 -0700155 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100156 break;
157
158 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700159 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700160 devices = getDevicesForStrategyInt(
161 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100162 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800163 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700164 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100165 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
166 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700167 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100168 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800169 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700170 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700171 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
172 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800173 availableOutputDevices,
jiabinf502d152019-08-07 14:43:33 -0700174 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800175 // if no media is playing on the device, check for mandatory use of "safe" speaker
176 // when media would have played on speaker, and the safe speaker path is available
jiabinf502d152019-08-07 14:43:33 -0700177 if (!media_active_locally) {
178 devices.replaceDevicesByType(
179 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700180 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700181 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700182 }
François Gaffie2110e042015-03-24 08:41:51 +0100183 }
184 break;
185
186 case STRATEGY_DTMF:
Eric Laurentcca11ce2020-11-25 15:31:27 +0100187 case STRATEGY_PHONE: {
François Gaffie2110e042015-03-24 08:41:51 +0100188 // Force use of only devices on primary output if:
189 // - in call AND
190 // - cannot route from voice call RX OR
191 // - audio HAL version is < 3.0 and TX device is on the primary HW module
192 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700193 audio_devices_t txDevice = getDeviceForInputSource(
194 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100195 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700196 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700197 DeviceVector availPrimaryInputDevices =
198 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800199
200 // TODO: getPrimaryOutput return only devices from first module in
201 // audio_policy_configuration.xml, hearing aid is not there, but it's
202 // a primary device
203 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700204 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700205 primaryOutput->supportedDevices().types());
206 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700207 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100208
jiabinf502d152019-08-07 14:43:33 -0700209 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
Eric Laurentcca11ce2020-11-25 15:31:27 +0100210 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
211 ((availPrimaryInputDevices.getDevice(
212 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
213 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700214 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100215 }
216 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100217 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
218 if (!devices.isEmpty()) break;
219 devices = availableOutputDevices.getFirstDevicesFromTypes({
220 AUDIO_DEVICE_OUT_WIRED_HEADPHONE,
221 AUDIO_DEVICE_OUT_WIRED_HEADSET,
222 AUDIO_DEVICE_OUT_LINE,
223 AUDIO_DEVICE_OUT_USB_HEADSET,
224 AUDIO_DEVICE_OUT_USB_DEVICE});
225 if (!devices.isEmpty()) break;
226 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
227 } break;
François Gaffie2110e042015-03-24 08:41:51 +0100228
229 case STRATEGY_SONIFICATION:
230
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700231 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100232 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700233 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700234 devices = getDevicesForStrategyInt(
235 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100236 break;
237 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700238 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100239
240 case STRATEGY_ENFORCED_AUDIBLE:
241 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
242 // except:
243 // - when in call where it doesn't default to STRATEGY_PHONE behavior
244 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
245
246 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100247 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700248 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100249 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800250
251 // if SCO headset is connected and we are told to use it, play ringtone over
252 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700253 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700254 DeviceVector devices2;
255 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
256 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
257 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800258 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100259 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800260 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100261 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800262 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700263 if (!devices2.isEmpty()) {
264 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800265 break;
266 }
267 }
268 }
269 // Use both Bluetooth SCO and phone default output when ringing in normal mode
Eric Laurentcca11ce2020-11-25 15:31:27 +0100270 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
271 availableOutputDevices, STRATEGY_PHONE))) {
jiabinf502d152019-08-07 14:43:33 -0700272 if (strategy == STRATEGY_SONIFICATION) {
273 devices.replaceDevicesByType(
274 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700275 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700276 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800277 }
jiabinf502d152019-08-07 14:43:33 -0700278 if (!devices2.isEmpty()) {
279 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800280 break;
281 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800282 }
283 }
François Gaffie2110e042015-03-24 08:41:51 +0100284 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700285 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100286
François Gaffie2110e042015-03-24 08:41:51 +0100287 case STRATEGY_ACCESSIBILITY:
288 if (strategy == STRATEGY_ACCESSIBILITY) {
289 // do not route accessibility prompts to a digital output currently configured with a
290 // compressed format as they would likely not be mixed and dropped.
291 for (size_t i = 0; i < outputs.size(); i++) {
292 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin5740f082019-08-19 15:08:30 -0700293 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin9a3361e2019-10-01 09:38:30 -0700294 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
295 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
296 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100297 }
298 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700299 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
300 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700301 return getDevicesForStrategyInt(
302 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800303 }
304 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700305 return getDevicesForStrategyInt(
306 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800307 }
François Gaffie2110e042015-03-24 08:41:51 +0100308 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800309 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700310 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100311
Eric Laurent28d09f02016-03-08 10:43:05 -0800312 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100313 case STRATEGY_REROUTING:
314 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700315 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100316 if (strategy != STRATEGY_SONIFICATION) {
317 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700318 sp<DeviceDescriptor> remoteSubmix;
319 if ((remoteSubmix = availableOutputDevices.getDevice(
320 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
321 AUDIO_FORMAT_DEFAULT)) != nullptr) {
322 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100323 }
324 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700325 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700326 devices = getDevicesForStrategyInt(
327 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700328 break;
329 }
Eric Laurentd77befb2020-10-07 08:42:28 -0700330
jiabinf502d152019-08-07 14:43:33 -0700331 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100332 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700333 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100334 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900335 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurentd77befb2020-10-07 08:42:28 -0700336 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900337 // Get the last connected device of wired and bluetooth a2dp
338 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
339 getLastRemovableMediaDevices());
340 } else {
341 // Get the last connected device of wired except bluetooth a2dp
342 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
343 getLastRemovableMediaDevices(GROUP_WIRED));
344 }
François Gaffie2110e042015-03-24 08:41:51 +0100345 }
jiabinf502d152019-08-07 14:43:33 -0700346 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100347 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700348 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100349 }
jiabinf502d152019-08-07 14:43:33 -0700350 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100351 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700352 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700353 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100354 }
jiabinf502d152019-08-07 14:43:33 -0700355 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700356 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100357 }
jiabinf502d152019-08-07 14:43:33 -0700358 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100359 if (strategy == STRATEGY_MEDIA) {
360 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700361 devices3 = availableOutputDevices.getDevicesFromTypes({
362 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100363 }
364
jiabinf502d152019-08-07 14:43:33 -0700365 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100366 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
367 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700368 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100369
370 // If hdmi system audio mode is on, remove speaker out of output list.
371 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100372 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100373 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700374 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100375 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700376
377 // for STRATEGY_SONIFICATION:
378 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700379 if (strategy == STRATEGY_SONIFICATION) {
380 devices.replaceDevicesByType(
381 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700382 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700383 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700384 }
François Gaffie2110e042015-03-24 08:41:51 +0100385 } break;
386
Eric Laurent21777f82019-12-06 18:12:06 -0800387 case STRATEGY_CALL_ASSISTANT:
388 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
389 break;
390
François Gaffie2110e042015-03-24 08:41:51 +0100391 default:
jiabinf502d152019-08-07 14:43:33 -0700392 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100393 break;
394 }
395
jiabinf502d152019-08-07 14:43:33 -0700396 if (devices.isEmpty()) {
397 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
398 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
399 if (defaultOutputDevice != nullptr) {
400 devices.add(defaultOutputDevice);
401 }
402 ALOGE_IF(devices.isEmpty(),
403 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700404 }
jiabinf502d152019-08-07 14:43:33 -0700405
jiabincd510522020-01-22 09:40:55 -0800406 ALOGVV("getDevices ForStrategy() strategy %d, device %s",
407 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700408 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100409}
410
411
jiabinf502d152019-08-07 14:43:33 -0700412sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100413{
Eric Laurentaf377772019-03-29 14:50:21 -0700414 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
415 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100416 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700417 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700418 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700419 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
420 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700421 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100422
Eric Laurentdc95a252018-04-12 12:46:56 -0700423 // when a call is active, force device selection to match source VOICE_COMMUNICATION
424 // for most other input sources to avoid rerouting call TX audio
425 if (isInCall()) {
426 switch (inputSource) {
427 case AUDIO_SOURCE_DEFAULT:
428 case AUDIO_SOURCE_MIC:
429 case AUDIO_SOURCE_VOICE_RECOGNITION:
430 case AUDIO_SOURCE_UNPROCESSED:
431 case AUDIO_SOURCE_HOTWORD:
432 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800433 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700434 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
435 break;
436 default:
437 break;
438 }
439 }
440
Eric Laurentcca11ce2020-11-25 15:31:27 +0100441 audio_devices_t commDeviceType =
442 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
443
François Gaffie2110e042015-03-24 08:41:51 +0100444 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100445 case AUDIO_SOURCE_DEFAULT:
446 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700447 device = availableDevices.getDevice(
448 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
449 if (device != nullptr) break;
Eric Laurentcca11ce2020-11-25 15:31:27 +0100450 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700451 device = availableDevices.getDevice(
452 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
453 if (device != nullptr) break;
454 }
455 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700456 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
457 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
458 AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700459 break;
François Gaffie2110e042015-03-24 08:41:51 +0100460
461 case AUDIO_SOURCE_VOICE_COMMUNICATION:
462 // Allow only use of devices on primary input if in call and HAL does not support routing
463 // to voice call path.
464 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700465 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
466 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700467 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700468 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100469 }
470
Eric Laurentcca11ce2020-11-25 15:31:27 +0100471 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
François Gaffie2110e042015-03-24 08:41:51 +0100472 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700473 device = availableDevices.getDevice(
474 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
475 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100476 break;
477 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100478 }
479 switch (commDeviceType) {
480 case AUDIO_DEVICE_OUT_BLE_HEADSET:
481 device = availableDevices.getDevice(
482 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100483 break;
Eric Laurentcca11ce2020-11-25 15:31:27 +0100484 case AUDIO_DEVICE_OUT_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700485 device = availableDevices.getFirstExistingDevice({
486 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100487 break;
Eric Laurentcca11ce2020-11-25 15:31:27 +0100488 default: // FORCE_NONE
489 device = availableDevices.getFirstExistingDevice({
490 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
491 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
492 break;
493
François Gaffie2110e042015-03-24 08:41:51 +0100494 }
495 break;
496
497 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800498 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100499 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700500 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700501 // We should not use primary output criteria for Hotword but rather limit
502 // to devices attached to the same HW module as the build in mic
503 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700504 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700505 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100506 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700507 device = availableDevices.getDevice(
508 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
509 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100510 }
jiabinf502d152019-08-07 14:43:33 -0700511 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700512 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
513 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
514 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100515 break;
516 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700517 // For a device without built-in mic, adding usb device
518 device = availableDevices.getFirstExistingDevice({
519 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
520 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100521 break;
522 case AUDIO_SOURCE_VOICE_DOWNLINK:
523 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700524 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700525 device = availableDevices.getDevice(
526 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100527 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800528 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700529 device = availableDevices.getFirstExistingDevice({
530 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
531 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800532 break;
François Gaffie2110e042015-03-24 08:41:51 +0100533 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700534 device = availableDevices.getDevice(
535 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100536 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800537 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700538 device = availableDevices.getDevice(
539 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100540 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800541 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700542 device = availableDevices.getDevice(
543 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800544 break;
François Gaffie2110e042015-03-24 08:41:51 +0100545 default:
546 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
547 break;
548 }
jiabinf502d152019-08-07 14:43:33 -0700549 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700550 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700551 device = availableDevices.getDevice(
552 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
553 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700554 "getDeviceForInputSource() no default device defined");
555 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100556
jiabinf502d152019-08-07 14:43:33 -0700557 ALOGV_IF(device != nullptr,
558 "getDeviceForInputSource()input source %d, device %08x",
559 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100560 return device;
561}
562
François Gaffiedc7553f2018-11-02 10:39:57 +0100563void Engine::updateDeviceSelectionCache()
564{
565 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700566 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100567 auto devices = getDevicesForProductStrategy(strategy->getId());
568 mDevicesForStrategies[strategy->getId()] = devices;
569 strategy->setDeviceTypes(devices.types());
570 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
571 }
572}
573
Eric Laurentcca11ce2020-11-25 15:31:27 +0100574product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
575 for (const auto& strategyMap : mLegacyStrategyMap) {
576 if (strategyMap.second == legacyStrategy) {
577 return strategyMap.first;
578 }
579 }
580 return PRODUCT_STRATEGY_NONE;
581}
François Gaffiedc7553f2018-11-02 10:39:57 +0100582
Eric Laurentcca11ce2020-11-25 15:31:27 +0100583audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
584 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
585 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
586 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
587 availableOutputDevices, strategy);
588 if (devices.size() > 0) {
589 return devices[0]->type();
590 }
591 return AUDIO_DEVICE_NONE;
592}
593
594DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
595 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
596 DeviceVector preferredAvailableDevVec = {};
jiabin4e826212020-08-07 17:32:40 -0700597 AudioDeviceTypeAddrVector preferredStrategyDevices;
598 const status_t status = getDevicesForRoleAndStrategy(
599 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700600 if (status == NO_ERROR) {
601 // there is a preferred device, is it available?
Eric Laurentcca11ce2020-11-25 15:31:27 +0100602 preferredAvailableDevVec =
jiabin4e826212020-08-07 17:32:40 -0700603 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
604 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
605 ALOGVV("%s using pref device %s for strategy %u",
606 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
607 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700608 }
609 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100610 return preferredAvailableDevVec;
611}
612
613DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
614 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
615 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
616 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
617
618 // When not in call, STRATEGY_PHONE and STRATEGY_DTMF follow STRATEGY_MEDIA
619 if (!isInCall() && (legacyStrategy == STRATEGY_PHONE || legacyStrategy == STRATEGY_DTMF)) {
620 legacyStrategy = STRATEGY_MEDIA;
621 strategy = getProductStrategyFromLegacy(STRATEGY_MEDIA);
622 }
623 // check if this strategy has a preferred device that is available,
624 // if yes, give priority to it.
625 DeviceVector preferredAvailableDevVec =
626 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
627 if (!preferredAvailableDevVec.isEmpty()) {
628 return preferredAvailableDevVec;
629 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700630
631 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
632 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
Eric Laurentcca11ce2020-11-25 15:31:27 +0100633
jiabinf502d152019-08-07 14:43:33 -0700634 return getDevicesForStrategyInt(legacyStrategy,
635 availableOutputDevices,
636 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100637}
638
639DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
640 const sp<DeviceDescriptor> &preferredDevice,
641 bool fromCache) const
642{
643 // First check for explict routing device
644 if (preferredDevice != nullptr) {
645 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
646 return DeviceVector(preferredDevice);
647 }
François Gaffiec005e562018-11-06 15:04:49 +0100648 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700649 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100650 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100651 //
652 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
653 // be by APM?
654 //
François Gaffiec005e562018-11-06 15:04:49 +0100655 // Honor explicit routing requests only if all active clients have a preferred route in which
656 // case the last active client route is used
657 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
658 if (device != nullptr) {
659 return DeviceVector(device);
660 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100661
662 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
663}
664
665DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
666{
667 auto attributes = getAttributesForStreamType(stream);
668 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
669}
670
671sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800672 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100673{
François Gaffiec005e562018-11-06 15:04:49 +0100674 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700675 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100676 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100677 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100678
François Gaffiedc7553f2018-11-02 10:39:57 +0100679 //
François Gaffiec005e562018-11-06 15:04:49 +0100680 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
681 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100682 //
François Gaffiec005e562018-11-06 15:04:49 +0100683 // Honor explicit routing requests only if all active clients have a preferred route in which
684 // case the last active client route is used
685 sp<DeviceDescriptor> device =
686 findPreferredDevice(inputs, attr.source, availableInputDevices);
687 if (device != nullptr) {
688 return device;
689 }
690
691 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
692 if (device != nullptr) {
693 return device;
694 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100695
jiabinf502d152019-08-07 14:43:33 -0700696 device = getDeviceForInputSource(attr.source);
697 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
698 // Return immediately if the device is null or it is not a remote submix device.
699 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100700 }
jiabinf502d152019-08-07 14:43:33 -0700701
702 // For remote submix device, try to find the device by address.
703 address = "0";
704 std::size_t pos;
705 std::string tags { attr.tags };
706 if ((pos = tags.find("addr=")) != std::string::npos) {
707 address = tags.substr(pos + std::strlen("addr="));
708 }
709 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100710 String8(address.c_str()),
711 AUDIO_FORMAT_DEFAULT);
712}
713
François Gaffie2110e042015-03-24 08:41:51 +0100714} // namespace audio_policy
715} // namespace android
716
717