blob: eccde7b4f11c86b47068c4724e783300542ddf84 [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 Laurenta9986862020-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 Laurenta9986862020-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 Laurenta9986862020-10-07 08:42:28 -0700275 if (!isInCall()) {
jiabin9a3361e2019-10-01 09:38:30 -0700276 devices = availableOutputDevices.getDevicesFromType(
Eric Laurenta9986862020-10-07 08:42:28 -0700277 AUDIO_DEVICE_OUT_BLE_SPEAKER);
jiabinf502d152019-08-07 14:43:33 -0700278 if (!devices.isEmpty()) break;
Eric Laurenta9986862020-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 Laurenta9986862020-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 Laurenta9986862020-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
Eric Laurentd9766932020-08-07 14:01:22 -0700459 case STRATEGY_NONE:
460 // Happens when internal strategies are processed ("rerouting", "patch"...)
461 break;
462
François Gaffie2110e042015-03-24 08:41:51 +0100463 default:
Eric Laurentd9766932020-08-07 14:01:22 -0700464 ALOGW("%s unknown strategy: %d", __func__, strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100465 break;
466 }
467
jiabinf502d152019-08-07 14:43:33 -0700468 if (devices.isEmpty()) {
Eric Laurentd9766932020-08-07 14:01:22 -0700469 ALOGV("%s no device found for strategy %d", __func__, strategy);
jiabinf502d152019-08-07 14:43:33 -0700470 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
471 if (defaultOutputDevice != nullptr) {
472 devices.add(defaultOutputDevice);
473 }
474 ALOGE_IF(devices.isEmpty(),
Eric Laurentd9766932020-08-07 14:01:22 -0700475 "%s no default device defined", __func__);
Eric Laurent5a2b6292016-04-14 18:05:57 -0700476 }
jiabinf502d152019-08-07 14:43:33 -0700477
Eric Laurentd9766932020-08-07 14:01:22 -0700478 ALOGVV("%s strategy %d, device %s", __func__,
jiabincd510522020-01-22 09:40:55 -0800479 strategy, dumpDeviceTypes(devices.types()).c_str());
jiabinf502d152019-08-07 14:43:33 -0700480 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100481}
482
483
jiabinf502d152019-08-07 14:43:33 -0700484sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100485{
Eric Laurentaf377772019-03-29 14:50:21 -0700486 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
487 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100488 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabinf502d152019-08-07 14:43:33 -0700489 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700490 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabinb6b07482019-09-26 18:05:18 -0700491 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
492 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
jiabinf502d152019-08-07 14:43:33 -0700493 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100494
Eric Laurentdc95a252018-04-12 12:46:56 -0700495 // when a call is active, force device selection to match source VOICE_COMMUNICATION
496 // for most other input sources to avoid rerouting call TX audio
497 if (isInCall()) {
498 switch (inputSource) {
499 case AUDIO_SOURCE_DEFAULT:
500 case AUDIO_SOURCE_MIC:
501 case AUDIO_SOURCE_VOICE_RECOGNITION:
502 case AUDIO_SOURCE_UNPROCESSED:
503 case AUDIO_SOURCE_HOTWORD:
504 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800505 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700506 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
507 break;
508 default:
509 break;
510 }
511 }
512
François Gaffie2110e042015-03-24 08:41:51 +0100513 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100514 case AUDIO_SOURCE_DEFAULT:
515 case AUDIO_SOURCE_MIC:
jiabinf502d152019-08-07 14:43:33 -0700516 device = availableDevices.getDevice(
517 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
518 if (device != nullptr) break;
519 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
520 device = availableDevices.getDevice(
521 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
522 if (device != nullptr) break;
523 }
524 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700525 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
526 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
527 AUDIO_DEVICE_IN_BUILTIN_MIC});
jiabinf502d152019-08-07 14:43:33 -0700528 break;
François Gaffie2110e042015-03-24 08:41:51 +0100529
530 case AUDIO_SOURCE_VOICE_COMMUNICATION:
531 // Allow only use of devices on primary input if in call and HAL does not support routing
532 // to voice call path.
533 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabinf502d152019-08-07 14:43:33 -0700534 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
535 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
jiabinb6b07482019-09-26 18:05:18 -0700536 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700537 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100538 }
539
François Gaffiedc7553f2018-11-02 10:39:57 +0100540 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100541 case AUDIO_POLICY_FORCE_BT_SCO:
542 // if SCO device is requested but no SCO device is available, fall back to default case
jiabinf502d152019-08-07 14:43:33 -0700543 device = availableDevices.getDevice(
544 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
545 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100546 break;
547 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700548 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100549
550 default: // FORCE_NONE
Eric Laurenta9986862020-10-07 08:42:28 -0700551 // TODO (b/161358428): remove AUDIO_DEVICE_IN_BLE_HEADSET from the list
552 // when preferred device for strategy phone will be used instead of
553 // AUDIO_POLICY_FORCE_FOR_COMMUNICATION.
jiabinf502d152019-08-07 14:43:33 -0700554 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700555 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
556 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
557 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100558 break;
559
560 case AUDIO_POLICY_FORCE_SPEAKER:
jiabinf502d152019-08-07 14:43:33 -0700561 device = availableDevices.getFirstExistingDevice({
562 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100563 break;
564 }
565 break;
566
567 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800568 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100569 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700570 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabinb6b07482019-09-26 18:05:18 -0700571 // We should not use primary output criteria for Hotword but rather limit
572 // to devices attached to the same HW module as the build in mic
573 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
jiabinf502d152019-08-07 14:43:33 -0700574 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700575 }
jiabinf502d152019-08-07 14:43:33 -0700576 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
577 device = availableDevices.getDevice(
578 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
579 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100580 }
jiabinf502d152019-08-07 14:43:33 -0700581 device = availableDevices.getFirstExistingDevice({
Eric Laurenta9986862020-10-07 08:42:28 -0700582 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
583 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
584 AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100585 break;
586 case AUDIO_SOURCE_CAMCORDER:
jiabinf502d152019-08-07 14:43:33 -0700587 // For a device without built-in mic, adding usb device
588 device = availableDevices.getFirstExistingDevice({
589 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
590 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100591 break;
592 case AUDIO_SOURCE_VOICE_DOWNLINK:
593 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700594 case AUDIO_SOURCE_VOICE_UPLINK:
jiabinf502d152019-08-07 14:43:33 -0700595 device = availableDevices.getDevice(
596 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100597 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800598 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabinf502d152019-08-07 14:43:33 -0700599 device = availableDevices.getFirstExistingDevice({
600 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
601 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800602 break;
François Gaffie2110e042015-03-24 08:41:51 +0100603 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabinf502d152019-08-07 14:43:33 -0700604 device = availableDevices.getDevice(
605 AUDIO_DEVICE_IN_REMOTE_SUBMIX, 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_FM_TUNER:
jiabinf502d152019-08-07 14:43:33 -0700608 device = availableDevices.getDevice(
609 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100610 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800611 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabinf502d152019-08-07 14:43:33 -0700612 device = availableDevices.getDevice(
613 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800614 break;
François Gaffie2110e042015-03-24 08:41:51 +0100615 default:
616 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
617 break;
618 }
jiabinf502d152019-08-07 14:43:33 -0700619 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700620 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabinf502d152019-08-07 14:43:33 -0700621 device = availableDevices.getDevice(
622 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
623 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700624 "getDeviceForInputSource() no default device defined");
625 }
jiabinf502d152019-08-07 14:43:33 -0700626 ALOGV_IF(device != nullptr,
627 "getDeviceForInputSource()input source %d, device %08x",
628 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100629 return device;
630}
631
François Gaffiedc7553f2018-11-02 10:39:57 +0100632void Engine::updateDeviceSelectionCache()
633{
634 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700635 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100636 auto devices = getDevicesForProductStrategy(strategy->getId());
637 mDevicesForStrategies[strategy->getId()] = devices;
638 strategy->setDeviceTypes(devices.types());
639 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
640 }
641}
642
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700643DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
François Gaffiedc7553f2018-11-02 10:39:57 +0100644 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100645
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700646 // check if this strategy has a preferred device that is available,
647 // if yes, give priority to it
jiabin0a488932020-08-07 17:32:40 -0700648 AudioDeviceTypeAddrVector preferredStrategyDevices;
649 const status_t status = getDevicesForRoleAndStrategy(
650 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700651 if (status == NO_ERROR) {
652 // there is a preferred device, is it available?
jiabin0a488932020-08-07 17:32:40 -0700653 DeviceVector preferredAvailableDevVec =
654 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
655 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
656 ALOGVV("%s using pref device %s for strategy %u",
657 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
658 return preferredAvailableDevVec;
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700659 }
660 }
661
662 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
663 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100664 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
Jean-Michel Trivi30857152019-11-01 11:04:15 -0700665 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabinf502d152019-08-07 14:43:33 -0700666 return getDevicesForStrategyInt(legacyStrategy,
667 availableOutputDevices,
668 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100669}
670
671DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
672 const sp<DeviceDescriptor> &preferredDevice,
673 bool fromCache) const
674{
675 // First check for explict routing device
676 if (preferredDevice != nullptr) {
677 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
678 return DeviceVector(preferredDevice);
679 }
François Gaffiec005e562018-11-06 15:04:49 +0100680 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700681 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100682 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100683 //
684 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
685 // be by APM?
686 //
François Gaffiec005e562018-11-06 15:04:49 +0100687 // Honor explicit routing requests only if all active clients have a preferred route in which
688 // case the last active client route is used
689 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
690 if (device != nullptr) {
691 return DeviceVector(device);
692 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100693
694 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
695}
696
697DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
698{
699 auto attributes = getAttributesForStreamType(stream);
700 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
701}
702
703sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800704 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100705{
François Gaffiec005e562018-11-06 15:04:49 +0100706 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700707 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100708 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100709 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100710
François Gaffiedc7553f2018-11-02 10:39:57 +0100711 //
François Gaffiec005e562018-11-06 15:04:49 +0100712 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
713 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100714 //
François Gaffiec005e562018-11-06 15:04:49 +0100715 // Honor explicit routing requests only if all active clients have a preferred route in which
716 // case the last active client route is used
717 sp<DeviceDescriptor> device =
718 findPreferredDevice(inputs, attr.source, availableInputDevices);
719 if (device != nullptr) {
720 return device;
721 }
722
723 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
724 if (device != nullptr) {
725 return device;
726 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100727
jiabinf502d152019-08-07 14:43:33 -0700728 device = getDeviceForInputSource(attr.source);
729 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
730 // Return immediately if the device is null or it is not a remote submix device.
731 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100732 }
jiabinf502d152019-08-07 14:43:33 -0700733
734 // For remote submix device, try to find the device by address.
735 address = "0";
736 std::size_t pos;
737 std::string tags { attr.tags };
738 if ((pos = tags.find("addr=")) != std::string::npos) {
739 address = tags.substr(pos + std::strlen("addr="));
740 }
741 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100742 String8(address.c_str()),
743 AUDIO_FORMAT_DEFAULT);
744}
745
François Gaffie2110e042015-03-24 08:41:51 +0100746} // namespace audio_policy
747} // namespace android
748
749