blob: e0a0bbbcd50562065470594fbfa3e4d66c91db71 [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,
wei wanga7020522020-12-10 21:36:11 -0500458 AUDIO_DEVICE_IN_BLUETOOTH_BLE, 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,
wei wanga7020522020-12-10 21:36:11 -0500491 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
492 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentcca11ce2020-11-25 15:31:27 +0100493 break;
494
François Gaffie2110e042015-03-24 08:41:51 +0100495 }
496 break;
497
498 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800499 case AUDIO_SOURCE_UNPROCESSED:
wei wanga7020522020-12-10 21:36:11 -0500500 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
501 device = availableDevices.getDevice(
502 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
503 if (device != nullptr) break;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700504 }
wei wanga7020522020-12-10 21:36:11 -0500505 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
506 // because sometimes user want to do voice search by bt remote
507 // even if BUILDIN_MIC is available.
508 device = availableDevices.getFirstExistingDevice({
509 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
510 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
511 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
512
513 break;
514 case AUDIO_SOURCE_HOTWORD:
515 // We should not use primary output criteria for Hotword but rather limit
516 // to devices attached to the same HW module as the build in mic
517 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
518 availableDevices = availablePrimaryDevices;
Eric Laurentcca11ce2020-11-25 15:31:27 +0100519 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
jiabinf502d152019-08-07 14:43:33 -0700520 device = availableDevices.getDevice(
521 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
522 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100523 }
jiabinf502d152019-08-07 14:43:33 -0700524 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700525 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
526 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
527 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100528 break;
529 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700530 // For a device without built-in mic, adding usb device
531 device = availableDevices.getFirstExistingDevice({
532 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
533 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100534 break;
535 case AUDIO_SOURCE_VOICE_DOWNLINK:
536 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700537 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700538 device = availableDevices.getDevice(
539 AUDIO_DEVICE_IN_VOICE_CALL, 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_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700542 device = availableDevices.getFirstExistingDevice({
543 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
wei wanga7020522020-12-10 21:36:11 -0500544 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
545 AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800546 break;
François Gaffie2110e042015-03-24 08:41:51 +0100547 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700548 device = availableDevices.getDevice(
549 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100550 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800551 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700552 device = availableDevices.getDevice(
553 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100554 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800555 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700556 device = availableDevices.getDevice(
557 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800558 break;
François Gaffie2110e042015-03-24 08:41:51 +0100559 default:
560 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
561 break;
562 }
jiabinf502d152019-08-07 14:43:33 -0700563 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700564 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700565 device = availableDevices.getDevice(
566 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
567 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700568 "getDeviceForInputSource() no default device defined");
569 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100570
jiabinf502d152019-08-07 14:43:33 -0700571 ALOGV_IF(device != nullptr,
572 "getDeviceForInputSource()input source %d, device %08x",
573 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100574 return device;
575}
576
François Gaffiedc7553f2018-11-02 10:39:57 +0100577void Engine::updateDeviceSelectionCache()
578{
579 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700580 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100581 auto devices = getDevicesForProductStrategy(strategy->getId());
582 mDevicesForStrategies[strategy->getId()] = devices;
583 strategy->setDeviceTypes(devices.types());
584 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
585 }
586}
587
Eric Laurentcca11ce2020-11-25 15:31:27 +0100588product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
589 for (const auto& strategyMap : mLegacyStrategyMap) {
590 if (strategyMap.second == legacyStrategy) {
591 return strategyMap.first;
592 }
593 }
594 return PRODUCT_STRATEGY_NONE;
595}
François Gaffiedc7553f2018-11-02 10:39:57 +0100596
Eric Laurentcca11ce2020-11-25 15:31:27 +0100597audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
598 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
599 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
600 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
601 availableOutputDevices, strategy);
602 if (devices.size() > 0) {
603 return devices[0]->type();
604 }
605 return AUDIO_DEVICE_NONE;
606}
607
608DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
609 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
610 DeviceVector preferredAvailableDevVec = {};
jiabin4e826212020-08-07 17:32:40 -0700611 AudioDeviceTypeAddrVector preferredStrategyDevices;
612 const status_t status = getDevicesForRoleAndStrategy(
613 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700614 if (status == NO_ERROR) {
615 // there is a preferred device, is it available?
Eric Laurentcca11ce2020-11-25 15:31:27 +0100616 preferredAvailableDevVec =
jiabin4e826212020-08-07 17:32:40 -0700617 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
618 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
619 ALOGVV("%s using pref device %s for strategy %u",
620 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
621 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700622 }
623 }
Eric Laurentcca11ce2020-11-25 15:31:27 +0100624 return preferredAvailableDevVec;
625}
626
627DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
628 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
629 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
630 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
631
632 // When not in call, STRATEGY_PHONE and STRATEGY_DTMF follow STRATEGY_MEDIA
633 if (!isInCall() && (legacyStrategy == STRATEGY_PHONE || legacyStrategy == STRATEGY_DTMF)) {
634 legacyStrategy = STRATEGY_MEDIA;
635 strategy = getProductStrategyFromLegacy(STRATEGY_MEDIA);
636 }
637 // check if this strategy has a preferred device that is available,
638 // if yes, give priority to it.
639 DeviceVector preferredAvailableDevVec =
640 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
641 if (!preferredAvailableDevVec.isEmpty()) {
642 return preferredAvailableDevVec;
643 }
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700644
645 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
646 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
Eric Laurentcca11ce2020-11-25 15:31:27 +0100647
jiabinf502d152019-08-07 14:43:33 -0700648 return getDevicesForStrategyInt(legacyStrategy,
649 availableOutputDevices,
650 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100651}
652
653DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
654 const sp<DeviceDescriptor> &preferredDevice,
655 bool fromCache) const
656{
657 // First check for explict routing device
658 if (preferredDevice != nullptr) {
659 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
660 return DeviceVector(preferredDevice);
661 }
François Gaffiec005e562018-11-06 15:04:49 +0100662 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700663 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100664 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100665 //
666 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
667 // be by APM?
668 //
François Gaffiec005e562018-11-06 15:04:49 +0100669 // Honor explicit routing requests only if all active clients have a preferred route in which
670 // case the last active client route is used
671 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
672 if (device != nullptr) {
673 return DeviceVector(device);
674 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100675
676 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
677}
678
679DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
680{
681 auto attributes = getAttributesForStreamType(stream);
682 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
683}
684
685sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800686 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100687{
François Gaffiec005e562018-11-06 15:04:49 +0100688 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700689 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100690 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100691 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100692
François Gaffiedc7553f2018-11-02 10:39:57 +0100693 //
François Gaffiec005e562018-11-06 15:04:49 +0100694 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
695 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100696 //
François Gaffiec005e562018-11-06 15:04:49 +0100697 // Honor explicit routing requests only if all active clients have a preferred route in which
698 // case the last active client route is used
699 sp<DeviceDescriptor> device =
700 findPreferredDevice(inputs, attr.source, availableInputDevices);
701 if (device != nullptr) {
702 return device;
703 }
704
705 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
706 if (device != nullptr) {
707 return device;
708 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100709
jiabinf502d152019-08-07 14:43:33 -0700710 device = getDeviceForInputSource(attr.source);
711 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
712 // Return immediately if the device is null or it is not a remote submix device.
713 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100714 }
jiabinf502d152019-08-07 14:43:33 -0700715
716 // For remote submix device, try to find the device by address.
717 address = "0";
718 std::size_t pos;
719 std::string tags { attr.tags };
720 if ((pos = tags.find("addr=")) != std::string::npos) {
721 address = tags.substr(pos + std::strlen("addr="));
722 }
723 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100724 String8(address.c_str()),
725 AUDIO_FORMAT_DEFAULT);
726}
727
François Gaffie2110e042015-03-24 08:41:51 +0100728} // namespace audio_policy
729} // namespace android
730
731