blob: 0ec169ab18362da0b05aaec2e015cf4b744ecc35 [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:
187 if (!isInCall()) {
188 // when off call, DTMF strategy follows the same rules as MEDIA strategy
jiabinf502d152019-08-07 14:43:33 -0700189 devices = getDevicesForStrategyInt(
190 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100191 break;
192 }
193 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700194 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100195
196 case STRATEGY_PHONE:
197 // Force use of only devices on primary output if:
198 // - in call AND
199 // - cannot route from voice call RX OR
200 // - audio HAL version is < 3.0 and TX device is on the primary HW module
201 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700202 audio_devices_t txDevice = getDeviceForInputSource(
203 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100204 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700205 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700206 DeviceVector availPrimaryInputDevices =
207 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800208
209 // TODO: getPrimaryOutput return only devices from first module in
210 // audio_policy_configuration.xml, hearing aid is not there, but it's
211 // a primary device
212 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700213 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700214 primaryOutput->supportedDevices().types());
215 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700216 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100217
jiabinf502d152019-08-07 14:43:33 -0700218 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
219 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
220 ((availPrimaryInputDevices.getDevice(
221 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
jiabin4ef93452019-09-10 14:29:54 -0700222 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700223 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100224 }
225 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800226 // for phone strategy, we first consider the forced use and then the available devices by
227 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100228 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100229 case AUDIO_POLICY_FORCE_BT_SCO:
230 if (!isInCall() || strategy != STRATEGY_DTMF) {
jiabin9a3361e2019-10-01 09:38:30 -0700231 devices = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700232 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
233 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100234 }
jiabinf502d152019-08-07 14:43:33 -0700235 devices = availableOutputDevices.getFirstDevicesFromTypes({
236 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
237 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100238 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700239 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100240
241 default: // FORCE_NONE
jiabin9a3361e2019-10-01 09:38:30 -0700242 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
jiabinf502d152019-08-07 14:43:33 -0700243 if (!devices.isEmpty()) break;
Eric Laurentd77befb2020-10-07 08:42:28 -0700244
245 // TODO (b/161358428): remove when preferred device
246 // for strategy phone will be used instead of AUDIO_POLICY_FORCE_FOR_COMMUNICATION
247 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_BLE_HEADSET);
248 if (!devices.isEmpty()) break;
249
François Gaffie2110e042015-03-24 08:41:51 +0100250 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
251 if (!isInCall() &&
Eric Laurentd77befb2020-10-07 08:42:28 -0700252 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
jiabinf502d152019-08-07 14:43:33 -0700253 devices = availableOutputDevices.getFirstDevicesFromTypes({
254 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
255 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
256 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100257 }
jiabinf502d152019-08-07 14:43:33 -0700258 devices = availableOutputDevices.getFirstDevicesFromTypes({
259 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET,
260 AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET,
261 AUDIO_DEVICE_OUT_USB_DEVICE});
262 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100263 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700264 devices = availableOutputDevices.getFirstDevicesFromTypes({
265 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
266 AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
267 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100268 }
jiabin9a3361e2019-10-01 09:38:30 -0700269 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
François Gaffie2110e042015-03-24 08:41:51 +0100270 break;
271
272 case AUDIO_POLICY_FORCE_SPEAKER:
273 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
274 // A2DP speaker when forcing to speaker output
Eric Laurentd77befb2020-10-07 08:42:28 -0700275 if (!isInCall()) {
jiabin9a3361e2019-10-01 09:38:30 -0700276 devices = availableOutputDevices.getDevicesFromType(
Eric Laurentd77befb2020-10-07 08:42:28 -0700277 AUDIO_DEVICE_OUT_BLE_SPEAKER);
jiabinf502d152019-08-07 14:43:33 -0700278 if (!devices.isEmpty()) break;
Eric Laurentd77befb2020-10-07 08:42:28 -0700279
280 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
281 devices = availableOutputDevices.getDevicesFromType(
282 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
283 if (!devices.isEmpty()) break;
284 }
François Gaffie2110e042015-03-24 08:41:51 +0100285 }
286 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700287 devices = availableOutputDevices.getFirstDevicesFromTypes({
288 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
289 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL,
290 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
291 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100292 }
jiabin9a3361e2019-10-01 09:38:30 -0700293 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100294 break;
295 }
296 break;
297
298 case STRATEGY_SONIFICATION:
299
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700300 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100301 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700302 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700303 devices = getDevicesForStrategyInt(
304 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100305 break;
306 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700307 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100308
309 case STRATEGY_ENFORCED_AUDIBLE:
310 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
311 // except:
312 // - when in call where it doesn't default to STRATEGY_PHONE behavior
313 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
314
315 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100316 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700317 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100318 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800319
320 // if SCO headset is connected and we are told to use it, play ringtone over
321 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700322 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700323 DeviceVector devices2;
324 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
325 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
326 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800327 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100328 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800329 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100330 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800331 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700332 if (!devices2.isEmpty()) {
333 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800334 break;
335 }
336 }
337 }
338 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100339 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700340 if (strategy == STRATEGY_SONIFICATION) {
341 devices.replaceDevicesByType(
342 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700343 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700344 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800345 }
jiabinf502d152019-08-07 14:43:33 -0700346 if (!devices2.isEmpty()) {
347 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800348 break;
349 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800350 }
351 }
François Gaffie2110e042015-03-24 08:41:51 +0100352 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700353 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100354
François Gaffie2110e042015-03-24 08:41:51 +0100355 case STRATEGY_ACCESSIBILITY:
356 if (strategy == STRATEGY_ACCESSIBILITY) {
357 // do not route accessibility prompts to a digital output currently configured with a
358 // compressed format as they would likely not be mixed and dropped.
359 for (size_t i = 0; i < outputs.size(); i++) {
360 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin5740f082019-08-19 15:08:30 -0700361 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin9a3361e2019-10-01 09:38:30 -0700362 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
363 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
364 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100365 }
366 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700367 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
368 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700369 return getDevicesForStrategyInt(
370 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800371 }
372 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700373 return getDevicesForStrategyInt(
374 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800375 }
François Gaffie2110e042015-03-24 08:41:51 +0100376 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800377 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700378 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100379
Eric Laurent28d09f02016-03-08 10:43:05 -0800380 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100381 case STRATEGY_REROUTING:
382 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700383 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100384 if (strategy != STRATEGY_SONIFICATION) {
385 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700386 sp<DeviceDescriptor> remoteSubmix;
387 if ((remoteSubmix = availableOutputDevices.getDevice(
388 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
389 AUDIO_FORMAT_DEFAULT)) != nullptr) {
390 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100391 }
392 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700393 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700394 devices = getDevicesForStrategyInt(
395 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700396 break;
397 }
Eric Laurentd77befb2020-10-07 08:42:28 -0700398
jiabinf502d152019-08-07 14:43:33 -0700399 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100400 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700401 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100402 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900403 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
Eric Laurentd77befb2020-10-07 08:42:28 -0700404 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
Baekgyeong Kim08451522019-11-01 20:40:02 +0900405 // Get the last connected device of wired and bluetooth a2dp
406 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
407 getLastRemovableMediaDevices());
408 } else {
409 // Get the last connected device of wired except bluetooth a2dp
410 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
411 getLastRemovableMediaDevices(GROUP_WIRED));
412 }
François Gaffie2110e042015-03-24 08:41:51 +0100413 }
jiabinf502d152019-08-07 14:43:33 -0700414 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100415 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700416 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100417 }
jiabinf502d152019-08-07 14:43:33 -0700418 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100419 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700420 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700421 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100422 }
jiabinf502d152019-08-07 14:43:33 -0700423 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700424 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100425 }
jiabinf502d152019-08-07 14:43:33 -0700426 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100427 if (strategy == STRATEGY_MEDIA) {
428 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700429 devices3 = availableOutputDevices.getDevicesFromTypes({
430 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100431 }
432
jiabinf502d152019-08-07 14:43:33 -0700433 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100434 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
435 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700436 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100437
438 // If hdmi system audio mode is on, remove speaker out of output list.
439 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100440 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100441 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700442 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100443 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700444
445 // for STRATEGY_SONIFICATION:
446 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700447 if (strategy == STRATEGY_SONIFICATION) {
448 devices.replaceDevicesByType(
449 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700450 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700451 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700452 }
François Gaffie2110e042015-03-24 08:41:51 +0100453 } break;
454
Eric Laurent21777f82019-12-06 18:12:06 -0800455 case STRATEGY_CALL_ASSISTANT:
456 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
457 break;
458
François Gaffie2110e042015-03-24 08:41:51 +0100459 default:
jiabinf502d152019-08-07 14:43:33 -0700460 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100461 break;
462 }
463
jiabinf502d152019-08-07 14:43:33 -0700464 if (devices.isEmpty()) {
465 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
466 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
467 if (defaultOutputDevice != nullptr) {
468 devices.add(defaultOutputDevice);
469 }
470 ALOGE_IF(devices.isEmpty(),
471 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700472 }
jiabinf502d152019-08-07 14:43:33 -0700473
jiabincd510522020-01-22 09:40:55 -0800474 ALOGVV("getDevices ForStrategy() strategy %d, device %s",
475 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700476 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100477}
478
479
jiabinf502d152019-08-07 14:43:33 -0700480sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100481{
Eric Laurentaf377772019-03-29 14:50:21 -0700482 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
483 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100484 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700485 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700486 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700487 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
488 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700489 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100490
Eric Laurentdc95a252018-04-12 12:46:56 -0700491 // when a call is active, force device selection to match source VOICE_COMMUNICATION
492 // for most other input sources to avoid rerouting call TX audio
493 if (isInCall()) {
494 switch (inputSource) {
495 case AUDIO_SOURCE_DEFAULT:
496 case AUDIO_SOURCE_MIC:
497 case AUDIO_SOURCE_VOICE_RECOGNITION:
498 case AUDIO_SOURCE_UNPROCESSED:
499 case AUDIO_SOURCE_HOTWORD:
500 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800501 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700502 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
503 break;
504 default:
505 break;
506 }
507 }
508
François Gaffie2110e042015-03-24 08:41:51 +0100509 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100510 case AUDIO_SOURCE_DEFAULT:
511 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700512 device = availableDevices.getDevice(
513 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
514 if (device != nullptr) break;
515 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
516 device = availableDevices.getDevice(
517 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
518 if (device != nullptr) break;
519 }
520 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700521 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
522 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
523 AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700524 break;
François Gaffie2110e042015-03-24 08:41:51 +0100525
526 case AUDIO_SOURCE_VOICE_COMMUNICATION:
527 // Allow only use of devices on primary input if in call and HAL does not support routing
528 // to voice call path.
529 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700530 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
531 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700532 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700533 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100534 }
535
François Gaffiedc7553f2018-11-02 10:39:57 +0100536 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100537 case AUDIO_POLICY_FORCE_BT_SCO:
538 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700539 device = availableDevices.getDevice(
540 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
541 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100542 break;
543 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700544 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100545
546 default: // FORCE_NONE
Eric Laurentd77befb2020-10-07 08:42:28 -0700547 // TODO (b/161358428): remove AUDIO_DEVICE_IN_BLE_HEADSET from the list
548 // when preferred device for strategy phone will be used instead of
549 // AUDIO_POLICY_FORCE_FOR_COMMUNICATION.
jiabinf502d152019-08-07 14:43:33 -0700550 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700551 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
552 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
553 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100554 break;
555
556 case AUDIO_POLICY_FORCE_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700557 device = availableDevices.getFirstExistingDevice({
558 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100559 break;
560 }
561 break;
562
563 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800564 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100565 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700566 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700567 // We should not use primary output criteria for Hotword but rather limit
568 // to devices attached to the same HW module as the build in mic
569 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700570 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700571 }
jiabinf502d152019-08-07 14:43:33 -0700572 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
573 device = availableDevices.getDevice(
574 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
575 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100576 }
jiabinf502d152019-08-07 14:43:33 -0700577 device = availableDevices.getFirstExistingDevice({
Eric Laurentd77befb2020-10-07 08:42:28 -0700578 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
579 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
580 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100581 break;
582 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700583 // For a device without built-in mic, adding usb device
584 device = availableDevices.getFirstExistingDevice({
585 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
586 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100587 break;
588 case AUDIO_SOURCE_VOICE_DOWNLINK:
589 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700590 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700591 device = availableDevices.getDevice(
592 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100593 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800594 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700595 device = availableDevices.getFirstExistingDevice({
596 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
597 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800598 break;
François Gaffie2110e042015-03-24 08:41:51 +0100599 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700600 device = availableDevices.getDevice(
601 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100602 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800603 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700604 device = availableDevices.getDevice(
605 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100606 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800607 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700608 device = availableDevices.getDevice(
609 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800610 break;
François Gaffie2110e042015-03-24 08:41:51 +0100611 default:
612 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
613 break;
614 }
jiabinf502d152019-08-07 14:43:33 -0700615 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700616 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700617 device = availableDevices.getDevice(
618 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
619 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700620 "getDeviceForInputSource() no default device defined");
621 }
jiabinf502d152019-08-07 14:43:33 -0700622 ALOGV_IF(device != nullptr,
623 "getDeviceForInputSource()input source %d, device %08x",
624 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100625 return device;
626}
627
François Gaffiedc7553f2018-11-02 10:39:57 +0100628void Engine::updateDeviceSelectionCache()
629{
630 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700631 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100632 auto devices = getDevicesForProductStrategy(strategy->getId());
633 mDevicesForStrategies[strategy->getId()] = devices;
634 strategy->setDeviceTypes(devices.types());
635 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
636 }
637}
638
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700639DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
François Gaffiedc7553f2018-11-02 10:39:57 +0100640 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100641
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700642 // check if this strategy has a preferred device that is available,
643 // if yes, give priority to it
644 AudioDeviceTypeAddr preferredStrategyDevice;
645 const status_t status = getPreferredDeviceForStrategy(strategy, preferredStrategyDevice);
646 if (status == NO_ERROR) {
647 // there is a preferred device, is it available?
648 sp<DeviceDescriptor> preferredAvailableDevDescr = availableOutputDevices.getDevice(
649 preferredStrategyDevice.mType,
650 String8(preferredStrategyDevice.mAddress.c_str()),
651 AUDIO_FORMAT_DEFAULT);
652 if (preferredAvailableDevDescr != nullptr) {
jiabincd510522020-01-22 09:40:55 -0800653 ALOGVV("%s using pref device 0x%08x/%s for strategy %u",
654 __func__, preferredStrategyDevice.mType,
655 preferredStrategyDevice.mAddress.c_str(), strategy);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700656 return DeviceVector(preferredAvailableDevDescr);
657 }
658 }
659
660 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
661 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100662 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700663 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabinf502d152019-08-07 14:43:33 -0700664 return getDevicesForStrategyInt(legacyStrategy,
665 availableOutputDevices,
666 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100667}
668
669DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
670 const sp<DeviceDescriptor> &preferredDevice,
671 bool fromCache) const
672{
673 // First check for explict routing device
674 if (preferredDevice != nullptr) {
675 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
676 return DeviceVector(preferredDevice);
677 }
François Gaffiec005e562018-11-06 15:04:49 +0100678 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700679 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100680 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100681 //
682 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
683 // be by APM?
684 //
François Gaffiec005e562018-11-06 15:04:49 +0100685 // Honor explicit routing requests only if all active clients have a preferred route in which
686 // case the last active client route is used
687 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
688 if (device != nullptr) {
689 return DeviceVector(device);
690 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100691
692 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
693}
694
695DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
696{
697 auto attributes = getAttributesForStreamType(stream);
698 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
699}
700
701sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800702 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100703{
François Gaffiec005e562018-11-06 15:04:49 +0100704 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700705 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100706 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100707 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100708
François Gaffiedc7553f2018-11-02 10:39:57 +0100709 //
François Gaffiec005e562018-11-06 15:04:49 +0100710 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
711 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100712 //
François Gaffiec005e562018-11-06 15:04:49 +0100713 // Honor explicit routing requests only if all active clients have a preferred route in which
714 // case the last active client route is used
715 sp<DeviceDescriptor> device =
716 findPreferredDevice(inputs, attr.source, availableInputDevices);
717 if (device != nullptr) {
718 return device;
719 }
720
721 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
722 if (device != nullptr) {
723 return device;
724 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100725
jiabinf502d152019-08-07 14:43:33 -0700726 device = getDeviceForInputSource(attr.source);
727 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
728 // Return immediately if the device is null or it is not a remote submix device.
729 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100730 }
jiabinf502d152019-08-07 14:43:33 -0700731
732 // For remote submix device, try to find the device by address.
733 address = "0";
734 std::size_t pos;
735 std::string tags { attr.tags };
736 if ((pos = tags.find("addr=")) != std::string::npos) {
737 address = tags.substr(pos + std::strlen("addr="));
738 }
739 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100740 String8(address.c_str()),
741 AUDIO_FORMAT_DEFAULT);
742}
743
François Gaffie2110e042015-03-24 08:41:51 +0100744} // namespace audio_policy
745} // namespace android
746
747