blob: cfb2206644f945810b5a86ac92ad4e400d442753 [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>
30#include <AudioPort.h>
31#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>
34#include <utils/String8.h>
35#include <utils/Log.h>
36
37namespace android
38{
39namespace audio_policy
40{
41
François Gaffiedc7553f2018-11-02 10:39:57 +010042struct legacy_strategy_map { const char *name; legacy_strategy id; };
43static const std::vector<legacy_strategy_map> gLegacyStrategy = {
44 { "STRATEGY_NONE", STRATEGY_NONE },
45 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
46 { "STRATEGY_PHONE", STRATEGY_PHONE },
47 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
48 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
49 { "STRATEGY_DTMF", STRATEGY_DTMF },
50 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
51 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
52 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
53 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
54 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
55};
56
François Gaffie2110e042015-03-24 08:41:51 +010057Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010058{
François Gaffiedc7553f2018-11-02 10:39:57 +010059 auto result = EngineBase::loadAudioPolicyEngineConfig();
60 ALOGE_IF(result.nbSkippedElement != 0,
61 "Policy Engine configuration is partially invalid, skipped %zu elements",
62 result.nbSkippedElement);
63
64 for (const auto &strategy : gLegacyStrategy) {
65 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010066 }
67}
68
François Gaffie2110e042015-03-24 08:41:51 +010069status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
70{
71 switch(usage) {
72 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
73 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
74 config != AUDIO_POLICY_FORCE_NONE) {
75 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
76 return BAD_VALUE;
77 }
François Gaffie2110e042015-03-24 08:41:51 +010078 break;
79 case AUDIO_POLICY_FORCE_FOR_MEDIA:
80 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
81 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
82 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
83 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
84 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
85 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
86 return BAD_VALUE;
87 }
François Gaffie2110e042015-03-24 08:41:51 +010088 break;
89 case AUDIO_POLICY_FORCE_FOR_RECORD:
90 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
91 config != AUDIO_POLICY_FORCE_NONE) {
92 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
93 return BAD_VALUE;
94 }
François Gaffie2110e042015-03-24 08:41:51 +010095 break;
96 case AUDIO_POLICY_FORCE_FOR_DOCK:
97 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
98 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
99 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
100 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
101 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
102 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
103 }
François Gaffie2110e042015-03-24 08:41:51 +0100104 break;
105 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
106 if (config != AUDIO_POLICY_FORCE_NONE &&
107 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
108 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
109 }
François Gaffie2110e042015-03-24 08:41:51 +0100110 break;
111 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
112 if (config != AUDIO_POLICY_FORCE_NONE &&
113 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800114 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
115 }
Phil Burk09bc4612016-02-24 15:58:15 -0800116 break;
117 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
118 if (config != AUDIO_POLICY_FORCE_NONE &&
119 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700120 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
121 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800122 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
123 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100124 }
François Gaffie2110e042015-03-24 08:41:51 +0100125 break;
Jack He96117ae2018-02-12 20:52:53 -0800126 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
127 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
128 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
129 return BAD_VALUE;
130 }
Jack He96117ae2018-02-12 20:52:53 -0800131 break;
François Gaffie2110e042015-03-24 08:41:51 +0100132 default:
133 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800134 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100135 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100136 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100137}
138
jiabin6cb004a2019-06-21 11:43:56 -0700139DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
140 DeviceVector availableOutputDevices,
141 DeviceVector availableInputDevices,
142 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800143{
jiabin6cb004a2019-06-21 11:43:56 -0700144 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100145
146 switch (strategy) {
147
148 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin6cb004a2019-06-21 11:43:56 -0700149 devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100150 break;
151
152 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700153 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabin6cb004a2019-06-21 11:43:56 -0700154 devices = getDevicesForStrategyInt(
155 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100156 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800157 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700158 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100159 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
160 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700161 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100162 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800163 // routing is same as media without the "remote" device
jiabin6cb004a2019-06-21 11:43:56 -0700164 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypeMask(
165 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
166 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800167 availableOutputDevices,
jiabin6cb004a2019-06-21 11:43:56 -0700168 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800169 // if no media is playing on the device, check for mandatory use of "safe" speaker
170 // when media would have played on speaker, and the safe speaker path is available
jiabin6cb004a2019-06-21 11:43:56 -0700171 if (!media_active_locally) {
172 devices.replaceDevicesByType(
173 AUDIO_DEVICE_OUT_SPEAKER,
174 availableOutputDevices.getDevicesFromTypeMask(
175 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700176 }
François Gaffie2110e042015-03-24 08:41:51 +0100177 }
178 break;
179
180 case STRATEGY_DTMF:
181 if (!isInCall()) {
182 // when off call, DTMF strategy follows the same rules as MEDIA strategy
jiabin6cb004a2019-06-21 11:43:56 -0700183 devices = getDevicesForStrategyInt(
184 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100185 break;
186 }
187 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700188 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100189
190 case STRATEGY_PHONE:
191 // Force use of only devices on primary output if:
192 // - in call AND
193 // - cannot route from voice call RX OR
194 // - audio HAL version is < 3.0 and TX device is on the primary HW module
195 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabin6cb004a2019-06-21 11:43:56 -0700196 audio_devices_t txDevice = getDeviceForInputSource(
197 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100198 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabin6cb004a2019-06-21 11:43:56 -0700199 DeviceVector availPrimaryInputDevices =
200 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800201
202 // TODO: getPrimaryOutput return only devices from first module in
203 // audio_policy_configuration.xml, hearing aid is not there, but it's
204 // a primary device
205 // FIXME: this is not the right way of solving this problem
jiabin6cb004a2019-06-21 11:43:56 -0700206 DeviceVector availPrimaryOutputDevices = primaryOutput->supportedDevices();
207 availPrimaryOutputDevices.add(
208 availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100209
jiabin6cb004a2019-06-21 11:43:56 -0700210 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
211 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
212 ((availPrimaryInputDevices.getDevice(
213 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
214 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
215 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100216 }
217 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800218 // for phone strategy, we first consider the forced use and then the available devices by
219 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100220 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100221 case AUDIO_POLICY_FORCE_BT_SCO:
222 if (!isInCall() || strategy != STRATEGY_DTMF) {
jiabin6cb004a2019-06-21 11:43:56 -0700223 devices = availableOutputDevices.getDevicesFromTypeMask(
224 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
225 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100226 }
jiabin6cb004a2019-06-21 11:43:56 -0700227 devices = availableOutputDevices.getFirstDevicesFromTypes({
228 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
229 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100230 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700231 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100232
233 default: // FORCE_NONE
jiabin6cb004a2019-06-21 11:43:56 -0700234 devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID);
235 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100236 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
237 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100238 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800239 outputs.isA2dpSupported()) {
jiabin6cb004a2019-06-21 11:43:56 -0700240 devices = availableOutputDevices.getFirstDevicesFromTypes({
241 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
242 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
243 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100244 }
jiabin6cb004a2019-06-21 11:43:56 -0700245 devices = availableOutputDevices.getFirstDevicesFromTypes({
246 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET,
247 AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET,
248 AUDIO_DEVICE_OUT_USB_DEVICE});
249 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100250 if (!isInCall()) {
jiabin6cb004a2019-06-21 11:43:56 -0700251 devices = availableOutputDevices.getFirstDevicesFromTypes({
252 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
253 AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
254 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100255 }
jiabin6cb004a2019-06-21 11:43:56 -0700256 devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_EARPIECE);
François Gaffie2110e042015-03-24 08:41:51 +0100257 break;
258
259 case AUDIO_POLICY_FORCE_SPEAKER:
260 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
261 // A2DP speaker when forcing to speaker output
262 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100263 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800264 outputs.isA2dpSupported()) {
jiabin6cb004a2019-06-21 11:43:56 -0700265 devices = availableOutputDevices.getDevicesFromTypeMask(
266 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
267 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100268 }
269 if (!isInCall()) {
jiabin6cb004a2019-06-21 11:43:56 -0700270 devices = availableOutputDevices.getFirstDevicesFromTypes({
271 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
272 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL,
273 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
274 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100275 }
jiabin6cb004a2019-06-21 11:43:56 -0700276 devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100277 break;
278 }
279 break;
280
281 case STRATEGY_SONIFICATION:
282
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700283 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100284 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700285 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabin6cb004a2019-06-21 11:43:56 -0700286 devices = getDevicesForStrategyInt(
287 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100288 break;
289 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700290 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100291
292 case STRATEGY_ENFORCED_AUDIBLE:
293 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
294 // except:
295 // - when in call where it doesn't default to STRATEGY_PHONE behavior
296 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
297
298 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100299 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin6cb004a2019-06-21 11:43:56 -0700300 devices = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100301 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800302
303 // if SCO headset is connected and we are told to use it, play ringtone over
304 // speaker and BT SCO
jiabin6cb004a2019-06-21 11:43:56 -0700305 if (!availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_ALL_SCO).isEmpty()) {
306 DeviceVector devices2;
307 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
308 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
309 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800310 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100311 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800312 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100313 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800314 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabin6cb004a2019-06-21 11:43:56 -0700315 if (!devices2.isEmpty()) {
316 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800317 break;
318 }
319 }
320 }
321 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100322 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
jiabin6cb004a2019-06-21 11:43:56 -0700323 if (strategy == STRATEGY_SONIFICATION) {
324 devices.replaceDevicesByType(
325 AUDIO_DEVICE_OUT_SPEAKER,
326 availableOutputDevices.getDevicesFromTypeMask(
327 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800328 }
jiabin6cb004a2019-06-21 11:43:56 -0700329 if (!devices2.isEmpty()) {
330 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800331 break;
332 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800333 }
334 }
François Gaffie2110e042015-03-24 08:41:51 +0100335 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700336 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100337
François Gaffie2110e042015-03-24 08:41:51 +0100338 case STRATEGY_ACCESSIBILITY:
339 if (strategy == STRATEGY_ACCESSIBILITY) {
340 // do not route accessibility prompts to a digital output currently configured with a
341 // compressed format as they would likely not be mixed and dropped.
342 for (size_t i = 0; i < outputs.size(); i++) {
343 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin6cb004a2019-06-21 11:43:56 -0700344 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat)) {
345 availableOutputDevices.remove(desc->devices().getDevicesFromTypeMask(
346 AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF
347 | AUDIO_DEVICE_OUT_HDMI_ARC));
François Gaffie2110e042015-03-24 08:41:51 +0100348 }
349 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700350 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
351 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabin6cb004a2019-06-21 11:43:56 -0700352 return getDevicesForStrategyInt(
353 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800354 }
355 if (isInCall()) {
jiabin6cb004a2019-06-21 11:43:56 -0700356 return getDevicesForStrategyInt(
357 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800358 }
François Gaffie2110e042015-03-24 08:41:51 +0100359 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800360 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700361 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100362
Eric Laurent28d09f02016-03-08 10:43:05 -0800363 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100364 case STRATEGY_REROUTING:
365 case STRATEGY_MEDIA: {
jiabin6cb004a2019-06-21 11:43:56 -0700366 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100367 if (strategy != STRATEGY_SONIFICATION) {
368 // no sonification on remote submix (e.g. WFD)
jiabin6cb004a2019-06-21 11:43:56 -0700369 sp<DeviceDescriptor> remoteSubmix;
370 if ((remoteSubmix = availableOutputDevices.getDevice(
371 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
372 AUDIO_FORMAT_DEFAULT)) != nullptr) {
373 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100374 }
375 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700376 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabin6cb004a2019-06-21 11:43:56 -0700377 devices = getDevicesForStrategyInt(
378 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700379 break;
380 }
juyuchenebebb5f2019-01-11 14:41:04 +0800381 // FIXME: Find a better solution to prevent routing to BT hearing aid(b/122931261).
jiabin6cb004a2019-06-21 11:43:56 -0700382 if ((devices2.isEmpty()) &&
juyuchenebebb5f2019-01-11 14:41:04 +0800383 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
jiabin6cb004a2019-06-21 11:43:56 -0700384 devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_HEARING_AID);
Eric Laurent58a73fc2018-02-21 18:46:13 -0800385 }
jiabin6cb004a2019-06-21 11:43:56 -0700386 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100387 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800388 outputs.isA2dpSupported()) {
jiabin6cb004a2019-06-21 11:43:56 -0700389 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
390 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
391 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER});
François Gaffie2110e042015-03-24 08:41:51 +0100392 }
jiabin6cb004a2019-06-21 11:43:56 -0700393 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100394 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin6cb004a2019-06-21 11:43:56 -0700395 devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100396 }
jiabin6cb004a2019-06-21 11:43:56 -0700397 if (devices2.isEmpty()) {
398 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
399 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_LINE,
400 AUDIO_DEVICE_OUT_WIRED_HEADSET, AUDIO_DEVICE_OUT_USB_HEADSET,
401 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
402 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET});
François Gaffie2110e042015-03-24 08:41:51 +0100403 }
jiabin6cb004a2019-06-21 11:43:56 -0700404 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100405 // no sonification on aux digital (e.g. HDMI)
jiabin6cb004a2019-06-21 11:43:56 -0700406 devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100407 }
jiabin6cb004a2019-06-21 11:43:56 -0700408 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100409 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin6cb004a2019-06-21 11:43:56 -0700410 devices2 = availableOutputDevices.getDevicesFromTypeMask(
411 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100412 }
jiabin6cb004a2019-06-21 11:43:56 -0700413 if (devices2.isEmpty()) {
414 devices2 = availableOutputDevices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100415 }
jiabin6cb004a2019-06-21 11:43:56 -0700416 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100417 if (strategy == STRATEGY_MEDIA) {
418 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin6cb004a2019-06-21 11:43:56 -0700419 devices3 = availableOutputDevices.getDevicesFromTypeMask(
420 AUDIO_DEVICE_OUT_HDMI_ARC | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_AUX_LINE);
François Gaffie2110e042015-03-24 08:41:51 +0100421 }
422
jiabin6cb004a2019-06-21 11:43:56 -0700423 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100424 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
425 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabin6cb004a2019-06-21 11:43:56 -0700426 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100427
428 // If hdmi system audio mode is on, remove speaker out of output list.
429 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100430 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100431 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin6cb004a2019-06-21 11:43:56 -0700432 devices.remove(devices.getDevicesFromTypeMask(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100433 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700434
435 // for STRATEGY_SONIFICATION:
436 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabin6cb004a2019-06-21 11:43:56 -0700437 if (strategy == STRATEGY_SONIFICATION) {
438 devices.replaceDevicesByType(
439 AUDIO_DEVICE_OUT_SPEAKER,
440 availableOutputDevices.getDevicesFromTypeMask(
441 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700442 }
François Gaffie2110e042015-03-24 08:41:51 +0100443 } break;
444
445 default:
jiabin6cb004a2019-06-21 11:43:56 -0700446 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100447 break;
448 }
449
jiabin6cb004a2019-06-21 11:43:56 -0700450 if (devices.isEmpty()) {
451 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
452 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
453 if (defaultOutputDevice != nullptr) {
454 devices.add(defaultOutputDevice);
455 }
456 ALOGE_IF(devices.isEmpty(),
457 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700458 }
jiabin6cb004a2019-06-21 11:43:56 -0700459
460 ALOGVV("getDevices"
461 "ForStrategy() strategy %d, device %x", strategy, devices.types());
462 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100463}
464
465
jiabin6cb004a2019-06-21 11:43:56 -0700466sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100467{
Eric Laurentaf377772019-03-29 14:50:21 -0700468 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
469 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100470 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabin6cb004a2019-06-21 11:43:56 -0700471 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700472 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabin6cb004a2019-06-21 11:43:56 -0700473 DeviceVector availablePrimaryDevices = availableInputDevices.getDevicesFromHwModule(
474 primaryOutput->getModuleHandle());
475 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100476
Eric Laurentdc95a252018-04-12 12:46:56 -0700477 // when a call is active, force device selection to match source VOICE_COMMUNICATION
478 // for most other input sources to avoid rerouting call TX audio
479 if (isInCall()) {
480 switch (inputSource) {
481 case AUDIO_SOURCE_DEFAULT:
482 case AUDIO_SOURCE_MIC:
483 case AUDIO_SOURCE_VOICE_RECOGNITION:
484 case AUDIO_SOURCE_UNPROCESSED:
485 case AUDIO_SOURCE_HOTWORD:
486 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800487 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700488 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
489 break;
490 default:
491 break;
492 }
493 }
494
François Gaffie2110e042015-03-24 08:41:51 +0100495 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100496 case AUDIO_SOURCE_DEFAULT:
497 case AUDIO_SOURCE_MIC:
jiabin6cb004a2019-06-21 11:43:56 -0700498 device = availableDevices.getDevice(
499 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
500 if (device != nullptr) break;
501 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
502 device = availableDevices.getDevice(
503 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
504 if (device != nullptr) break;
505 }
506 device = availableDevices.getFirstExistingDevice({
507 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
508 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
509 break;
François Gaffie2110e042015-03-24 08:41:51 +0100510
511 case AUDIO_SOURCE_VOICE_COMMUNICATION:
512 // Allow only use of devices on primary input if in call and HAL does not support routing
513 // to voice call path.
514 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabin6cb004a2019-06-21 11:43:56 -0700515 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
516 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
517 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100518 }
519
François Gaffiedc7553f2018-11-02 10:39:57 +0100520 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100521 case AUDIO_POLICY_FORCE_BT_SCO:
522 // if SCO device is requested but no SCO device is available, fall back to default case
jiabin6cb004a2019-06-21 11:43:56 -0700523 device = availableDevices.getDevice(
524 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
525 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100526 break;
527 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700528 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100529
530 default: // FORCE_NONE
jiabin6cb004a2019-06-21 11:43:56 -0700531 device = availableDevices.getFirstExistingDevice({
532 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
533 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100534 break;
535
536 case AUDIO_POLICY_FORCE_SPEAKER:
jiabin6cb004a2019-06-21 11:43:56 -0700537 device = availableDevices.getFirstExistingDevice({
538 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100539 break;
540 }
541 break;
542
543 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800544 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100545 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700546 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabin6cb004a2019-06-21 11:43:56 -0700547 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700548 }
jiabin6cb004a2019-06-21 11:43:56 -0700549 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
550 device = availableDevices.getDevice(
551 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
552 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100553 }
jiabin6cb004a2019-06-21 11:43:56 -0700554 device = availableDevices.getFirstExistingDevice({
555 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
556 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100557 break;
558 case AUDIO_SOURCE_CAMCORDER:
jiabin6cb004a2019-06-21 11:43:56 -0700559 // For a device without built-in mic, adding usb device
560 device = availableDevices.getFirstExistingDevice({
561 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
562 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100563 break;
564 case AUDIO_SOURCE_VOICE_DOWNLINK:
565 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700566 case AUDIO_SOURCE_VOICE_UPLINK:
jiabin6cb004a2019-06-21 11:43:56 -0700567 device = availableDevices.getDevice(
568 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100569 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800570 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabin6cb004a2019-06-21 11:43:56 -0700571 device = availableDevices.getFirstExistingDevice({
572 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
573 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800574 break;
François Gaffie2110e042015-03-24 08:41:51 +0100575 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabin6cb004a2019-06-21 11:43:56 -0700576 device = availableDevices.getDevice(
577 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100578 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800579 case AUDIO_SOURCE_FM_TUNER:
jiabin6cb004a2019-06-21 11:43:56 -0700580 device = availableDevices.getDevice(
581 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100582 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800583 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabin6cb004a2019-06-21 11:43:56 -0700584 device = availableDevices.getDevice(
585 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800586 break;
François Gaffie2110e042015-03-24 08:41:51 +0100587 default:
588 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
589 break;
590 }
jiabin6cb004a2019-06-21 11:43:56 -0700591 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700592 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabin6cb004a2019-06-21 11:43:56 -0700593 device = availableDevices.getDevice(
594 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
595 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700596 "getDeviceForInputSource() no default device defined");
597 }
jiabin6cb004a2019-06-21 11:43:56 -0700598 ALOGV_IF(device != nullptr,
599 "getDeviceForInputSource()input source %d, device %08x",
600 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100601 return device;
602}
603
François Gaffiedc7553f2018-11-02 10:39:57 +0100604void Engine::updateDeviceSelectionCache()
605{
606 for (const auto &iter : getProductStrategies()) {
607 const auto &strategy = iter.second;
608 auto devices = getDevicesForProductStrategy(strategy->getId());
609 mDevicesForStrategies[strategy->getId()] = devices;
610 strategy->setDeviceTypes(devices.types());
611 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
612 }
613}
614
615DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const
616{
617 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
618 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
619 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
620
621 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
622 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabin6cb004a2019-06-21 11:43:56 -0700623 return getDevicesForStrategyInt(legacyStrategy,
624 availableOutputDevices,
625 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100626}
627
628DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
629 const sp<DeviceDescriptor> &preferredDevice,
630 bool fromCache) const
631{
632 // First check for explict routing device
633 if (preferredDevice != nullptr) {
634 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
635 return DeviceVector(preferredDevice);
636 }
François Gaffiec005e562018-11-06 15:04:49 +0100637 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700638 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100639 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100640 //
641 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
642 // be by APM?
643 //
François Gaffiec005e562018-11-06 15:04:49 +0100644 // Honor explicit routing requests only if all active clients have a preferred route in which
645 // case the last active client route is used
646 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
647 if (device != nullptr) {
648 return DeviceVector(device);
649 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100650
651 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
652}
653
654DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
655{
656 auto attributes = getAttributesForStreamType(stream);
657 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
658}
659
660sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800661 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100662{
François Gaffiec005e562018-11-06 15:04:49 +0100663 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700664 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100665 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100666 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100667
François Gaffiedc7553f2018-11-02 10:39:57 +0100668 //
François Gaffiec005e562018-11-06 15:04:49 +0100669 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
670 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100671 //
François Gaffiec005e562018-11-06 15:04:49 +0100672 // Honor explicit routing requests only if all active clients have a preferred route in which
673 // case the last active client route is used
674 sp<DeviceDescriptor> device =
675 findPreferredDevice(inputs, attr.source, availableInputDevices);
676 if (device != nullptr) {
677 return device;
678 }
679
680 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
681 if (device != nullptr) {
682 return device;
683 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100684
jiabin6cb004a2019-06-21 11:43:56 -0700685 device = getDeviceForInputSource(attr.source);
686 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
687 // Return immediately if the device is null or it is not a remote submix device.
688 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100689 }
jiabin6cb004a2019-06-21 11:43:56 -0700690
691 // For remote submix device, try to find the device by address.
692 address = "0";
693 std::size_t pos;
694 std::string tags { attr.tags };
695 if ((pos = tags.find("addr=")) != std::string::npos) {
696 address = tags.substr(pos + std::strlen("addr="));
697 }
698 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100699 String8(address.c_str()),
700 AUDIO_FORMAT_DEFAULT);
701}
702
François Gaffie2110e042015-03-24 08:41:51 +0100703} // namespace audio_policy
704} // namespace android
705
706