blob: a3fa974c9c32533167315be757c880e0af63e826 [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
Eric Laurent21777f82019-12-06 18:12:06 -080056 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
François Gaffiedc7553f2018-11-02 10:39:57 +010057};
58
François Gaffie2110e042015-03-24 08:41:51 +010059Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010060{
François Gaffiedc7553f2018-11-02 10:39:57 +010061 auto result = EngineBase::loadAudioPolicyEngineConfig();
62 ALOGE_IF(result.nbSkippedElement != 0,
63 "Policy Engine configuration is partially invalid, skipped %zu elements",
64 result.nbSkippedElement);
65
66 for (const auto &strategy : gLegacyStrategy) {
67 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010068 }
69}
70
François Gaffie2110e042015-03-24 08:41:51 +010071status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
72{
73 switch(usage) {
74 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
75 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
76 config != AUDIO_POLICY_FORCE_NONE) {
77 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
78 return BAD_VALUE;
79 }
François Gaffie2110e042015-03-24 08:41:51 +010080 break;
81 case AUDIO_POLICY_FORCE_FOR_MEDIA:
82 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
83 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
84 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
85 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
86 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
87 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
88 return BAD_VALUE;
89 }
François Gaffie2110e042015-03-24 08:41:51 +010090 break;
91 case AUDIO_POLICY_FORCE_FOR_RECORD:
92 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
93 config != AUDIO_POLICY_FORCE_NONE) {
94 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
95 return BAD_VALUE;
96 }
François Gaffie2110e042015-03-24 08:41:51 +010097 break;
98 case AUDIO_POLICY_FORCE_FOR_DOCK:
99 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
100 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
101 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
102 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
103 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
104 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
105 }
François Gaffie2110e042015-03-24 08:41:51 +0100106 break;
107 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
108 if (config != AUDIO_POLICY_FORCE_NONE &&
109 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
110 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
111 }
François Gaffie2110e042015-03-24 08:41:51 +0100112 break;
113 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
114 if (config != AUDIO_POLICY_FORCE_NONE &&
115 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800116 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
117 }
Phil Burk09bc4612016-02-24 15:58:15 -0800118 break;
119 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
120 if (config != AUDIO_POLICY_FORCE_NONE &&
121 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700122 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
123 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800124 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
125 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100126 }
François Gaffie2110e042015-03-24 08:41:51 +0100127 break;
Jack He96117ae2018-02-12 20:52:53 -0800128 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
129 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
130 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
131 return BAD_VALUE;
132 }
Jack He96117ae2018-02-12 20:52:53 -0800133 break;
François Gaffie2110e042015-03-24 08:41:51 +0100134 default:
135 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800136 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100137 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100138 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100139}
140
jiabinf502d152019-08-07 14:43:33 -0700141DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
142 DeviceVector availableOutputDevices,
143 DeviceVector availableInputDevices,
144 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800145{
jiabinf502d152019-08-07 14:43:33 -0700146 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100147
148 switch (strategy) {
149
150 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin9a3361e2019-10-01 09:38:30 -0700151 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100152 break;
153
154 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700155 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700156 devices = getDevicesForStrategyInt(
157 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100158 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800159 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700160 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100161 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
162 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700163 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100164 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800165 // routing is same as media without the "remote" device
jiabin9a3361e2019-10-01 09:38:30 -0700166 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700167 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
168 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800169 availableOutputDevices,
jiabinf502d152019-08-07 14:43:33 -0700170 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800171 // if no media is playing on the device, check for mandatory use of "safe" speaker
172 // when media would have played on speaker, and the safe speaker path is available
jiabinf502d152019-08-07 14:43:33 -0700173 if (!media_active_locally) {
174 devices.replaceDevicesByType(
175 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700176 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700177 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700178 }
François Gaffie2110e042015-03-24 08:41:51 +0100179 }
180 break;
181
182 case STRATEGY_DTMF:
183 if (!isInCall()) {
184 // when off call, DTMF strategy follows the same rules as MEDIA strategy
jiabinf502d152019-08-07 14:43:33 -0700185 devices = getDevicesForStrategyInt(
186 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100187 break;
188 }
189 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700190 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100191
192 case STRATEGY_PHONE:
193 // Force use of only devices on primary output if:
194 // - in call AND
195 // - cannot route from voice call RX OR
196 // - audio HAL version is < 3.0 and TX device is on the primary HW module
197 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabinf502d152019-08-07 14:43:33 -0700198 audio_devices_t txDevice = getDeviceForInputSource(
199 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100200 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700201 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
jiabinf502d152019-08-07 14:43:33 -0700202 DeviceVector availPrimaryInputDevices =
203 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800204
205 // TODO: getPrimaryOutput return only devices from first module in
206 // audio_policy_configuration.xml, hearing aid is not there, but it's
207 // a primary device
208 // FIXME: this is not the right way of solving this problem
jiabin9a3361e2019-10-01 09:38:30 -0700209 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabinf502d152019-08-07 14:43:33 -0700210 primaryOutput->supportedDevices().types());
211 availPrimaryOutputDevices.add(
jiabin9a3361e2019-10-01 09:38:30 -0700212 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100213
jiabinf502d152019-08-07 14:43:33 -0700214 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
215 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
216 ((availPrimaryInputDevices.getDevice(
217 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
jiabin4ef93452019-09-10 14:29:54 -0700218 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabinf502d152019-08-07 14:43:33 -0700219 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100220 }
221 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800222 // for phone strategy, we first consider the forced use and then the available devices by
223 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100224 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100225 case AUDIO_POLICY_FORCE_BT_SCO:
226 if (!isInCall() || strategy != STRATEGY_DTMF) {
jiabin9a3361e2019-10-01 09:38:30 -0700227 devices = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700228 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
229 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100230 }
jiabinf502d152019-08-07 14:43:33 -0700231 devices = availableOutputDevices.getFirstDevicesFromTypes({
232 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
233 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100234 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700235 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100236
237 default: // FORCE_NONE
jiabin9a3361e2019-10-01 09:38:30 -0700238 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
jiabinf502d152019-08-07 14:43:33 -0700239 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100240 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
241 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100242 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800243 outputs.isA2dpSupported()) {
jiabinf502d152019-08-07 14:43:33 -0700244 devices = availableOutputDevices.getFirstDevicesFromTypes({
245 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
246 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
247 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100248 }
jiabinf502d152019-08-07 14:43:33 -0700249 devices = availableOutputDevices.getFirstDevicesFromTypes({
250 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET,
251 AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET,
252 AUDIO_DEVICE_OUT_USB_DEVICE});
253 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100254 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700255 devices = availableOutputDevices.getFirstDevicesFromTypes({
256 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
257 AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
258 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100259 }
jiabin9a3361e2019-10-01 09:38:30 -0700260 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
François Gaffie2110e042015-03-24 08:41:51 +0100261 break;
262
263 case AUDIO_POLICY_FORCE_SPEAKER:
264 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
265 // A2DP speaker when forcing to speaker output
266 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100267 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800268 outputs.isA2dpSupported()) {
jiabin9a3361e2019-10-01 09:38:30 -0700269 devices = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700270 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
271 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100272 }
273 if (!isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700274 devices = availableOutputDevices.getFirstDevicesFromTypes({
275 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
276 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL,
277 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
278 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100279 }
jiabin9a3361e2019-10-01 09:38:30 -0700280 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100281 break;
282 }
283 break;
284
285 case STRATEGY_SONIFICATION:
286
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700287 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100288 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700289 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabinf502d152019-08-07 14:43:33 -0700290 devices = getDevicesForStrategyInt(
291 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100292 break;
293 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700294 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100295
296 case STRATEGY_ENFORCED_AUDIBLE:
297 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
298 // except:
299 // - when in call where it doesn't default to STRATEGY_PHONE behavior
300 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
301
302 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100303 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700304 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100305 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800306
307 // if SCO headset is connected and we are told to use it, play ringtone over
308 // speaker and BT SCO
jiabin9a3361e2019-10-01 09:38:30 -0700309 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabinf502d152019-08-07 14:43:33 -0700310 DeviceVector devices2;
311 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
312 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
313 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800314 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100315 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800316 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100317 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800318 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700319 if (!devices2.isEmpty()) {
320 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800321 break;
322 }
323 }
324 }
325 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100326 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
jiabinf502d152019-08-07 14:43:33 -0700327 if (strategy == STRATEGY_SONIFICATION) {
328 devices.replaceDevicesByType(
329 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700330 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700331 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800332 }
jiabinf502d152019-08-07 14:43:33 -0700333 if (!devices2.isEmpty()) {
334 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800335 break;
336 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800337 }
338 }
François Gaffie2110e042015-03-24 08:41:51 +0100339 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700340 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100341
François Gaffie2110e042015-03-24 08:41:51 +0100342 case STRATEGY_ACCESSIBILITY:
343 if (strategy == STRATEGY_ACCESSIBILITY) {
344 // do not route accessibility prompts to a digital output currently configured with a
345 // compressed format as they would likely not be mixed and dropped.
346 for (size_t i = 0; i < outputs.size(); i++) {
347 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabin5740f082019-08-19 15:08:30 -0700348 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin9a3361e2019-10-01 09:38:30 -0700349 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
350 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
351 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100352 }
353 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700354 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
355 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabinf502d152019-08-07 14:43:33 -0700356 return getDevicesForStrategyInt(
357 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800358 }
359 if (isInCall()) {
jiabinf502d152019-08-07 14:43:33 -0700360 return getDevicesForStrategyInt(
361 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800362 }
François Gaffie2110e042015-03-24 08:41:51 +0100363 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800364 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700365 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100366
Eric Laurent28d09f02016-03-08 10:43:05 -0800367 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100368 case STRATEGY_REROUTING:
369 case STRATEGY_MEDIA: {
jiabinf502d152019-08-07 14:43:33 -0700370 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100371 if (strategy != STRATEGY_SONIFICATION) {
372 // no sonification on remote submix (e.g. WFD)
jiabinf502d152019-08-07 14:43:33 -0700373 sp<DeviceDescriptor> remoteSubmix;
374 if ((remoteSubmix = availableOutputDevices.getDevice(
375 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
376 AUDIO_FORMAT_DEFAULT)) != nullptr) {
377 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100378 }
379 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700380 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabinf502d152019-08-07 14:43:33 -0700381 devices = getDevicesForStrategyInt(
382 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700383 break;
384 }
juyuchenebebb5f2019-01-11 14:41:04 +0800385 // FIXME: Find a better solution to prevent routing to BT hearing aid(b/122931261).
jiabinf502d152019-08-07 14:43:33 -0700386 if ((devices2.isEmpty()) &&
juyuchenebebb5f2019-01-11 14:41:04 +0800387 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
jiabin9a3361e2019-10-01 09:38:30 -0700388 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
Eric Laurent58a73fc2018-02-21 18:46:13 -0800389 }
jiabinf502d152019-08-07 14:43:33 -0700390 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100391 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin9a3361e2019-10-01 09:38:30 -0700392 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100393 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900394 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
395 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
396 outputs.isA2dpSupported()) {
397 // Get the last connected device of wired and bluetooth a2dp
398 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
399 getLastRemovableMediaDevices());
400 } else {
401 // Get the last connected device of wired except bluetooth a2dp
402 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
403 getLastRemovableMediaDevices(GROUP_WIRED));
404 }
François Gaffie2110e042015-03-24 08:41:51 +0100405 }
jiabinf502d152019-08-07 14:43:33 -0700406 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100407 // no sonification on aux digital (e.g. HDMI)
jiabin9a3361e2019-10-01 09:38:30 -0700408 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100409 }
jiabinf502d152019-08-07 14:43:33 -0700410 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100411 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin9a3361e2019-10-01 09:38:30 -0700412 devices2 = availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700413 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100414 }
jiabinf502d152019-08-07 14:43:33 -0700415 if (devices2.isEmpty()) {
jiabin9a3361e2019-10-01 09:38:30 -0700416 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100417 }
jiabinf502d152019-08-07 14:43:33 -0700418 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100419 if (strategy == STRATEGY_MEDIA) {
420 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin9a3361e2019-10-01 09:38:30 -0700421 devices3 = availableOutputDevices.getDevicesFromTypes({
422 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100423 }
424
jiabinf502d152019-08-07 14:43:33 -0700425 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100426 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
427 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabinf502d152019-08-07 14:43:33 -0700428 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100429
430 // If hdmi system audio mode is on, remove speaker out of output list.
431 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100432 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100433 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin9a3361e2019-10-01 09:38:30 -0700434 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100435 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700436
437 // for STRATEGY_SONIFICATION:
438 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabinf502d152019-08-07 14:43:33 -0700439 if (strategy == STRATEGY_SONIFICATION) {
440 devices.replaceDevicesByType(
441 AUDIO_DEVICE_OUT_SPEAKER,
jiabin9a3361e2019-10-01 09:38:30 -0700442 availableOutputDevices.getDevicesFromType(
jiabinf502d152019-08-07 14:43:33 -0700443 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700444 }
François Gaffie2110e042015-03-24 08:41:51 +0100445 } break;
446
Eric Laurent21777f82019-12-06 18:12:06 -0800447 case STRATEGY_CALL_ASSISTANT:
448 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
449 break;
450
François Gaffie2110e042015-03-24 08:41:51 +0100451 default:
jiabinf502d152019-08-07 14:43:33 -0700452 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100453 break;
454 }
455
jiabinf502d152019-08-07 14:43:33 -0700456 if (devices.isEmpty()) {
457 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
458 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
459 if (defaultOutputDevice != nullptr) {
460 devices.add(defaultOutputDevice);
461 }
462 ALOGE_IF(devices.isEmpty(),
463 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700464 }
jiabinf502d152019-08-07 14:43:33 -0700465
466 ALOGVV("getDevices"
467 "ForStrategy() strategy %d, device %x", strategy, devices.types());
468 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100469}
470
471
jiabinf502d152019-08-07 14:43:33 -0700472sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100473{
Eric Laurentaf377772019-03-29 14:50:21 -0700474 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
475 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100476 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700477 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700478 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700479 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
480 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700481 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100482
Eric Laurentdc95a252018-04-12 12:46:56 -0700483 // when a call is active, force device selection to match source VOICE_COMMUNICATION
484 // for most other input sources to avoid rerouting call TX audio
485 if (isInCall()) {
486 switch (inputSource) {
487 case AUDIO_SOURCE_DEFAULT:
488 case AUDIO_SOURCE_MIC:
489 case AUDIO_SOURCE_VOICE_RECOGNITION:
490 case AUDIO_SOURCE_UNPROCESSED:
491 case AUDIO_SOURCE_HOTWORD:
492 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800493 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700494 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
495 break;
496 default:
497 break;
498 }
499 }
500
François Gaffie2110e042015-03-24 08:41:51 +0100501 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100502 case AUDIO_SOURCE_DEFAULT:
503 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700504 device = availableDevices.getDevice(
505 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
506 if (device != nullptr) break;
507 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
508 device = availableDevices.getDevice(
509 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
510 if (device != nullptr) break;
511 }
512 device = availableDevices.getFirstExistingDevice({
513 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
514 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
515 break;
François Gaffie2110e042015-03-24 08:41:51 +0100516
517 case AUDIO_SOURCE_VOICE_COMMUNICATION:
518 // Allow only use of devices on primary input if in call and HAL does not support routing
519 // to voice call path.
520 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700521 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
522 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700523 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700524 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100525 }
526
François Gaffiedc7553f2018-11-02 10:39:57 +0100527 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100528 case AUDIO_POLICY_FORCE_BT_SCO:
529 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700530 device = availableDevices.getDevice(
531 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
532 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100533 break;
534 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700535 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100536
537 default: // FORCE_NONE
jiabinf502d152019-08-07 14:43:33 -0700538 device = availableDevices.getFirstExistingDevice({
539 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
540 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100541 break;
542
543 case AUDIO_POLICY_FORCE_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700544 device = availableDevices.getFirstExistingDevice({
545 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100546 break;
547 }
548 break;
549
550 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800551 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100552 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700553 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700554 // We should not use primary output criteria for Hotword but rather limit
555 // to devices attached to the same HW module as the build in mic
556 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700557 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700558 }
jiabinf502d152019-08-07 14:43:33 -0700559 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
560 device = availableDevices.getDevice(
561 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
562 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100563 }
jiabinf502d152019-08-07 14:43:33 -0700564 device = availableDevices.getFirstExistingDevice({
565 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
566 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100567 break;
568 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700569 // For a device without built-in mic, adding usb device
570 device = availableDevices.getFirstExistingDevice({
571 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
572 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100573 break;
574 case AUDIO_SOURCE_VOICE_DOWNLINK:
575 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700576 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700577 device = availableDevices.getDevice(
578 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100579 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800580 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700581 device = availableDevices.getFirstExistingDevice({
582 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
583 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800584 break;
François Gaffie2110e042015-03-24 08:41:51 +0100585 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700586 device = availableDevices.getDevice(
587 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100588 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800589 case AUDIO_SOURCE_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700590 device = availableDevices.getDevice(
591 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100592 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800593 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700594 device = availableDevices.getDevice(
595 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800596 break;
François Gaffie2110e042015-03-24 08:41:51 +0100597 default:
598 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
599 break;
600 }
jiabinf502d152019-08-07 14:43:33 -0700601 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700602 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700603 device = availableDevices.getDevice(
604 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
605 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700606 "getDeviceForInputSource() no default device defined");
607 }
jiabinf502d152019-08-07 14:43:33 -0700608 ALOGV_IF(device != nullptr,
609 "getDeviceForInputSource()input source %d, device %08x",
610 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100611 return device;
612}
613
François Gaffiedc7553f2018-11-02 10:39:57 +0100614void Engine::updateDeviceSelectionCache()
615{
616 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700617 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100618 auto devices = getDevicesForProductStrategy(strategy->getId());
619 mDevicesForStrategies[strategy->getId()] = devices;
620 strategy->setDeviceTypes(devices.types());
621 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
622 }
623}
624
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700625DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
François Gaffiedc7553f2018-11-02 10:39:57 +0100626 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100627
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700628 // check if this strategy has a preferred device that is available,
629 // if yes, give priority to it
630 AudioDeviceTypeAddr preferredStrategyDevice;
631 const status_t status = getPreferredDeviceForStrategy(strategy, preferredStrategyDevice);
632 if (status == NO_ERROR) {
633 // there is a preferred device, is it available?
634 sp<DeviceDescriptor> preferredAvailableDevDescr = availableOutputDevices.getDevice(
635 preferredStrategyDevice.mType,
636 String8(preferredStrategyDevice.mAddress.c_str()),
637 AUDIO_FORMAT_DEFAULT);
638 if (preferredAvailableDevDescr != nullptr) {
639 ALOGVV("%s using pref device 0x%08x/%s for strategy %u", __FUNCTION__,
640 preferredStrategyDevice.mType, preferredStrategyDevice.mAddress, strategy);
641 return DeviceVector(preferredAvailableDevDescr);
642 }
643 }
644
645 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
646 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100647 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700648 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabinf502d152019-08-07 14:43:33 -0700649 return getDevicesForStrategyInt(legacyStrategy,
650 availableOutputDevices,
651 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100652}
653
654DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
655 const sp<DeviceDescriptor> &preferredDevice,
656 bool fromCache) const
657{
658 // First check for explict routing device
659 if (preferredDevice != nullptr) {
660 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
661 return DeviceVector(preferredDevice);
662 }
François Gaffiec005e562018-11-06 15:04:49 +0100663 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700664 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100665 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100666 //
667 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
668 // be by APM?
669 //
François Gaffiec005e562018-11-06 15:04:49 +0100670 // Honor explicit routing requests only if all active clients have a preferred route in which
671 // case the last active client route is used
672 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
673 if (device != nullptr) {
674 return DeviceVector(device);
675 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100676
677 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
678}
679
680DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
681{
682 auto attributes = getAttributesForStreamType(stream);
683 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
684}
685
686sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800687 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100688{
François Gaffiec005e562018-11-06 15:04:49 +0100689 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700690 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100691 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100692 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100693
François Gaffiedc7553f2018-11-02 10:39:57 +0100694 //
François Gaffiec005e562018-11-06 15:04:49 +0100695 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
696 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100697 //
François Gaffiec005e562018-11-06 15:04:49 +0100698 // Honor explicit routing requests only if all active clients have a preferred route in which
699 // case the last active client route is used
700 sp<DeviceDescriptor> device =
701 findPreferredDevice(inputs, attr.source, availableInputDevices);
702 if (device != nullptr) {
703 return device;
704 }
705
706 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
707 if (device != nullptr) {
708 return device;
709 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100710
jiabinf502d152019-08-07 14:43:33 -0700711 device = getDeviceForInputSource(attr.source);
712 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
713 // Return immediately if the device is null or it is not a remote submix device.
714 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100715 }
jiabinf502d152019-08-07 14:43:33 -0700716
717 // For remote submix device, try to find the device by address.
718 address = "0";
719 std::size_t pos;
720 std::string tags { attr.tags };
721 if ((pos = tags.find("addr=")) != std::string::npos) {
722 address = tags.substr(pos + std::strlen("addr="));
723 }
724 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100725 String8(address.c_str()),
726 AUDIO_FORMAT_DEFAULT);
727}
728
François Gaffie2110e042015-03-24 08:41:51 +0100729} // namespace audio_policy
730} // namespace android
731
732