blob: 41bb4e405865ed197410264989cbdd0e20baf6cf [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>
jiabindd81cc22019-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>
jiabin12dc6b02019-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 Naganov80ae6b42020-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 };
58 return legacyStrategy;
François Gaffiedc7553f2018-11-02 10:39:57 +010059};
60
François Gaffie2110e042015-03-24 08:41:51 +010061Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010062{
François Gaffiedc7553f2018-11-02 10:39:57 +010063 auto result = EngineBase::loadAudioPolicyEngineConfig();
64 ALOGE_IF(result.nbSkippedElement != 0,
65 "Policy Engine configuration is partially invalid, skipped %zu elements",
66 result.nbSkippedElement);
67
Mikhail Naganov80ae6b42020-03-10 13:15:08 -070068 auto legacyStrategy = getLegacyStrategy();
69 for (const auto &strategy : legacyStrategy) {
François Gaffiedc7553f2018-11-02 10:39:57 +010070 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010071 }
72}
73
François Gaffie2110e042015-03-24 08:41:51 +010074status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
75{
76 switch(usage) {
77 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
78 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
79 config != AUDIO_POLICY_FORCE_NONE) {
80 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
81 return BAD_VALUE;
82 }
François Gaffie2110e042015-03-24 08:41:51 +010083 break;
84 case AUDIO_POLICY_FORCE_FOR_MEDIA:
85 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
86 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
87 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
88 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
89 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
90 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
91 return BAD_VALUE;
92 }
François Gaffie2110e042015-03-24 08:41:51 +010093 break;
94 case AUDIO_POLICY_FORCE_FOR_RECORD:
95 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
96 config != AUDIO_POLICY_FORCE_NONE) {
97 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
98 return BAD_VALUE;
99 }
François Gaffie2110e042015-03-24 08:41:51 +0100100 break;
101 case AUDIO_POLICY_FORCE_FOR_DOCK:
102 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
103 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
104 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
105 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
106 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
107 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
108 }
François Gaffie2110e042015-03-24 08:41:51 +0100109 break;
110 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
111 if (config != AUDIO_POLICY_FORCE_NONE &&
112 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
113 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
114 }
François Gaffie2110e042015-03-24 08:41:51 +0100115 break;
116 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
117 if (config != AUDIO_POLICY_FORCE_NONE &&
118 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800119 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
120 }
Phil Burk09bc4612016-02-24 15:58:15 -0800121 break;
122 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
123 if (config != AUDIO_POLICY_FORCE_NONE &&
124 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700125 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800127 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
128 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100129 }
François Gaffie2110e042015-03-24 08:41:51 +0100130 break;
Jack He96117ae2018-02-12 20:52:53 -0800131 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
132 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
133 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
134 return BAD_VALUE;
135 }
Jack He96117ae2018-02-12 20:52:53 -0800136 break;
François Gaffie2110e042015-03-24 08:41:51 +0100137 default:
138 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800139 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100140 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100141 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100142}
143
jiabin147f5c32019-08-07 14:43:33 -0700144DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
145 DeviceVector availableOutputDevices,
146 DeviceVector availableInputDevices,
147 const SwAudioOutputCollection &outputs) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800148{
jiabin147f5c32019-08-07 14:43:33 -0700149 DeviceVector devices;
François Gaffie2110e042015-03-24 08:41:51 +0100150
151 switch (strategy) {
152
153 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
jiabin12dc6b02019-10-01 09:38:30 -0700154 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100155 break;
156
157 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent83d17c22019-04-02 17:10:01 -0700158 if (isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabin147f5c32019-08-07 14:43:33 -0700159 devices = getDevicesForStrategyInt(
160 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100161 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800162 bool media_active_locally =
Eric Laurent83d17c22019-04-02 17:10:01 -0700163 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
François Gaffie1c878552018-11-22 16:53:21 +0100164 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
165 || outputs.isActiveLocally(
Eric Laurent83d17c22019-04-02 17:10:01 -0700166 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
François Gaffie1c878552018-11-22 16:53:21 +0100167 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800168 // routing is same as media without the "remote" device
jiabin12dc6b02019-10-01 09:38:30 -0700169 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700170 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
171 devices = getDevicesForStrategyInt(STRATEGY_MEDIA,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800172 availableOutputDevices,
jiabin147f5c32019-08-07 14:43:33 -0700173 availableInputDevices, outputs);
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800174 // if no media is playing on the device, check for mandatory use of "safe" speaker
175 // when media would have played on speaker, and the safe speaker path is available
jiabin147f5c32019-08-07 14:43:33 -0700176 if (!media_active_locally) {
177 devices.replaceDevicesByType(
178 AUDIO_DEVICE_OUT_SPEAKER,
jiabin12dc6b02019-10-01 09:38:30 -0700179 availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700180 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Eric Laurent9a7d9222015-07-02 15:30:23 -0700181 }
François Gaffie2110e042015-03-24 08:41:51 +0100182 }
183 break;
184
185 case STRATEGY_DTMF:
186 if (!isInCall()) {
187 // when off call, DTMF strategy follows the same rules as MEDIA strategy
jiabin147f5c32019-08-07 14:43:33 -0700188 devices = getDevicesForStrategyInt(
189 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100190 break;
191 }
192 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700193 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100194
195 case STRATEGY_PHONE:
196 // Force use of only devices on primary output if:
197 // - in call AND
198 // - cannot route from voice call RX OR
199 // - audio HAL version is < 3.0 and TX device is on the primary HW module
200 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
jiabin147f5c32019-08-07 14:43:33 -0700201 audio_devices_t txDevice = getDeviceForInputSource(
202 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
François Gaffie2110e042015-03-24 08:41:51 +0100203 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabin147f5c32019-08-07 14:43:33 -0700204 DeviceVector availPrimaryInputDevices =
205 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800206
207 // TODO: getPrimaryOutput return only devices from first module in
208 // audio_policy_configuration.xml, hearing aid is not there, but it's
209 // a primary device
210 // FIXME: this is not the right way of solving this problem
jiabin12dc6b02019-10-01 09:38:30 -0700211 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
jiabin147f5c32019-08-07 14:43:33 -0700212 primaryOutput->supportedDevices().types());
213 availPrimaryOutputDevices.add(
jiabin12dc6b02019-10-01 09:38:30 -0700214 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
François Gaffie2110e042015-03-24 08:41:51 +0100215
jiabin147f5c32019-08-07 14:43:33 -0700216 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
217 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
218 ((availPrimaryInputDevices.getDevice(
219 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
jiabindff2a4f2019-09-10 14:29:54 -0700220 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
jiabin147f5c32019-08-07 14:43:33 -0700221 availableOutputDevices = availPrimaryOutputDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100222 }
223 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800224 // for phone strategy, we first consider the forced use and then the available devices by
225 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100226 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100227 case AUDIO_POLICY_FORCE_BT_SCO:
228 if (!isInCall() || strategy != STRATEGY_DTMF) {
jiabin12dc6b02019-10-01 09:38:30 -0700229 devices = availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700230 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT);
231 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100232 }
jiabin147f5c32019-08-07 14:43:33 -0700233 devices = availableOutputDevices.getFirstDevicesFromTypes({
234 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET, AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
235 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100236 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700237 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100238
239 default: // FORCE_NONE
jiabin12dc6b02019-10-01 09:38:30 -0700240 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
jiabin147f5c32019-08-07 14:43:33 -0700241 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100242 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
243 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100244 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800245 outputs.isA2dpSupported()) {
jiabin147f5c32019-08-07 14:43:33 -0700246 devices = availableOutputDevices.getFirstDevicesFromTypes({
247 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP,
248 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES});
249 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100250 }
jiabin147f5c32019-08-07 14:43:33 -0700251 devices = availableOutputDevices.getFirstDevicesFromTypes({
252 AUDIO_DEVICE_OUT_WIRED_HEADPHONE, AUDIO_DEVICE_OUT_WIRED_HEADSET,
253 AUDIO_DEVICE_OUT_LINE, AUDIO_DEVICE_OUT_USB_HEADSET,
254 AUDIO_DEVICE_OUT_USB_DEVICE});
255 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100256 if (!isInCall()) {
jiabin147f5c32019-08-07 14:43:33 -0700257 devices = availableOutputDevices.getFirstDevicesFromTypes({
258 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET,
259 AUDIO_DEVICE_OUT_AUX_DIGITAL, AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
260 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100261 }
jiabin12dc6b02019-10-01 09:38:30 -0700262 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
François Gaffie2110e042015-03-24 08:41:51 +0100263 break;
264
265 case AUDIO_POLICY_FORCE_SPEAKER:
266 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
267 // A2DP speaker when forcing to speaker output
268 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100269 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800270 outputs.isA2dpSupported()) {
jiabin12dc6b02019-10-01 09:38:30 -0700271 devices = availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700272 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
273 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100274 }
275 if (!isInCall()) {
jiabin147f5c32019-08-07 14:43:33 -0700276 devices = availableOutputDevices.getFirstDevicesFromTypes({
277 AUDIO_DEVICE_OUT_USB_ACCESSORY, AUDIO_DEVICE_OUT_USB_DEVICE,
278 AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET, AUDIO_DEVICE_OUT_AUX_DIGITAL,
279 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET});
280 if (!devices.isEmpty()) break;
François Gaffie2110e042015-03-24 08:41:51 +0100281 }
jiabin12dc6b02019-10-01 09:38:30 -0700282 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100283 break;
284 }
285 break;
286
287 case STRATEGY_SONIFICATION:
288
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700289 // If incall, just select the STRATEGY_PHONE device
François Gaffie1c878552018-11-22 16:53:21 +0100290 if (isInCall() ||
Eric Laurent83d17c22019-04-02 17:10:01 -0700291 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
jiabin147f5c32019-08-07 14:43:33 -0700292 devices = getDevicesForStrategyInt(
293 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100294 break;
295 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700296 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100297
298 case STRATEGY_ENFORCED_AUDIBLE:
299 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
300 // except:
301 // - when in call where it doesn't default to STRATEGY_PHONE behavior
302 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
303
304 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100305 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
jiabin12dc6b02019-10-01 09:38:30 -0700306 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100307 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800308
309 // if SCO headset is connected and we are told to use it, play ringtone over
310 // speaker and BT SCO
jiabin12dc6b02019-10-01 09:38:30 -0700311 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
jiabin147f5c32019-08-07 14:43:33 -0700312 DeviceVector devices2;
313 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
314 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
315 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
Jack He96117ae2018-02-12 20:52:53 -0800316 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100317 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800318 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100319 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800320 == AUDIO_POLICY_FORCE_BT_SCO) {
jiabin147f5c32019-08-07 14:43:33 -0700321 if (!devices2.isEmpty()) {
322 devices = devices2;
Jack He96117ae2018-02-12 20:52:53 -0800323 break;
324 }
325 }
326 }
327 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100328 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
jiabin147f5c32019-08-07 14:43:33 -0700329 if (strategy == STRATEGY_SONIFICATION) {
330 devices.replaceDevicesByType(
331 AUDIO_DEVICE_OUT_SPEAKER,
jiabin12dc6b02019-10-01 09:38:30 -0700332 availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700333 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
juyuchen5fa0aed2018-05-16 10:58:37 +0800334 }
jiabin147f5c32019-08-07 14:43:33 -0700335 if (!devices2.isEmpty()) {
336 devices.add(devices2);
Jack He96117ae2018-02-12 20:52:53 -0800337 break;
338 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800339 }
340 }
François Gaffie2110e042015-03-24 08:41:51 +0100341 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700342 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100343
François Gaffie2110e042015-03-24 08:41:51 +0100344 case STRATEGY_ACCESSIBILITY:
345 if (strategy == STRATEGY_ACCESSIBILITY) {
346 // do not route accessibility prompts to a digital output currently configured with a
347 // compressed format as they would likely not be mixed and dropped.
348 for (size_t i = 0; i < outputs.size(); i++) {
349 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
jiabineaf09f02019-08-19 15:08:30 -0700350 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
jiabin12dc6b02019-10-01 09:38:30 -0700351 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
352 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
353 AUDIO_DEVICE_OUT_HDMI_ARC}));
François Gaffie2110e042015-03-24 08:41:51 +0100354 }
355 }
Eric Laurent83d17c22019-04-02 17:10:01 -0700356 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
357 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
jiabin147f5c32019-08-07 14:43:33 -0700358 return getDevicesForStrategyInt(
359 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800360 }
361 if (isInCall()) {
jiabin147f5c32019-08-07 14:43:33 -0700362 return getDevicesForStrategyInt(
363 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurent28d09f02016-03-08 10:43:05 -0800364 }
François Gaffie2110e042015-03-24 08:41:51 +0100365 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800366 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700367 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100368
Eric Laurent28d09f02016-03-08 10:43:05 -0800369 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100370 case STRATEGY_REROUTING:
371 case STRATEGY_MEDIA: {
jiabin147f5c32019-08-07 14:43:33 -0700372 DeviceVector devices2;
François Gaffie2110e042015-03-24 08:41:51 +0100373 if (strategy != STRATEGY_SONIFICATION) {
374 // no sonification on remote submix (e.g. WFD)
jiabin147f5c32019-08-07 14:43:33 -0700375 sp<DeviceDescriptor> remoteSubmix;
376 if ((remoteSubmix = availableOutputDevices.getDevice(
377 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
378 AUDIO_FORMAT_DEFAULT)) != nullptr) {
379 devices2.add(remoteSubmix);
François Gaffie2110e042015-03-24 08:41:51 +0100380 }
381 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700382 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
jiabin147f5c32019-08-07 14:43:33 -0700383 devices = getDevicesForStrategyInt(
384 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700385 break;
386 }
juyuchenebebb5f2019-01-11 14:41:04 +0800387 // FIXME: Find a better solution to prevent routing to BT hearing aid(b/122931261).
jiabin147f5c32019-08-07 14:43:33 -0700388 if ((devices2.isEmpty()) &&
juyuchenebebb5f2019-01-11 14:41:04 +0800389 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
jiabin12dc6b02019-10-01 09:38:30 -0700390 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
Eric Laurent58a73fc2018-02-21 18:46:13 -0800391 }
jiabin147f5c32019-08-07 14:43:33 -0700392 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100393 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
jiabin12dc6b02019-10-01 09:38:30 -0700394 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100395 }
Baekgyeong Kim08451522019-11-01 20:40:02 +0900396 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
397 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
398 outputs.isA2dpSupported()) {
399 // Get the last connected device of wired and bluetooth a2dp
400 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
401 getLastRemovableMediaDevices());
402 } else {
403 // Get the last connected device of wired except bluetooth a2dp
404 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
405 getLastRemovableMediaDevices(GROUP_WIRED));
406 }
François Gaffie2110e042015-03-24 08:41:51 +0100407 }
jiabin147f5c32019-08-07 14:43:33 -0700408 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100409 // no sonification on aux digital (e.g. HDMI)
jiabin12dc6b02019-10-01 09:38:30 -0700410 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
François Gaffie2110e042015-03-24 08:41:51 +0100411 }
jiabin147f5c32019-08-07 14:43:33 -0700412 if ((devices2.isEmpty()) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100413 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
jiabin12dc6b02019-10-01 09:38:30 -0700414 devices2 = availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700415 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
François Gaffie2110e042015-03-24 08:41:51 +0100416 }
jiabin147f5c32019-08-07 14:43:33 -0700417 if (devices2.isEmpty()) {
jiabin12dc6b02019-10-01 09:38:30 -0700418 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
François Gaffie2110e042015-03-24 08:41:51 +0100419 }
jiabin147f5c32019-08-07 14:43:33 -0700420 DeviceVector devices3;
François Gaffie2110e042015-03-24 08:41:51 +0100421 if (strategy == STRATEGY_MEDIA) {
422 // ARC, SPDIF and AUX_LINE can co-exist with others.
jiabin12dc6b02019-10-01 09:38:30 -0700423 devices3 = availableOutputDevices.getDevicesFromTypes({
424 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE});
François Gaffie2110e042015-03-24 08:41:51 +0100425 }
426
jiabin147f5c32019-08-07 14:43:33 -0700427 devices2.add(devices3);
François Gaffie2110e042015-03-24 08:41:51 +0100428 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
429 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
jiabin147f5c32019-08-07 14:43:33 -0700430 devices.add(devices2);
François Gaffie2110e042015-03-24 08:41:51 +0100431
432 // If hdmi system audio mode is on, remove speaker out of output list.
433 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100434 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100435 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
jiabin12dc6b02019-10-01 09:38:30 -0700436 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
François Gaffie2110e042015-03-24 08:41:51 +0100437 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700438
439 // for STRATEGY_SONIFICATION:
440 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
jiabin147f5c32019-08-07 14:43:33 -0700441 if (strategy == STRATEGY_SONIFICATION) {
442 devices.replaceDevicesByType(
443 AUDIO_DEVICE_OUT_SPEAKER,
jiabin12dc6b02019-10-01 09:38:30 -0700444 availableOutputDevices.getDevicesFromType(
jiabin147f5c32019-08-07 14:43:33 -0700445 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700446 }
François Gaffie2110e042015-03-24 08:41:51 +0100447 } break;
448
449 default:
jiabin147f5c32019-08-07 14:43:33 -0700450 ALOGW("getDevicesForStrategy() unknown strategy: %d", strategy);
François Gaffie2110e042015-03-24 08:41:51 +0100451 break;
452 }
453
jiabin147f5c32019-08-07 14:43:33 -0700454 if (devices.isEmpty()) {
455 ALOGV("getDevicesForStrategy() no device found for strategy %d", strategy);
456 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
457 if (defaultOutputDevice != nullptr) {
458 devices.add(defaultOutputDevice);
459 }
460 ALOGE_IF(devices.isEmpty(),
461 "getDevicesForStrategy() no default device defined");
Eric Laurent5a2b6292016-04-14 18:05:57 -0700462 }
jiabin147f5c32019-08-07 14:43:33 -0700463
464 ALOGVV("getDevices"
465 "ForStrategy() strategy %d, device %x", strategy, devices.types());
466 return devices;
François Gaffie2110e042015-03-24 08:41:51 +0100467}
468
469
jiabin147f5c32019-08-07 14:43:33 -0700470sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
François Gaffie2110e042015-03-24 08:41:51 +0100471{
Eric Laurentaf377772019-03-29 14:50:21 -0700472 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
473 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100474 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
jiabin147f5c32019-08-07 14:43:33 -0700475 DeviceVector availableDevices = availableInputDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700476 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
jiabin147f5c32019-08-07 14:43:33 -0700477 DeviceVector availablePrimaryDevices = availableInputDevices.getDevicesFromHwModule(
478 primaryOutput->getModuleHandle());
479 sp<DeviceDescriptor> device;
François Gaffie2110e042015-03-24 08:41:51 +0100480
Eric Laurentdc95a252018-04-12 12:46:56 -0700481 // when a call is active, force device selection to match source VOICE_COMMUNICATION
482 // for most other input sources to avoid rerouting call TX audio
483 if (isInCall()) {
484 switch (inputSource) {
485 case AUDIO_SOURCE_DEFAULT:
486 case AUDIO_SOURCE_MIC:
487 case AUDIO_SOURCE_VOICE_RECOGNITION:
488 case AUDIO_SOURCE_UNPROCESSED:
489 case AUDIO_SOURCE_HOTWORD:
490 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800491 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700492 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
493 break;
494 default:
495 break;
496 }
497 }
498
François Gaffie2110e042015-03-24 08:41:51 +0100499 switch (inputSource) {
François Gaffie2110e042015-03-24 08:41:51 +0100500 case AUDIO_SOURCE_DEFAULT:
501 case AUDIO_SOURCE_MIC:
jiabin147f5c32019-08-07 14:43:33 -0700502 device = availableDevices.getDevice(
503 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
504 if (device != nullptr) break;
505 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
506 device = availableDevices.getDevice(
507 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
508 if (device != nullptr) break;
509 }
510 device = availableDevices.getFirstExistingDevice({
511 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
512 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
513 break;
François Gaffie2110e042015-03-24 08:41:51 +0100514
515 case AUDIO_SOURCE_VOICE_COMMUNICATION:
516 // Allow only use of devices on primary input if in call and HAL does not support routing
517 // to voice call path.
518 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
jiabin147f5c32019-08-07 14:43:33 -0700519 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
520 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
521 availableDevices = availablePrimaryDevices;
François Gaffie2110e042015-03-24 08:41:51 +0100522 }
523
François Gaffiedc7553f2018-11-02 10:39:57 +0100524 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100525 case AUDIO_POLICY_FORCE_BT_SCO:
526 // if SCO device is requested but no SCO device is available, fall back to default case
jiabin147f5c32019-08-07 14:43:33 -0700527 device = availableDevices.getDevice(
528 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
529 if (device != nullptr) {
François Gaffie2110e042015-03-24 08:41:51 +0100530 break;
531 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700532 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100533
534 default: // FORCE_NONE
jiabin147f5c32019-08-07 14:43:33 -0700535 device = availableDevices.getFirstExistingDevice({
536 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
537 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100538 break;
539
540 case AUDIO_POLICY_FORCE_SPEAKER:
jiabin147f5c32019-08-07 14:43:33 -0700541 device = availableDevices.getFirstExistingDevice({
542 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100543 break;
544 }
545 break;
546
547 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800548 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100549 case AUDIO_SOURCE_HOTWORD:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700550 if (inputSource == AUDIO_SOURCE_HOTWORD) {
jiabin147f5c32019-08-07 14:43:33 -0700551 availableDevices = availablePrimaryDevices;
Eric Laurent5ee8d122019-04-19 15:41:52 -0700552 }
jiabin147f5c32019-08-07 14:43:33 -0700553 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) {
554 device = availableDevices.getDevice(
555 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
556 if (device != nullptr) break;
François Gaffie2110e042015-03-24 08:41:51 +0100557 }
jiabin147f5c32019-08-07 14:43:33 -0700558 device = availableDevices.getFirstExistingDevice({
559 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
560 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
François Gaffie2110e042015-03-24 08:41:51 +0100561 break;
562 case AUDIO_SOURCE_CAMCORDER:
jiabin147f5c32019-08-07 14:43:33 -0700563 // For a device without built-in mic, adding usb device
564 device = availableDevices.getFirstExistingDevice({
565 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
566 AUDIO_DEVICE_IN_USB_DEVICE});
François Gaffie2110e042015-03-24 08:41:51 +0100567 break;
568 case AUDIO_SOURCE_VOICE_DOWNLINK:
569 case AUDIO_SOURCE_VOICE_CALL:
Eric Laurent5ee8d122019-04-19 15:41:52 -0700570 case AUDIO_SOURCE_VOICE_UPLINK:
jiabin147f5c32019-08-07 14:43:33 -0700571 device = availableDevices.getDevice(
572 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100573 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800574 case AUDIO_SOURCE_VOICE_PERFORMANCE:
jiabin147f5c32019-08-07 14:43:33 -0700575 device = availableDevices.getFirstExistingDevice({
576 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
577 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BUILTIN_MIC});
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800578 break;
François Gaffie2110e042015-03-24 08:41:51 +0100579 case AUDIO_SOURCE_REMOTE_SUBMIX:
jiabin147f5c32019-08-07 14:43:33 -0700580 device = availableDevices.getDevice(
581 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100582 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800583 case AUDIO_SOURCE_FM_TUNER:
jiabin147f5c32019-08-07 14:43:33 -0700584 device = availableDevices.getDevice(
585 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
François Gaffie2110e042015-03-24 08:41:51 +0100586 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800587 case AUDIO_SOURCE_ECHO_REFERENCE:
jiabin147f5c32019-08-07 14:43:33 -0700588 device = availableDevices.getDevice(
589 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800590 break;
François Gaffie2110e042015-03-24 08:41:51 +0100591 default:
592 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
593 break;
594 }
jiabin147f5c32019-08-07 14:43:33 -0700595 if (device == nullptr) {
Eric Laurent5a2b6292016-04-14 18:05:57 -0700596 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
jiabin147f5c32019-08-07 14:43:33 -0700597 device = availableDevices.getDevice(
598 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
599 ALOGE_IF(device == nullptr,
Eric Laurent5a2b6292016-04-14 18:05:57 -0700600 "getDeviceForInputSource() no default device defined");
601 }
jiabin147f5c32019-08-07 14:43:33 -0700602 ALOGV_IF(device != nullptr,
603 "getDeviceForInputSource()input source %d, device %08x",
604 inputSource, device->type());
François Gaffie2110e042015-03-24 08:41:51 +0100605 return device;
606}
607
François Gaffiedc7553f2018-11-02 10:39:57 +0100608void Engine::updateDeviceSelectionCache()
609{
610 for (const auto &iter : getProductStrategies()) {
Jean-Michel Trivi2deb4782019-11-01 11:04:15 -0700611 const auto& strategy = iter.second;
François Gaffiedc7553f2018-11-02 10:39:57 +0100612 auto devices = getDevicesForProductStrategy(strategy->getId());
613 mDevicesForStrategies[strategy->getId()] = devices;
614 strategy->setDeviceTypes(devices.types());
615 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
616 }
617}
618
Jean-Michel Trivi2deb4782019-11-01 11:04:15 -0700619DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
François Gaffiedc7553f2018-11-02 10:39:57 +0100620 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiedc7553f2018-11-02 10:39:57 +0100621
Jean-Michel Trivi2deb4782019-11-01 11:04:15 -0700622 // check if this strategy has a preferred device that is available,
623 // if yes, give priority to it
624 AudioDeviceTypeAddr preferredStrategyDevice;
625 const status_t status = getPreferredDeviceForStrategy(strategy, preferredStrategyDevice);
626 if (status == NO_ERROR) {
627 // there is a preferred device, is it available?
628 sp<DeviceDescriptor> preferredAvailableDevDescr = availableOutputDevices.getDevice(
629 preferredStrategyDevice.mType,
630 String8(preferredStrategyDevice.mAddress.c_str()),
631 AUDIO_FORMAT_DEFAULT);
632 if (preferredAvailableDevDescr != nullptr) {
633 ALOGVV("%s using pref device 0x%08x/%s for strategy %u", __FUNCTION__,
634 preferredStrategyDevice.mType, preferredStrategyDevice.mAddress, strategy);
635 return DeviceVector(preferredAvailableDevDescr);
636 }
637 }
638
639 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
640 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100641 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
Jean-Michel Trivi2deb4782019-11-01 11:04:15 -0700642 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
jiabin147f5c32019-08-07 14:43:33 -0700643 return getDevicesForStrategyInt(legacyStrategy,
644 availableOutputDevices,
645 availableInputDevices, outputs);
François Gaffiedc7553f2018-11-02 10:39:57 +0100646}
647
648DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
649 const sp<DeviceDescriptor> &preferredDevice,
650 bool fromCache) const
651{
652 // First check for explict routing device
653 if (preferredDevice != nullptr) {
654 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
655 return DeviceVector(preferredDevice);
656 }
François Gaffiec005e562018-11-06 15:04:49 +0100657 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
Eric Laurentaf377772019-03-29 14:50:21 -0700658 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100659 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100660 //
661 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
662 // be by APM?
663 //
François Gaffiec005e562018-11-06 15:04:49 +0100664 // Honor explicit routing requests only if all active clients have a preferred route in which
665 // case the last active client route is used
666 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
667 if (device != nullptr) {
668 return DeviceVector(device);
669 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100670
671 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
672}
673
674DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
675{
676 auto attributes = getAttributesForStreamType(stream);
677 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
678}
679
680sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
Mikhail Naganovbfac5832019-03-05 16:55:28 -0800681 sp<AudioPolicyMix> *mix) const
François Gaffiedc7553f2018-11-02 10:39:57 +0100682{
François Gaffiec005e562018-11-06 15:04:49 +0100683 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
Eric Laurentaf377772019-03-29 14:50:21 -0700684 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffiec005e562018-11-06 15:04:49 +0100685 const auto &inputs = getApmObserver()->getInputs();
François Gaffiedc7553f2018-11-02 10:39:57 +0100686 std::string address;
François Gaffiec005e562018-11-06 15:04:49 +0100687
François Gaffiedc7553f2018-11-02 10:39:57 +0100688 //
François Gaffiec005e562018-11-06 15:04:49 +0100689 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
690 // first as it used to be by APM?
François Gaffiedc7553f2018-11-02 10:39:57 +0100691 //
François Gaffiec005e562018-11-06 15:04:49 +0100692 // Honor explicit routing requests only if all active clients have a preferred route in which
693 // case the last active client route is used
694 sp<DeviceDescriptor> device =
695 findPreferredDevice(inputs, attr.source, availableInputDevices);
696 if (device != nullptr) {
697 return device;
698 }
699
700 device = policyMixes.getDeviceAndMixForInputSource(attr.source, availableInputDevices, mix);
701 if (device != nullptr) {
702 return device;
703 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100704
jiabin147f5c32019-08-07 14:43:33 -0700705 device = getDeviceForInputSource(attr.source);
706 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
707 // Return immediately if the device is null or it is not a remote submix device.
708 return device;
François Gaffiedc7553f2018-11-02 10:39:57 +0100709 }
jiabin147f5c32019-08-07 14:43:33 -0700710
711 // For remote submix device, try to find the device by address.
712 address = "0";
713 std::size_t pos;
714 std::string tags { attr.tags };
715 if ((pos = tags.find("addr=")) != std::string::npos) {
716 address = tags.substr(pos + std::strlen("addr="));
717 }
718 return availableInputDevices.getDevice(device->type(),
François Gaffiedc7553f2018-11-02 10:39:57 +0100719 String8(address.c_str()),
720 AUDIO_FORMAT_DEFAULT);
721}
722
François Gaffie2110e042015-03-24 08:41:51 +0100723} // namespace audio_policy
724} // namespace android
725
726