blob: 3aa3db5c2eff3d316dd5ffcfc5a18d236765b50e [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; };
44static const std::vector<legacy_strategy_map> gLegacyStrategy = {
45 { "STRATEGY_NONE", STRATEGY_NONE },
46 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
47 { "STRATEGY_PHONE", STRATEGY_PHONE },
48 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
49 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
50 { "STRATEGY_DTMF", STRATEGY_DTMF },
51 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
52 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
53 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
54 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
55 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
56};
57
François Gaffie2110e042015-03-24 08:41:51 +010058Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010059{
François Gaffiedc7553f2018-11-02 10:39:57 +010060 auto result = EngineBase::loadAudioPolicyEngineConfig();
61 ALOGE_IF(result.nbSkippedElement != 0,
62 "Policy Engine configuration is partially invalid, skipped %zu elements",
63 result.nbSkippedElement);
64
65 for (const auto &strategy : gLegacyStrategy) {
66 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010067 }
68}
69
François Gaffie2110e042015-03-24 08:41:51 +010070status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
71{
72 switch(usage) {
73 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
74 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
75 config != AUDIO_POLICY_FORCE_NONE) {
76 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
77 return BAD_VALUE;
78 }
François Gaffie2110e042015-03-24 08:41:51 +010079 break;
80 case AUDIO_POLICY_FORCE_FOR_MEDIA:
81 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
82 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
83 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
84 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
85 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
86 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
87 return BAD_VALUE;
88 }
François Gaffie2110e042015-03-24 08:41:51 +010089 break;
90 case AUDIO_POLICY_FORCE_FOR_RECORD:
91 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
92 config != AUDIO_POLICY_FORCE_NONE) {
93 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
94 return BAD_VALUE;
95 }
François Gaffie2110e042015-03-24 08:41:51 +010096 break;
97 case AUDIO_POLICY_FORCE_FOR_DOCK:
98 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
99 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
100 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
101 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
102 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
103 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
104 }
François Gaffie2110e042015-03-24 08:41:51 +0100105 break;
106 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
107 if (config != AUDIO_POLICY_FORCE_NONE &&
108 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
109 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
110 }
François Gaffie2110e042015-03-24 08:41:51 +0100111 break;
112 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
113 if (config != AUDIO_POLICY_FORCE_NONE &&
114 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800115 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
116 }
Phil Burk09bc4612016-02-24 15:58:15 -0800117 break;
118 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
119 if (config != AUDIO_POLICY_FORCE_NONE &&
120 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700121 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
122 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800123 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
124 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100125 }
François Gaffie2110e042015-03-24 08:41:51 +0100126 break;
Jack He96117ae2018-02-12 20:52:53 -0800127 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
128 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
129 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
130 return BAD_VALUE;
131 }
Jack He96117ae2018-02-12 20:52:53 -0800132 break;
François Gaffie2110e042015-03-24 08:41:51 +0100133 default:
134 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800135 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100136 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100137 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100138}
139
jiabinf502d152019-08-07 14:43:33 -0700140DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
141 DeviceVector availableOutputDevices,
142 DeviceVector availableInputDevices,
143 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800144{
jiabinf502d152019-08-07 14:43:33 -0700145 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100146
147 switch (strategy) {
148
149 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin9a3361e2019-10-01 09:38:30 -0700150 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100151 break;
152
153 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700154 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700155 devices = getDevicesForStrategyInt(
156 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100157 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800158 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700159 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100160 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
161 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700162 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100163 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800164 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700165 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700166 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
167 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800168 availableOutputDevices,
jiabinf502d152019-08-07 14:43:33 -0700169 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800170 // if no media is playing on the device, check for mandatory use of "safe" speaker
171 // when media would have played on speaker, and the safe speaker path is available
jiabinf502d152019-08-07 14:43:33 -0700172 if (!media_active_locally) {
173 devices.replaceDevicesByType(
174 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700175 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700176 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700177 }
François Gaffie2110e042015-03-24 08:41:51 +0100178 }
179 break;
180
181 case STRATEGY_DTMF:
182 if (!isInCall()) {
183 // when off call, DTMF strategy follows the same rules as MEDIA strategy
jiabinf502d152019-08-07 14:43:33 -0700184 devices = getDevicesForStrategyInt(
185 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100186 break;
187 }
188 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700189 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100190
191 case STRATEGY_PHONE:
192 // Force use of only devices on primary output if:
193 // - in call AND
194 // - cannot route from voice call RX OR
195 // - audio HAL version is < 3.0 and TX device is on the primary HW module
196 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700197 audio_devices_t txDevice = getDeviceForInputSource(
198 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100199 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700200 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700201 DeviceVector availPrimaryInputDevices =
202 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800203
204 // TODO: getPrimaryOutput return only devices from first module in
205 // audio_policy_configuration.xml, hearing aid is not there, but it's
206 // a primary device
207 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700208 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700209 primaryOutput->supportedDevices().types());
210 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700211 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100212
jiabinf502d152019-08-07 14:43:33 -0700213 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
214 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
215 ((availPrimaryInputDevices.getDevice(
216 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
jiabin4ef93452019-09-10 14:29:54 -0700217 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700218 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100219 }
220 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800221 // for phone strategy, we first consider the forced use and then the available devices by
222 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100223 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100224 case AUDIO_POLICY_FORCE_BT_SCO:
225 if (!isInCall() || strategy != STRATEGY_DTMF) {
jiabin9a3361e2019-10-01 09:38:30 -0700226 devices = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700227 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
228 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100229 }
jiabinf502d152019-08-07 14:43:33 -0700230 devices = availableOutputDevices.getFirstDevicesFromTypes({
231 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
232 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100233 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700234 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100235
236 default: // FORCE_NONE
jiabin9a3361e2019-10-01 09:38:30 -0700237 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
jiabinf502d152019-08-07 14:43:33 -0700238 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100239 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
240 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100241 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800242 outputs.isA2dpSupported()) {
jiabinf502d152019-08-07 14:43:33 -0700243 devices = availableOutputDevices.getFirstDevicesFromTypes({
244 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
245 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
246 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100247 }
jiabinf502d152019-08-07 14:43:33 -0700248 devices = availableOutputDevices.getFirstDevicesFromTypes({
249 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET,
250 AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET,
251 AUDIO_DEVICE_OUT_USB_DEVICE});
252 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100253 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700254 devices = availableOutputDevices.getFirstDevicesFromTypes({
255 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
256 AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
257 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100258 }
jiabin9a3361e2019-10-01 09:38:30 -0700259 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
François Gaffie2110e042015-03-24 08:41:51 +0100260 break;
261
262 case AUDIO_POLICY_FORCE_SPEAKER:
263 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
264 // A2DP speaker when forcing to speaker output
265 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100266 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800267 outputs.isA2dpSupported()) {
jiabin9a3361e2019-10-01 09:38:30 -0700268 devices = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700269 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
270 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100271 }
272 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700273 devices = availableOutputDevices.getFirstDevicesFromTypes({
274 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
275 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL,
276 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
277 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100278 }
jiabin9a3361e2019-10-01 09:38:30 -0700279 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100280 break;
281 }
282 break;
283
284 case STRATEGY_SONIFICATION:
285
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700286 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100287 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700288 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700289 devices = getDevicesForStrategyInt(
290 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100291 break;
292 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700293 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100294
295 case STRATEGY_ENFORCED_AUDIBLE:
296 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
297 // except:
298 // - when in call where it doesn't default to STRATEGY_PHONE behavior
299 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
300
301 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100302 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700303 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100304 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800305
306 // if SCO headset is connected and we are told to use it, play ringtone over
307 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700308 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700309 DeviceVector devices2;
310 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
311 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
312 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800313 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100314 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800315 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100316 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800317 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700318 if (!devices2.isEmpty()) {
319 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800320 break;
321 }
322 }
323 }
324 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100325 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700326 if (strategy == STRATEGY_SONIFICATION) {
327 devices.replaceDevicesByType(
328 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700329 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700330 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800331 }
jiabinf502d152019-08-07 14:43:33 -0700332 if (!devices2.isEmpty()) {
333 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800334 break;
335 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800336 }
337 }
François Gaffie2110e042015-03-24 08:41:51 +0100338 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700339 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100340
François Gaffie2110e042015-03-24 08:41:51 +0100341 case STRATEGY_ACCESSIBILITY:
342 if (strategy == STRATEGY_ACCESSIBILITY) {
343 // do not route accessibility prompts to a digital output currently configured with a
344 // compressed format as they would likely not be mixed and dropped.
345 for (size_t i = 0; i < outputs.size(); i++) {
346 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin5740f082019-08-19 15:08:30 -0700347 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin9a3361e2019-10-01 09:38:30 -0700348 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
349 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
350 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100351 }
352 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700353 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
354 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700355 return getDevicesForStrategyInt(
356 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800357 }
358 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700359 return getDevicesForStrategyInt(
360 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800361 }
François Gaffie2110e042015-03-24 08:41:51 +0100362 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800363 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700364 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100365
Eric Laurent28d09f02016-03-08 10:43:05 -0800366 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100367 case STRATEGY_REROUTING:
368 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700369 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100370 if (strategy != STRATEGY_SONIFICATION) {
371 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700372 sp<DeviceDescriptor> remoteSubmix;
373 if ((remoteSubmix = availableOutputDevices.getDevice(
374 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
375 AUDIO_FORMAT_DEFAULT)) != nullptr) {
376 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100377 }
378 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700379 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700380 devices = getDevicesForStrategyInt(
381 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700382 break;
383 }
juyuchenebebb5f2019-01-11 14:41:04 +0800384 // FIXME: Find a better solution to prevent routing to BT hearing aid(b/122931261).
jiabinf502d152019-08-07 14:43:33 -0700385 if ((devices2.isEmpty()) &&
juyuchenebebb5f2019-01-11 14:41:04 +0800386 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
jiabin9a3361e2019-10-01 09:38:30 -0700387 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
Eric Laurent58a73fc2018-02-21 18:46:13 -0800388 }
jiabinf502d152019-08-07 14:43:33 -0700389 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100390 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800391 outputs.isA2dpSupported()) {
jiabinf502d152019-08-07 14:43:33 -0700392 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
393 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
394 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100395 }
jiabinf502d152019-08-07 14:43:33 -0700396 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100397 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700398 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100399 }
jiabinf502d152019-08-07 14:43:33 -0700400 if (devices2.isEmpty()) {
401 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
402 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_LINE,
403 AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_USB_HEADSET,
404 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
405 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100406 }
jiabinf502d152019-08-07 14:43:33 -0700407 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100408 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700409 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100410 }
jiabinf502d152019-08-07 14:43:33 -0700411 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100412 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700413 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700414 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100415 }
jiabinf502d152019-08-07 14:43:33 -0700416 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700417 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100418 }
jiabinf502d152019-08-07 14:43:33 -0700419 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100420 if (strategy == STRATEGY_MEDIA) {
421 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700422 devices3 = availableOutputDevices.getDevicesFromTypes({
423 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100424 }
425
jiabinf502d152019-08-07 14:43:33 -0700426 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100427 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
428 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700429 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100430
431 // If hdmi system audio mode is on, remove speaker out of output list.
432 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100433 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100434 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700435 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100436 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700437
438 // for STRATEGY_SONIFICATION:
439 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700440 if (strategy == STRATEGY_SONIFICATION) {
441 devices.replaceDevicesByType(
442 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700443 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700444 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700445 }
François Gaffie2110e042015-03-24 08:41:51 +0100446 } break;
447
448 default:
jiabinf502d152019-08-07 14:43:33 -0700449 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100450 break;
451 }
452
jiabinf502d152019-08-07 14:43:33 -0700453 if (devices.isEmpty()) {
454 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
455 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
456 if (defaultOutputDevice != nullptr) {
457 devices.add(defaultOutputDevice);
458 }
459 ALOGE_IF(devices.isEmpty(),
460 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700461 }
jiabinf502d152019-08-07 14:43:33 -0700462
463 ALOGVV("getDevices"
464 "ForStrategy() strategy %d, device %x", strategy, devices.types());
465 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100466}
467
468
jiabinf502d152019-08-07 14:43:33 -0700469sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100470{
Eric Laurentaf377772019-03-29 14:50:21 -0700471 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
472 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100473 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700474 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700475 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700476 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
477 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700478 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100479
Eric Laurentdc95a252018-04-12 12:46:56 -0700480 // when a call is active, force device selection to match source VOICE_COMMUNICATION
481 // for most other input sources to avoid rerouting call TX audio
482 if (isInCall()) {
483 switch (inputSource) {
484 case AUDIO_SOURCE_DEFAULT:
485 case AUDIO_SOURCE_MIC:
486 case AUDIO_SOURCE_VOICE_RECOGNITION:
487 case AUDIO_SOURCE_UNPROCESSED:
488 case AUDIO_SOURCE_HOTWORD:
489 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800490 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700491 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
492 break;
493 default:
494 break;
495 }
496 }
497
François Gaffie2110e042015-03-24 08:41:51 +0100498 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100499 case AUDIO_SOURCE_DEFAULT:
500 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700501 device = availableDevices.getDevice(
502 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
503 if (device != nullptr) break;
504 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
505 device = availableDevices.getDevice(
506 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
507 if (device != nullptr) break;
508 }
509 device = availableDevices.getFirstExistingDevice({
510 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
511 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
512 break;
François Gaffie2110e042015-03-24 08:41:51 +0100513
514 case AUDIO_SOURCE_VOICE_COMMUNICATION:
515 // Allow only use of devices on primary input if in call and HAL does not support routing
516 // to voice call path.
517 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700518 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
519 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700520 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700521 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100522 }
523
François Gaffiedc7553f2018-11-02 10:39:57 +0100524 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100525 case AUDIO_POLICY_FORCE_BT_SCO:
526 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700527 device = availableDevices.getDevice(
528 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
529 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100530 break;
531 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700532 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100533
534 default: // FORCE_NONE
jiabinf502d152019-08-07 14:43:33 -0700535 device = availableDevices.getFirstExistingDevice({
536 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
537 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100538 break;
539
540 case AUDIO_POLICY_FORCE_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700541 device = availableDevices.getFirstExistingDevice({
542 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100543 break;
544 }
545 break;
546
547 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800548 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100549 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700550 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700551 // We should not use primary output criteria for Hotword but rather limit
552 // to devices attached to the same HW module as the build in mic
553 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700554 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700555 }
jiabinf502d152019-08-07 14:43:33 -0700556 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
557 device = availableDevices.getDevice(
558 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
559 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100560 }
jiabinf502d152019-08-07 14:43:33 -0700561 device = availableDevices.getFirstExistingDevice({
562 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
563 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100564 break;
565 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700566 // For a device without built-in mic, adding usb device
567 device = availableDevices.getFirstExistingDevice({
568 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
569 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100570 break;
571 case AUDIO_SOURCE_VOICE_DOWNLINK:
572 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700573 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700574 device = availableDevices.getDevice(
575 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100576 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800577 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700578 device = availableDevices.getFirstExistingDevice({
579 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
580 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800581 break;
François Gaffie2110e042015-03-24 08:41:51 +0100582 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700583 device = availableDevices.getDevice(
584 AUDIO_DEVICE_IN_REMOTE_SUBMIX, 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_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700587 device = availableDevices.getDevice(
588 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100589 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800590 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700591 device = availableDevices.getDevice(
592 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800593 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 }
jiabinf502d152019-08-07 14:43:33 -0700605 ALOGV_IF(device != nullptr,
606 "getDeviceForInputSource()input source %d, device %08x",
607 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100608 return device;
609}
610
François Gaffiedc7553f2018-11-02 10:39:57 +0100611void Engine::updateDeviceSelectionCache()
612{
613 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700614 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100615 auto devices = getDevicesForProductStrategy(strategy->getId());
616 mDevicesForStrategies[strategy->getId()] = devices;
617 strategy->setDeviceTypes(devices.types());
618 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
619 }
620}
621
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700622DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
François Gaffiedc7553f2018-11-02 10:39:57 +0100623 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100624
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700625 // check if this strategy has a preferred device that is available,
626 // if yes, give priority to it
627 AudioDeviceTypeAddr preferredStrategyDevice;
628 const status_t status = getPreferredDeviceForStrategy(strategy, preferredStrategyDevice);
629 if (status == NO_ERROR) {
630 // there is a preferred device, is it available?
631 sp<DeviceDescriptor> preferredAvailableDevDescr = availableOutputDevices.getDevice(
632 preferredStrategyDevice.mType,
633 String8(preferredStrategyDevice.mAddress.c_str()),
634 AUDIO_FORMAT_DEFAULT);
635 if (preferredAvailableDevDescr != nullptr) {
636 ALOGVV("%s using pref device 0x%08x/%s for strategy %u", __FUNCTION__,
637 preferredStrategyDevice.mType, preferredStrategyDevice.mAddress, strategy);
638 return DeviceVector(preferredAvailableDevDescr);
639 }
640 }
641
642 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
643 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100644 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700645 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabinf502d152019-08-07 14:43:33 -0700646 return getDevicesForStrategyInt(legacyStrategy,
647 availableOutputDevices,
648 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100649}
650
651DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
652 const sp<DeviceDescriptor> &preferredDevice,
653 bool fromCache) const
654{
655 // First check for explict routing device
656 if (preferredDevice != nullptr) {
657 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
658 return DeviceVector(preferredDevice);
659 }
François Gaffiec005e562018-11-06 15:04:49 +0100660 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700661 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100662 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100663 //
664 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
665 // be by APM?
666 //
François Gaffiec005e562018-11-06 15:04:49 +0100667 // Honor explicit routing requests only if all active clients have a preferred route in which
668 // case the last active client route is used
669 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
670 if (device != nullptr) {
671 return DeviceVector(device);
672 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100673
674 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
675}
676
677DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
678{
679 auto attributes = getAttributesForStreamType(stream);
680 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
681}
682
683sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800684 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100685{
François Gaffiec005e562018-11-06 15:04:49 +0100686 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700687 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100688 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100689 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100690
François Gaffiedc7553f2018-11-02 10:39:57 +0100691 //
François Gaffiec005e562018-11-06 15:04:49 +0100692 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
693 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100694 //
François Gaffiec005e562018-11-06 15:04:49 +0100695 // Honor explicit routing requests only if all active clients have a preferred route in which
696 // case the last active client route is used
697 sp<DeviceDescriptor> device =
698 findPreferredDevice(inputs, attr.source, availableInputDevices);
699 if (device != nullptr) {
700 return device;
701 }
702
703 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
704 if (device != nullptr) {
705 return device;
706 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100707
jiabinf502d152019-08-07 14:43:33 -0700708 device = getDeviceForInputSource(attr.source);
709 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
710 // Return immediately if the device is null or it is not a remote submix device.
711 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100712 }
jiabinf502d152019-08-07 14:43:33 -0700713
714 // For remote submix device, try to find the device by address.
715 address = "0";
716 std::size_t pos;
717 std::string tags { attr.tags };
718 if ((pos = tags.find("addr=")) != std::string::npos) {
719 address = tags.substr(pos + std::strlen("addr="));
720 }
721 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100722 String8(address.c_str()),
723 AUDIO_FORMAT_DEFAULT);
724}
725
François Gaffie2110e042015-03-24 08:41:51 +0100726} // namespace audio_policy
727} // namespace android
728
729