blob: 69f06982e5c5335ab2534898d22bb91f7be713f5 [file] [log] [blame]
François Gaffie2110e042015-03-24 08:41:51 +01001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "APM::AudioPolicyEngine"
18//#define LOG_NDEBUG 0
19
20//#define VERY_VERBOSE_LOGGING
21#ifdef VERY_VERBOSE_LOGGING
22#define ALOGVV ALOGV
23#else
24#define ALOGVV(a...) do { } while(0)
25#endif
26
27#include "Engine.h"
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -070028#include <android-base/macros.h>
François Gaffie2110e042015-03-24 08:41:51 +010029#include <AudioPolicyManagerObserver.h>
30#include <AudioPort.h>
31#include <IOProfile.h>
32#include <policy.h>
33#include <utils/String8.h>
34#include <utils/Log.h>
35
36namespace android
37{
38namespace audio_policy
39{
40
François Gaffiedc7553f2018-11-02 10:39:57 +010041struct legacy_strategy_map { const char *name; legacy_strategy id; };
42static const std::vector<legacy_strategy_map> gLegacyStrategy = {
43 { "STRATEGY_NONE", STRATEGY_NONE },
44 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
45 { "STRATEGY_PHONE", STRATEGY_PHONE },
46 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
47 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
48 { "STRATEGY_DTMF", STRATEGY_DTMF },
49 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
50 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
51 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
52 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
53 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
54};
55
François Gaffie2110e042015-03-24 08:41:51 +010056Engine::Engine()
François Gaffie2110e042015-03-24 08:41:51 +010057{
François Gaffiedc7553f2018-11-02 10:39:57 +010058 auto result = EngineBase::loadAudioPolicyEngineConfig();
59 ALOGE_IF(result.nbSkippedElement != 0,
60 "Policy Engine configuration is partially invalid, skipped %zu elements",
61 result.nbSkippedElement);
62
63 for (const auto &strategy : gLegacyStrategy) {
64 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
François Gaffie2110e042015-03-24 08:41:51 +010065 }
66}
67
François Gaffie2110e042015-03-24 08:41:51 +010068status_t Engine::setPhoneState(audio_mode_t state)
69{
70 ALOGV("setPhoneState() state %d", state);
71
72 if (state < 0 || state >= AUDIO_MODE_CNT) {
73 ALOGW("setPhoneState() invalid state %d", state);
74 return BAD_VALUE;
75 }
76
François Gaffiedc7553f2018-11-02 10:39:57 +010077 if (state == getPhoneState()) {
François Gaffie2110e042015-03-24 08:41:51 +010078 ALOGW("setPhoneState() setting same state %d", state);
79 return BAD_VALUE;
80 }
81
82 // store previous phone state for management of sonification strategy below
François Gaffiedc7553f2018-11-02 10:39:57 +010083 int oldState = getPhoneState();
84 EngineBase::setPhoneState(state);
François Gaffied1ab2bd2015-12-02 18:20:06 +010085
François Gaffie2110e042015-03-24 08:41:51 +010086 if (!is_state_in_call(oldState) && is_state_in_call(state)) {
87 ALOGV(" Entering call in setPhoneState()");
François Gaffiedc7553f2018-11-02 10:39:57 +010088 getApmObserver()->getVolumeCurves().switchVolumeCurve(AUDIO_STREAM_VOICE_CALL,
François Gaffied1ab2bd2015-12-02 18:20:06 +010089 AUDIO_STREAM_DTMF);
François Gaffie2110e042015-03-24 08:41:51 +010090 } else if (is_state_in_call(oldState) && !is_state_in_call(state)) {
91 ALOGV(" Exiting call in setPhoneState()");
François Gaffiedc7553f2018-11-02 10:39:57 +010092 getApmObserver()->getVolumeCurves().restoreOriginVolumeCurve(AUDIO_STREAM_DTMF);
François Gaffie2110e042015-03-24 08:41:51 +010093 }
94 return NO_ERROR;
95}
96
97status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
98{
99 switch(usage) {
100 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
101 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
102 config != AUDIO_POLICY_FORCE_NONE) {
103 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
104 return BAD_VALUE;
105 }
François Gaffie2110e042015-03-24 08:41:51 +0100106 break;
107 case AUDIO_POLICY_FORCE_FOR_MEDIA:
108 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
109 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
110 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
111 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
112 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
113 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
114 return BAD_VALUE;
115 }
François Gaffie2110e042015-03-24 08:41:51 +0100116 break;
117 case AUDIO_POLICY_FORCE_FOR_RECORD:
118 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
119 config != AUDIO_POLICY_FORCE_NONE) {
120 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
121 return BAD_VALUE;
122 }
François Gaffie2110e042015-03-24 08:41:51 +0100123 break;
124 case AUDIO_POLICY_FORCE_FOR_DOCK:
125 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
126 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
127 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
128 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
129 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
130 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
131 }
François Gaffie2110e042015-03-24 08:41:51 +0100132 break;
133 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
134 if (config != AUDIO_POLICY_FORCE_NONE &&
135 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
136 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
137 }
François Gaffie2110e042015-03-24 08:41:51 +0100138 break;
139 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
140 if (config != AUDIO_POLICY_FORCE_NONE &&
141 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800142 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
143 }
Phil Burk09bc4612016-02-24 15:58:15 -0800144 break;
145 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
146 if (config != AUDIO_POLICY_FORCE_NONE &&
147 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
jiabin81772902018-04-02 17:52:27 -0700148 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
149 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
Phil Burk09bc4612016-02-24 15:58:15 -0800150 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
151 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100152 }
François Gaffie2110e042015-03-24 08:41:51 +0100153 break;
Jack He96117ae2018-02-12 20:52:53 -0800154 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
155 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
156 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
157 return BAD_VALUE;
158 }
Jack He96117ae2018-02-12 20:52:53 -0800159 break;
François Gaffie2110e042015-03-24 08:41:51 +0100160 default:
161 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800162 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100163 }
François Gaffiedc7553f2018-11-02 10:39:57 +0100164 return EngineBase::setForceUse(usage, config);
François Gaffie2110e042015-03-24 08:41:51 +0100165}
166
167routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream)
168{
169 // stream to strategy mapping
170 switch (stream) {
171 case AUDIO_STREAM_VOICE_CALL:
172 case AUDIO_STREAM_BLUETOOTH_SCO:
François Gaffiedc7553f2018-11-02 10:39:57 +0100173 return android::STRATEGY_PHONE;
François Gaffie2110e042015-03-24 08:41:51 +0100174 case AUDIO_STREAM_RING:
175 case AUDIO_STREAM_ALARM:
François Gaffiedc7553f2018-11-02 10:39:57 +0100176 return android::STRATEGY_SONIFICATION;
François Gaffie2110e042015-03-24 08:41:51 +0100177 case AUDIO_STREAM_NOTIFICATION:
François Gaffiedc7553f2018-11-02 10:39:57 +0100178 return android::STRATEGY_SONIFICATION_RESPECTFUL;
François Gaffie2110e042015-03-24 08:41:51 +0100179 case AUDIO_STREAM_DTMF:
François Gaffiedc7553f2018-11-02 10:39:57 +0100180 return android::STRATEGY_DTMF;
François Gaffie2110e042015-03-24 08:41:51 +0100181 default:
182 ALOGE("unknown stream type %d", stream);
Andy Hung320fd852018-10-09 14:06:37 -0700183 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100184 case AUDIO_STREAM_SYSTEM:
185 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
186 // while key clicks are played produces a poor result
187 case AUDIO_STREAM_MUSIC:
François Gaffiedc7553f2018-11-02 10:39:57 +0100188 return android::STRATEGY_MEDIA;
François Gaffie2110e042015-03-24 08:41:51 +0100189 case AUDIO_STREAM_ENFORCED_AUDIBLE:
François Gaffiedc7553f2018-11-02 10:39:57 +0100190 return android::STRATEGY_ENFORCED_AUDIBLE;
François Gaffie2110e042015-03-24 08:41:51 +0100191 case AUDIO_STREAM_TTS:
François Gaffiedc7553f2018-11-02 10:39:57 +0100192 return android::STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100193 case AUDIO_STREAM_ACCESSIBILITY:
François Gaffiedc7553f2018-11-02 10:39:57 +0100194 return android::STRATEGY_ACCESSIBILITY;
François Gaffie2110e042015-03-24 08:41:51 +0100195 case AUDIO_STREAM_REROUTING:
François Gaffiedc7553f2018-11-02 10:39:57 +0100196 return android::STRATEGY_REROUTING;
François Gaffie2110e042015-03-24 08:41:51 +0100197 }
198}
199
200routing_strategy Engine::getStrategyForUsage(audio_usage_t usage)
201{
François Gaffie2110e042015-03-24 08:41:51 +0100202 // usage to strategy mapping
203 switch (usage) {
204 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
François Gaffiedc7553f2018-11-02 10:39:57 +0100205 return android::STRATEGY_ACCESSIBILITY;
François Gaffie2110e042015-03-24 08:41:51 +0100206
207 case AUDIO_USAGE_MEDIA:
208 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -0800209 case AUDIO_USAGE_ASSISTANT:
François Gaffie2110e042015-03-24 08:41:51 +0100210 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
211 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
François Gaffiedc7553f2018-11-02 10:39:57 +0100212 return android::STRATEGY_MEDIA;
François Gaffie2110e042015-03-24 08:41:51 +0100213
214 case AUDIO_USAGE_VOICE_COMMUNICATION:
François Gaffiedc7553f2018-11-02 10:39:57 +0100215 return android::STRATEGY_PHONE;
François Gaffie2110e042015-03-24 08:41:51 +0100216
217 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
François Gaffiedc7553f2018-11-02 10:39:57 +0100218 return android::STRATEGY_DTMF;
François Gaffie2110e042015-03-24 08:41:51 +0100219
220 case AUDIO_USAGE_ALARM:
221 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
François Gaffiedc7553f2018-11-02 10:39:57 +0100222 return android::STRATEGY_SONIFICATION;
François Gaffie2110e042015-03-24 08:41:51 +0100223
224 case AUDIO_USAGE_NOTIFICATION:
225 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
226 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
227 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
228 case AUDIO_USAGE_NOTIFICATION_EVENT:
François Gaffiedc7553f2018-11-02 10:39:57 +0100229 return android::STRATEGY_SONIFICATION_RESPECTFUL;
François Gaffie2110e042015-03-24 08:41:51 +0100230
231 case AUDIO_USAGE_UNKNOWN:
232 default:
François Gaffiedc7553f2018-11-02 10:39:57 +0100233 return android::STRATEGY_MEDIA;
François Gaffie2110e042015-03-24 08:41:51 +0100234 }
235}
236
237audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const
238{
François Gaffiedc7553f2018-11-02 10:39:57 +0100239 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
240 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
François Gaffie2110e042015-03-24 08:41:51 +0100241
François Gaffiedc7553f2018-11-02 10:39:57 +0100242 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100243
François Gaffiedc7553f2018-11-02 10:39:57 +0100244 return getDeviceForStrategyInt(static_cast<legacy_strategy>(strategy), availableOutputDevices,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800245 availableInputDevices, outputs, (uint32_t)AUDIO_DEVICE_NONE);
Eric Laurent28d09f02016-03-08 10:43:05 -0800246}
247
François Gaffiedc7553f2018-11-02 10:39:57 +0100248audio_devices_t Engine::getDeviceForStrategyInt(legacy_strategy strategy,
249 DeviceVector availableOutputDevices,
250 DeviceVector availableInputDevices,
251 const SwAudioOutputCollection &outputs,
252 uint32_t outputDeviceTypesToIgnore) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800253{
François Gaffie2110e042015-03-24 08:41:51 +0100254 uint32_t device = AUDIO_DEVICE_NONE;
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800255 uint32_t availableOutputDevicesType =
256 availableOutputDevices.types() & ~outputDeviceTypesToIgnore;
François Gaffie2110e042015-03-24 08:41:51 +0100257
258 switch (strategy) {
259
260 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
261 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100262 break;
263
264 case STRATEGY_SONIFICATION_RESPECTFUL:
Eric Laurent7731b5a2018-04-06 15:47:22 -0700265 if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800266 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800267 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs,
268 outputDeviceTypesToIgnore);
François Gaffie2110e042015-03-24 08:41:51 +0100269 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800270 bool media_active_locally =
271 outputs.isStreamActiveLocally(
272 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
273 || outputs.isStreamActiveLocally(
274 AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
275 // routing is same as media without the "remote" device
276 device = getDeviceForStrategyInt(STRATEGY_MEDIA,
277 availableOutputDevices,
278 availableInputDevices, outputs,
279 AUDIO_DEVICE_OUT_REMOTE_SUBMIX | outputDeviceTypesToIgnore);
280 // if no media is playing on the device, check for mandatory use of "safe" speaker
281 // when media would have played on speaker, and the safe speaker path is available
282 if (!media_active_locally
283 && (device & AUDIO_DEVICE_OUT_SPEAKER)
284 && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Eric Laurent9a7d9222015-07-02 15:30:23 -0700285 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
286 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
287 }
François Gaffie2110e042015-03-24 08:41:51 +0100288 }
289 break;
290
291 case STRATEGY_DTMF:
292 if (!isInCall()) {
293 // when off call, DTMF strategy follows the same rules as MEDIA strategy
Eric Laurent28d09f02016-03-08 10:43:05 -0800294 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800295 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs,
296 outputDeviceTypesToIgnore);
François Gaffie2110e042015-03-24 08:41:51 +0100297 break;
298 }
299 // when in call, DTMF and PHONE strategies follow the same rules
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700300 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100301
302 case STRATEGY_PHONE:
303 // Force use of only devices on primary output if:
304 // - in call AND
305 // - cannot route from voice call RX OR
306 // - audio HAL version is < 3.0 and TX device is on the primary HW module
307 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
308 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
309 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
310 audio_devices_t availPrimaryInputDevices =
Mikhail Naganov93661932018-07-26 14:37:41 -0700311 availableInputDevices.getDeviceTypesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800312
313 // TODO: getPrimaryOutput return only devices from first module in
314 // audio_policy_configuration.xml, hearing aid is not there, but it's
315 // a primary device
316 // FIXME: this is not the right way of solving this problem
François Gaffie2110e042015-03-24 08:41:51 +0100317 audio_devices_t availPrimaryOutputDevices =
François Gaffie11d30102018-11-02 16:09:09 +0100318 (primaryOutput->supportedDevices().types() | AUDIO_DEVICE_OUT_HEARING_AID) &
Eric Laurent58a73fc2018-02-21 18:46:13 -0800319 availableOutputDevices.types();
François Gaffie2110e042015-03-24 08:41:51 +0100320
321 if (((availableInputDevices.types() &
322 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
323 (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700324 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
François Gaffie2110e042015-03-24 08:41:51 +0100325 availableOutputDevicesType = availPrimaryOutputDevices;
326 }
327 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800328 // for phone strategy, we first consider the forced use and then the available devices by
329 // order of priority
François Gaffiedc7553f2018-11-02 10:39:57 +0100330 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100331 case AUDIO_POLICY_FORCE_BT_SCO:
332 if (!isInCall() || strategy != STRATEGY_DTMF) {
333 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
334 if (device) break;
335 }
336 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
337 if (device) break;
338 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
339 if (device) break;
340 // if SCO device is requested but no SCO device is available, fall back to default case
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700341 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100342
343 default: // FORCE_NONE
Eric Laurent58a73fc2018-02-21 18:46:13 -0800344 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID;
345 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100346 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
347 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100348 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800349 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100350 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
351 if (device) break;
352 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
353 if (device) break;
354 }
355 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
356 if (device) break;
357 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
358 if (device) break;
Eric Laurenta0b18ce2016-03-08 11:05:00 -0800359 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
360 if (device) break;
Eric Laurent904d6322017-03-17 17:20:47 -0700361 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
362 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100363 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
364 if (device) break;
365 if (!isInCall()) {
366 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
367 if (device) break;
368 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
369 if (device) break;
370 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
371 if (device) break;
372 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
373 if (device) break;
374 }
375 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
François Gaffie2110e042015-03-24 08:41:51 +0100376 break;
377
378 case AUDIO_POLICY_FORCE_SPEAKER:
379 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
380 // A2DP speaker when forcing to speaker output
381 if (!isInCall() &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100382 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800383 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100384 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
385 if (device) break;
386 }
387 if (!isInCall()) {
388 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
389 if (device) break;
390 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
391 if (device) break;
392 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
393 if (device) break;
394 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
395 if (device) break;
396 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
397 if (device) break;
398 }
François Gaffie2110e042015-03-24 08:41:51 +0100399 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100400 break;
401 }
402 break;
403
404 case STRATEGY_SONIFICATION:
405
Eric Laurentdcd4ab12018-06-29 17:45:13 -0700406 // If incall, just select the STRATEGY_PHONE device
Eric Laurent7731b5a2018-04-06 15:47:22 -0700407 if (isInCall() || outputs.isStreamActiveLocally(AUDIO_STREAM_VOICE_CALL)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800408 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800409 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
410 outputDeviceTypesToIgnore);
François Gaffie2110e042015-03-24 08:41:51 +0100411 break;
412 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700413 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100414
415 case STRATEGY_ENFORCED_AUDIBLE:
416 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
417 // except:
418 // - when in call where it doesn't default to STRATEGY_PHONE behavior
419 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
420
421 if ((strategy == STRATEGY_SONIFICATION) ||
François Gaffiedc7553f2018-11-02 10:39:57 +0100422 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
François Gaffie2110e042015-03-24 08:41:51 +0100423 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100424 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800425
426 // if SCO headset is connected and we are told to use it, play ringtone over
427 // speaker and BT SCO
Jack He96117ae2018-02-12 20:52:53 -0800428 if ((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) {
Eric Laurenta8e0f022017-01-27 17:41:53 -0800429 uint32_t device2 = AUDIO_DEVICE_NONE;
430 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
431 if (device2 == AUDIO_DEVICE_NONE) {
432 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
433 }
434 if (device2 == AUDIO_DEVICE_NONE) {
435 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
436 }
Jack He96117ae2018-02-12 20:52:53 -0800437 // Use ONLY Bluetooth SCO output when ringing in vibration mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100438 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
Jack He96117ae2018-02-12 20:52:53 -0800439 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
François Gaffiedc7553f2018-11-02 10:39:57 +0100440 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
Jack He96117ae2018-02-12 20:52:53 -0800441 == AUDIO_POLICY_FORCE_BT_SCO) {
442 if (device2 != AUDIO_DEVICE_NONE) {
443 device = device2;
444 break;
445 }
446 }
447 }
448 // Use both Bluetooth SCO and phone default output when ringing in normal mode
François Gaffiedc7553f2018-11-02 10:39:57 +0100449 if (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION) == AUDIO_POLICY_FORCE_BT_SCO) {
juyuchen5fa0aed2018-05-16 10:58:37 +0800450 if ((strategy == STRATEGY_SONIFICATION) &&
451 (device & AUDIO_DEVICE_OUT_SPEAKER) &&
452 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
453 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
454 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
455 }
Jack He96117ae2018-02-12 20:52:53 -0800456 if (device2 != AUDIO_DEVICE_NONE) {
457 device |= device2;
458 break;
459 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800460 }
461 }
François Gaffie2110e042015-03-24 08:41:51 +0100462 // The second device used for sonification is the same as the device used by media strategy
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700463 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100464
François Gaffie2110e042015-03-24 08:41:51 +0100465 case STRATEGY_ACCESSIBILITY:
466 if (strategy == STRATEGY_ACCESSIBILITY) {
467 // do not route accessibility prompts to a digital output currently configured with a
468 // compressed format as they would likely not be mixed and dropped.
469 for (size_t i = 0; i < outputs.size(); i++) {
470 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
François Gaffie11d30102018-11-02 16:09:09 +0100471 audio_devices_t devices = desc->devices().types() &
François Gaffie2110e042015-03-24 08:41:51 +0100472 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
473 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
474 devices != AUDIO_DEVICE_NONE) {
475 availableOutputDevicesType = availableOutputDevices.types() & ~devices;
476 }
477 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800478 availableOutputDevices =
Mikhail Naganov708e0382018-05-30 09:53:04 -0700479 availableOutputDevices.getDevicesFromTypeMask(availableOutputDevicesType);
Eric Laurent28d09f02016-03-08 10:43:05 -0800480 if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
481 outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
482 return getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800483 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs,
484 outputDeviceTypesToIgnore);
Eric Laurent28d09f02016-03-08 10:43:05 -0800485 }
486 if (isInCall()) {
487 return getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800488 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
489 outputDeviceTypesToIgnore);
Eric Laurent28d09f02016-03-08 10:43:05 -0800490 }
François Gaffie2110e042015-03-24 08:41:51 +0100491 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800492 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700493 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100494
Eric Laurent28d09f02016-03-08 10:43:05 -0800495 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100496 case STRATEGY_REROUTING:
497 case STRATEGY_MEDIA: {
498 uint32_t device2 = AUDIO_DEVICE_NONE;
499 if (strategy != STRATEGY_SONIFICATION) {
500 // no sonification on remote submix (e.g. WFD)
Eric Laurent28d09f02016-03-08 10:43:05 -0800501 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800502 String8("0"), AUDIO_FORMAT_DEFAULT) != 0) {
François Gaffie2110e042015-03-24 08:41:51 +0100503 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
504 }
505 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700506 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800507 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800508 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
509 outputDeviceTypesToIgnore);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700510 break;
511 }
Eric Laurent58a73fc2018-02-21 18:46:13 -0800512 if (device2 == AUDIO_DEVICE_NONE) {
513 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID;
514 }
François Gaffie2110e042015-03-24 08:41:51 +0100515 if ((device2 == AUDIO_DEVICE_NONE) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100516 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lataa8ee9962018-01-31 20:24:23 -0800517 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100518 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
519 if (device2 == AUDIO_DEVICE_NONE) {
520 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
521 }
522 if (device2 == AUDIO_DEVICE_NONE) {
523 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
524 }
525 }
526 if ((device2 == AUDIO_DEVICE_NONE) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100527 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
François Gaffie2110e042015-03-24 08:41:51 +0100528 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
529 }
530 if (device2 == AUDIO_DEVICE_NONE) {
531 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
532 }
Jean-Michel Trivi5c233f82015-04-03 09:21:24 -0700533 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100534 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
535 }
536 if (device2 == AUDIO_DEVICE_NONE) {
537 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
538 }
539 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent904d6322017-03-17 17:20:47 -0700540 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
541 }
542 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100543 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
544 }
545 if (device2 == AUDIO_DEVICE_NONE) {
546 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
547 }
548 if (device2 == AUDIO_DEVICE_NONE) {
549 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
550 }
551 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
552 // no sonification on aux digital (e.g. HDMI)
553 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
554 }
555 if ((device2 == AUDIO_DEVICE_NONE) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100556 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
François Gaffie2110e042015-03-24 08:41:51 +0100557 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
558 }
559 if (device2 == AUDIO_DEVICE_NONE) {
560 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
561 }
562 int device3 = AUDIO_DEVICE_NONE;
563 if (strategy == STRATEGY_MEDIA) {
564 // ARC, SPDIF and AUX_LINE can co-exist with others.
565 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
566 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
567 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
568 }
569
570 device2 |= device3;
571 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
572 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
573 device |= device2;
574
575 // If hdmi system audio mode is on, remove speaker out of output list.
576 if ((strategy == STRATEGY_MEDIA) &&
François Gaffiedc7553f2018-11-02 10:39:57 +0100577 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
François Gaffie2110e042015-03-24 08:41:51 +0100578 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
579 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
580 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700581
582 // for STRATEGY_SONIFICATION:
583 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
584 if ((strategy == STRATEGY_SONIFICATION) &&
585 (device & AUDIO_DEVICE_OUT_SPEAKER) &&
586 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
587 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
588 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
589 }
François Gaffie2110e042015-03-24 08:41:51 +0100590 } break;
591
592 default:
593 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
594 break;
595 }
596
Eric Laurent5a2b6292016-04-14 18:05:57 -0700597 if (device == AUDIO_DEVICE_NONE) {
598 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
François Gaffiedc7553f2018-11-02 10:39:57 +0100599 device = getApmObserver()->getDefaultOutputDevice()->type();
Eric Laurent5a2b6292016-04-14 18:05:57 -0700600 ALOGE_IF(device == AUDIO_DEVICE_NONE,
601 "getDeviceForStrategy() no default device defined");
602 }
François Gaffie2110e042015-03-24 08:41:51 +0100603 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
604 return device;
605}
606
607
608audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
609{
François Gaffiedc7553f2018-11-02 10:39:57 +0100610 const DeviceVector &availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
611 const DeviceVector &availableInputDevices = getApmObserver()->getAvailableInputDevices();
612 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100613 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
614
615 uint32_t device = AUDIO_DEVICE_NONE;
616
Eric Laurentdc95a252018-04-12 12:46:56 -0700617 // when a call is active, force device selection to match source VOICE_COMMUNICATION
618 // for most other input sources to avoid rerouting call TX audio
619 if (isInCall()) {
620 switch (inputSource) {
621 case AUDIO_SOURCE_DEFAULT:
622 case AUDIO_SOURCE_MIC:
623 case AUDIO_SOURCE_VOICE_RECOGNITION:
624 case AUDIO_SOURCE_UNPROCESSED:
625 case AUDIO_SOURCE_HOTWORD:
626 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800627 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Eric Laurentdc95a252018-04-12 12:46:56 -0700628 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
629 break;
630 default:
631 break;
632 }
633 }
634
François Gaffie2110e042015-03-24 08:41:51 +0100635 switch (inputSource) {
636 case AUDIO_SOURCE_VOICE_UPLINK:
637 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
638 device = AUDIO_DEVICE_IN_VOICE_CALL;
639 break;
640 }
641 break;
642
643 case AUDIO_SOURCE_DEFAULT:
644 case AUDIO_SOURCE_MIC:
645 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
646 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
François Gaffiedc7553f2018-11-02 10:39:57 +0100647 } else if ((getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO) &&
François Gaffie2110e042015-03-24 08:41:51 +0100648 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
649 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
650 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
651 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700652 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
653 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100654 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
655 device = AUDIO_DEVICE_IN_USB_DEVICE;
656 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
657 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
658 }
659 break;
660
661 case AUDIO_SOURCE_VOICE_COMMUNICATION:
662 // Allow only use of devices on primary input if in call and HAL does not support routing
663 // to voice call path.
664 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
665 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
666 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
Mikhail Naganov93661932018-07-26 14:37:41 -0700667 availableDeviceTypes = availableInputDevices.getDeviceTypesFromHwModule(
668 primaryOutput->getModuleHandle()) & ~AUDIO_DEVICE_BIT_IN;
François Gaffie2110e042015-03-24 08:41:51 +0100669 }
670
François Gaffiedc7553f2018-11-02 10:39:57 +0100671 switch (getForceUse(AUDIO_POLICY_FORCE_FOR_COMMUNICATION)) {
François Gaffie2110e042015-03-24 08:41:51 +0100672 case AUDIO_POLICY_FORCE_BT_SCO:
673 // if SCO device is requested but no SCO device is available, fall back to default case
674 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
675 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
676 break;
677 }
Chih-Hung Hsieh2b487032018-09-13 14:16:02 -0700678 FALLTHROUGH_INTENDED;
François Gaffie2110e042015-03-24 08:41:51 +0100679
680 default: // FORCE_NONE
681 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
682 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700683 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
684 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100685 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
686 device = AUDIO_DEVICE_IN_USB_DEVICE;
687 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
688 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
689 }
690 break;
691
692 case AUDIO_POLICY_FORCE_SPEAKER:
693 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
694 device = AUDIO_DEVICE_IN_BACK_MIC;
695 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
696 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
697 }
698 break;
699 }
700 break;
701
702 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800703 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100704 case AUDIO_SOURCE_HOTWORD:
François Gaffiedc7553f2018-11-02 10:39:57 +0100705 if (getForceUse(AUDIO_POLICY_FORCE_FOR_RECORD) == AUDIO_POLICY_FORCE_BT_SCO &&
François Gaffie2110e042015-03-24 08:41:51 +0100706 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
707 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
708 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
709 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700710 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
711 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100712 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
713 device = AUDIO_DEVICE_IN_USB_DEVICE;
714 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
715 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
716 }
717 break;
718 case AUDIO_SOURCE_CAMCORDER:
719 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
720 device = AUDIO_DEVICE_IN_BACK_MIC;
721 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
722 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
Takeshi Oishi86ad3d12018-07-20 16:13:06 +0900723 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
724 // This is specifically for a device without built-in mic
725 device = AUDIO_DEVICE_IN_USB_DEVICE;
François Gaffie2110e042015-03-24 08:41:51 +0100726 }
727 break;
728 case AUDIO_SOURCE_VOICE_DOWNLINK:
729 case AUDIO_SOURCE_VOICE_CALL:
730 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
731 device = AUDIO_DEVICE_IN_VOICE_CALL;
732 }
733 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800734 case AUDIO_SOURCE_VOICE_PERFORMANCE:
735 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
736 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
737 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
738 device = AUDIO_DEVICE_IN_USB_HEADSET;
739 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
740 device = AUDIO_DEVICE_IN_USB_DEVICE;
741 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
742 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
743 }
744 break;
François Gaffie2110e042015-03-24 08:41:51 +0100745 case AUDIO_SOURCE_REMOTE_SUBMIX:
746 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
747 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
748 }
749 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800750 case AUDIO_SOURCE_FM_TUNER:
François Gaffie2110e042015-03-24 08:41:51 +0100751 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
752 device = AUDIO_DEVICE_IN_FM_TUNER;
753 }
754 break;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800755 case AUDIO_SOURCE_ECHO_REFERENCE:
756 if (availableDeviceTypes & AUDIO_DEVICE_IN_ECHO_REFERENCE) {
757 device = AUDIO_DEVICE_IN_ECHO_REFERENCE;
758 }
759 break;
François Gaffie2110e042015-03-24 08:41:51 +0100760 default:
761 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
762 break;
763 }
Eric Laurent5a2b6292016-04-14 18:05:57 -0700764 if (device == AUDIO_DEVICE_NONE) {
765 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
766 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
767 device = AUDIO_DEVICE_IN_STUB;
768 }
769 ALOGE_IF(device == AUDIO_DEVICE_NONE,
770 "getDeviceForInputSource() no default device defined");
771 }
François Gaffie2110e042015-03-24 08:41:51 +0100772 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
773 return device;
774}
775
François Gaffiedc7553f2018-11-02 10:39:57 +0100776void Engine::updateDeviceSelectionCache()
777{
778 for (const auto &iter : getProductStrategies()) {
779 const auto &strategy = iter.second;
780 auto devices = getDevicesForProductStrategy(strategy->getId());
781 mDevicesForStrategies[strategy->getId()] = devices;
782 strategy->setDeviceTypes(devices.types());
783 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
784 }
785}
786
787DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const
788{
789 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
790 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
791 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
792
793 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
794 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
795 audio_devices_t devices = getDeviceForStrategyInt(legacyStrategy,
796 availableOutputDevices,
797 availableInputDevices, outputs,
798 (uint32_t)AUDIO_DEVICE_NONE);
799 return availableOutputDevices.getDevicesFromTypeMask(devices);
800}
801
802DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
803 const sp<DeviceDescriptor> &preferredDevice,
804 bool fromCache) const
805{
806 // First check for explict routing device
807 if (preferredDevice != nullptr) {
808 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
809 return DeviceVector(preferredDevice);
810 }
811 //
812 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
813 // be by APM?
814 //
815 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
816 //
817 // @TODO: manage dynamic mix
818 //
819
820 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
821}
822
823DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
824{
825 auto attributes = getAttributesForStreamType(stream);
826 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
827}
828
829sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
830 AudioMix **/*mix*/) const
831{
832 const auto &availableInputDevices = getApmObserver()->getAvailableInputDevices();
833 std::string address;
834 //
835 // @TODO: manage explicit routing and dynamic mix
836 //
837 audio_devices_t deviceType = getDeviceForInputSource(attr.source);
838
839 if (audio_is_remote_submix_device(deviceType)) {
840 address = "0";
841 std::size_t pos;
842 std::string tags { attr.tags };
843 if ((pos = tags.find("addr=")) != std::string::npos) {
844 address = tags.substr(pos + std::strlen("addr="));
845 }
846 }
847 return availableInputDevices.getDevice(deviceType,
848 String8(address.c_str()),
849 AUDIO_FORMAT_DEFAULT);
850}
851
François Gaffie2110e042015-03-24 08:41:51 +0100852template <>
853AudioPolicyManagerInterface *Engine::queryInterface()
854{
François Gaffiedc7553f2018-11-02 10:39:57 +0100855 return this;
François Gaffie2110e042015-03-24 08:41:51 +0100856}
857
858} // namespace audio_policy
859} // namespace android
860
861