blob: 43205a22a7c61cefec84ae6a606b698526f716f0 [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"
François Gaffie2110e042015-03-24 08:41:51 +010028#include <AudioPolicyManagerObserver.h>
29#include <AudioPort.h>
30#include <IOProfile.h>
31#include <policy.h>
32#include <utils/String8.h>
33#include <utils/Log.h>
34
35namespace android
36{
37namespace audio_policy
38{
39
40Engine::Engine()
41 : mManagerInterface(this),
42 mPhoneState(AUDIO_MODE_NORMAL),
43 mApmObserver(NULL)
44{
45 for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
46 mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
47 }
48}
49
50Engine::~Engine()
51{
52}
53
54void Engine::setObserver(AudioPolicyManagerObserver *observer)
55{
56 ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer");
57 mApmObserver = observer;
58}
59
60status_t Engine::initCheck()
61{
62 return (mApmObserver != NULL) ? NO_ERROR : NO_INIT;
63}
64
François Gaffie2110e042015-03-24 08:41:51 +010065status_t Engine::setPhoneState(audio_mode_t state)
66{
67 ALOGV("setPhoneState() state %d", state);
68
69 if (state < 0 || state >= AUDIO_MODE_CNT) {
70 ALOGW("setPhoneState() invalid state %d", state);
71 return BAD_VALUE;
72 }
73
74 if (state == mPhoneState ) {
75 ALOGW("setPhoneState() setting same state %d", state);
76 return BAD_VALUE;
77 }
78
79 // store previous phone state for management of sonification strategy below
80 int oldState = mPhoneState;
81 mPhoneState = state;
François Gaffied1ab2bd2015-12-02 18:20:06 +010082
François Gaffie2110e042015-03-24 08:41:51 +010083 if (!is_state_in_call(oldState) && is_state_in_call(state)) {
84 ALOGV(" Entering call in setPhoneState()");
François Gaffied1ab2bd2015-12-02 18:20:06 +010085 mApmObserver->getVolumeCurves().switchVolumeCurve(AUDIO_STREAM_VOICE_CALL,
86 AUDIO_STREAM_DTMF);
François Gaffie2110e042015-03-24 08:41:51 +010087 } else if (is_state_in_call(oldState) && !is_state_in_call(state)) {
88 ALOGV(" Exiting call in setPhoneState()");
François Gaffied1ab2bd2015-12-02 18:20:06 +010089 mApmObserver->getVolumeCurves().restoreOriginVolumeCurve(AUDIO_STREAM_DTMF);
François Gaffie2110e042015-03-24 08:41:51 +010090 }
91 return NO_ERROR;
92}
93
94status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
95{
96 switch(usage) {
97 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
98 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
99 config != AUDIO_POLICY_FORCE_NONE) {
100 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
101 return BAD_VALUE;
102 }
103 mForceUse[usage] = config;
104 break;
105 case AUDIO_POLICY_FORCE_FOR_MEDIA:
106 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
107 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
108 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
109 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
110 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
111 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
112 return BAD_VALUE;
113 }
114 mForceUse[usage] = config;
115 break;
116 case AUDIO_POLICY_FORCE_FOR_RECORD:
117 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
118 config != AUDIO_POLICY_FORCE_NONE) {
119 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
120 return BAD_VALUE;
121 }
122 mForceUse[usage] = config;
123 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 }
132 mForceUse[usage] = config;
133 break;
134 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
135 if (config != AUDIO_POLICY_FORCE_NONE &&
136 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
137 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
138 }
139 mForceUse[usage] = config;
140 break;
141 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
142 if (config != AUDIO_POLICY_FORCE_NONE &&
143 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
Phil Burk09bc4612016-02-24 15:58:15 -0800144 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
145 }
146 mForceUse[usage] = config;
147 break;
148 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
149 if (config != AUDIO_POLICY_FORCE_NONE &&
150 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
151 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
152 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
153 return BAD_VALUE;
François Gaffie2110e042015-03-24 08:41:51 +0100154 }
155 mForceUse[usage] = config;
156 break;
157 default:
158 ALOGW("setForceUse() invalid usage %d", usage);
Phil Burk09bc4612016-02-24 15:58:15 -0800159 break; // TODO return BAD_VALUE?
François Gaffie2110e042015-03-24 08:41:51 +0100160 }
161 return NO_ERROR;
162}
163
164routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream)
165{
166 // stream to strategy mapping
167 switch (stream) {
168 case AUDIO_STREAM_VOICE_CALL:
169 case AUDIO_STREAM_BLUETOOTH_SCO:
170 return STRATEGY_PHONE;
171 case AUDIO_STREAM_RING:
172 case AUDIO_STREAM_ALARM:
173 return STRATEGY_SONIFICATION;
174 case AUDIO_STREAM_NOTIFICATION:
175 return STRATEGY_SONIFICATION_RESPECTFUL;
176 case AUDIO_STREAM_DTMF:
177 return STRATEGY_DTMF;
178 default:
179 ALOGE("unknown stream type %d", stream);
180 case AUDIO_STREAM_SYSTEM:
181 // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
182 // while key clicks are played produces a poor result
183 case AUDIO_STREAM_MUSIC:
184 return STRATEGY_MEDIA;
185 case AUDIO_STREAM_ENFORCED_AUDIBLE:
186 return STRATEGY_ENFORCED_AUDIBLE;
187 case AUDIO_STREAM_TTS:
188 return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
189 case AUDIO_STREAM_ACCESSIBILITY:
190 return STRATEGY_ACCESSIBILITY;
191 case AUDIO_STREAM_REROUTING:
192 return STRATEGY_REROUTING;
193 }
194}
195
196routing_strategy Engine::getStrategyForUsage(audio_usage_t usage)
197{
François Gaffie2110e042015-03-24 08:41:51 +0100198 // usage to strategy mapping
199 switch (usage) {
200 case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
François Gaffie2110e042015-03-24 08:41:51 +0100201 return STRATEGY_ACCESSIBILITY;
202
203 case AUDIO_USAGE_MEDIA:
204 case AUDIO_USAGE_GAME:
Jean-Michel Trivi36867762016-12-29 12:03:28 -0800205 case AUDIO_USAGE_ASSISTANT:
François Gaffie2110e042015-03-24 08:41:51 +0100206 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
207 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
208 return STRATEGY_MEDIA;
209
210 case AUDIO_USAGE_VOICE_COMMUNICATION:
211 return STRATEGY_PHONE;
212
213 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
214 return STRATEGY_DTMF;
215
216 case AUDIO_USAGE_ALARM:
217 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
218 return STRATEGY_SONIFICATION;
219
220 case AUDIO_USAGE_NOTIFICATION:
221 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
222 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
223 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
224 case AUDIO_USAGE_NOTIFICATION_EVENT:
225 return STRATEGY_SONIFICATION_RESPECTFUL;
226
227 case AUDIO_USAGE_UNKNOWN:
228 default:
229 return STRATEGY_MEDIA;
230 }
231}
232
233audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const
234{
Eric Laurent0f928fa2016-03-21 12:06:20 -0700235 DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices();
236 DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices();
François Gaffie2110e042015-03-24 08:41:51 +0100237
Eric Laurentc75307b2015-03-17 15:29:32 -0700238 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100239
Eric Laurent0f928fa2016-03-21 12:06:20 -0700240 return getDeviceForStrategyInt(strategy, availableOutputDevices,
Eric Laurent28d09f02016-03-08 10:43:05 -0800241 availableInputDevices, outputs);
242}
243
244
245
246audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy,
Eric Laurent0f928fa2016-03-21 12:06:20 -0700247 DeviceVector availableOutputDevices,
248 DeviceVector availableInputDevices,
Eric Laurent28d09f02016-03-08 10:43:05 -0800249 const SwAudioOutputCollection &outputs) const
250{
François Gaffie2110e042015-03-24 08:41:51 +0100251 uint32_t device = AUDIO_DEVICE_NONE;
252 uint32_t availableOutputDevicesType = availableOutputDevices.types();
253
254 switch (strategy) {
255
256 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
257 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100258 break;
259
260 case STRATEGY_SONIFICATION_RESPECTFUL:
261 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800262 device = getDeviceForStrategyInt(
263 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100264 } else if (outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
265 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
266 // while media is playing on a remote device, use the the sonification behavior.
267 // Note that we test this usecase before testing if media is playing because
268 // the isStreamActive() method only informs about the activity of a stream, not
269 // if it's for local playback. Note also that we use the same delay between both tests
Eric Laurent28d09f02016-03-08 10:43:05 -0800270 device = getDeviceForStrategyInt(
271 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100272 //user "safe" speaker if available instead of normal speaker to avoid triggering
273 //other acoustic safety mechanisms for notification
Eric Laurent9a7d9222015-07-02 15:30:23 -0700274 if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
275 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
276 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
277 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
278 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800279 } else if (outputs.isStreamActive(
Jean-Michel Trivi94c26c02017-06-30 14:50:01 -0700280 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
281 || outputs.isStreamActive(
282 AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY))
283 {
284 // while media/a11y is playing (or has recently played), use the same device
Eric Laurent28d09f02016-03-08 10:43:05 -0800285 device = getDeviceForStrategyInt(
286 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100287 } else {
288 // when media is not playing anymore, fall back on the sonification behavior
Eric Laurent28d09f02016-03-08 10:43:05 -0800289 device = getDeviceForStrategyInt(
290 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100291 //user "safe" speaker if available instead of normal speaker to avoid triggering
292 //other acoustic safety mechanisms for notification
Eric Laurent9a7d9222015-07-02 15:30:23 -0700293 if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
294 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
295 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
296 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
297 }
François Gaffie2110e042015-03-24 08:41:51 +0100298 }
299 break;
300
301 case STRATEGY_DTMF:
302 if (!isInCall()) {
303 // when off call, DTMF strategy follows the same rules as MEDIA strategy
Eric Laurent28d09f02016-03-08 10:43:05 -0800304 device = getDeviceForStrategyInt(
305 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100306 break;
307 }
308 // when in call, DTMF and PHONE strategies follow the same rules
309 // FALL THROUGH
310
311 case STRATEGY_PHONE:
312 // Force use of only devices on primary output if:
313 // - in call AND
314 // - cannot route from voice call RX OR
315 // - audio HAL version is < 3.0 and TX device is on the primary HW module
316 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
317 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
318 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
319 audio_devices_t availPrimaryInputDevices =
320 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
321 audio_devices_t availPrimaryOutputDevices =
322 primaryOutput->supportedDevices() & availableOutputDevices.types();
323
324 if (((availableInputDevices.types() &
325 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
326 (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700327 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
François Gaffie2110e042015-03-24 08:41:51 +0100328 availableOutputDevicesType = availPrimaryOutputDevices;
329 }
330 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800331 // for phone strategy, we first consider the forced use and then the available devices by
332 // order of priority
François Gaffie2110e042015-03-24 08:41:51 +0100333 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
334 case AUDIO_POLICY_FORCE_BT_SCO:
335 if (!isInCall() || strategy != STRATEGY_DTMF) {
336 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
337 if (device) break;
338 }
339 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
340 if (device) break;
341 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
342 if (device) break;
343 // if SCO device is requested but no SCO device is available, fall back to default case
344 // FALL THROUGH
345
346 default: // FORCE_NONE
347 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
348 if (!isInCall() &&
349 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lata32371762018-01-31 20:24:23 -0800350 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100351 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
352 if (device) break;
353 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
354 if (device) break;
355 }
356 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
357 if (device) break;
358 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
359 if (device) break;
Eric Laurenta0b18ce2016-03-08 11:05:00 -0800360 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
361 if (device) break;
Eric Laurent904d6322017-03-17 17:20:47 -0700362 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
363 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100364 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
365 if (device) break;
366 if (!isInCall()) {
367 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
368 if (device) break;
369 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
370 if (device) break;
371 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
372 if (device) break;
373 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
374 if (device) break;
375 }
376 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
François Gaffie2110e042015-03-24 08:41:51 +0100377 break;
378
379 case AUDIO_POLICY_FORCE_SPEAKER:
380 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
381 // A2DP speaker when forcing to speaker output
382 if (!isInCall() &&
383 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lata32371762018-01-31 20:24:23 -0800384 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100385 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
386 if (device) break;
387 }
388 if (!isInCall()) {
389 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
390 if (device) break;
391 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
392 if (device) break;
393 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
394 if (device) break;
395 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
396 if (device) break;
397 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
398 if (device) break;
399 }
François Gaffie2110e042015-03-24 08:41:51 +0100400 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100401 break;
402 }
403 break;
404
405 case STRATEGY_SONIFICATION:
406
407 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
408 // handleIncallSonification().
409 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800410 device = getDeviceForStrategyInt(
411 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100412 break;
413 }
414 // FALL THROUGH
415
416 case STRATEGY_ENFORCED_AUDIBLE:
417 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
418 // except:
419 // - when in call where it doesn't default to STRATEGY_PHONE behavior
420 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
421
422 if ((strategy == STRATEGY_SONIFICATION) ||
423 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
424 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100425 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800426
427 // if SCO headset is connected and we are told to use it, play ringtone over
428 // speaker and BT SCO
429 if (((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) &&
430 (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO)) {
431 uint32_t device2 = AUDIO_DEVICE_NONE;
432 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
433 if (device2 == AUDIO_DEVICE_NONE) {
434 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
435 }
436 if (device2 == AUDIO_DEVICE_NONE) {
437 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
438 }
439
440 if (device2 != AUDIO_DEVICE_NONE) {
441 device |= device2;
442 break;
443 }
444 }
François Gaffie2110e042015-03-24 08:41:51 +0100445 // The second device used for sonification is the same as the device used by media strategy
446 // FALL THROUGH
447
François Gaffie2110e042015-03-24 08:41:51 +0100448 case STRATEGY_ACCESSIBILITY:
449 if (strategy == STRATEGY_ACCESSIBILITY) {
450 // do not route accessibility prompts to a digital output currently configured with a
451 // compressed format as they would likely not be mixed and dropped.
452 for (size_t i = 0; i < outputs.size(); i++) {
453 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
454 audio_devices_t devices = desc->device() &
455 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
456 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
457 devices != AUDIO_DEVICE_NONE) {
458 availableOutputDevicesType = availableOutputDevices.types() & ~devices;
459 }
460 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800461 availableOutputDevices =
462 availableOutputDevices.getDevicesFromType(availableOutputDevicesType);
463 if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
464 outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
465 return getDeviceForStrategyInt(
466 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
467 }
468 if (isInCall()) {
469 return getDeviceForStrategyInt(
470 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
471 }
François Gaffie2110e042015-03-24 08:41:51 +0100472 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800473 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
François Gaffie2110e042015-03-24 08:41:51 +0100474 // FALL THROUGH
475
Eric Laurent28d09f02016-03-08 10:43:05 -0800476 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100477 case STRATEGY_REROUTING:
478 case STRATEGY_MEDIA: {
479 uint32_t device2 = AUDIO_DEVICE_NONE;
480 if (strategy != STRATEGY_SONIFICATION) {
481 // no sonification on remote submix (e.g. WFD)
Eric Laurent28d09f02016-03-08 10:43:05 -0800482 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
483 String8("0")) != 0) {
François Gaffie2110e042015-03-24 08:41:51 +0100484 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
485 }
486 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700487 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800488 device = getDeviceForStrategyInt(
489 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700490 break;
491 }
François Gaffie2110e042015-03-24 08:41:51 +0100492 if ((device2 == AUDIO_DEVICE_NONE) &&
493 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
Aniket Kumar Lata32371762018-01-31 20:24:23 -0800494 outputs.isA2dpSupported()) {
François Gaffie2110e042015-03-24 08:41:51 +0100495 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
496 if (device2 == AUDIO_DEVICE_NONE) {
497 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
498 }
499 if (device2 == AUDIO_DEVICE_NONE) {
500 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
501 }
502 }
503 if ((device2 == AUDIO_DEVICE_NONE) &&
504 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
505 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
506 }
507 if (device2 == AUDIO_DEVICE_NONE) {
508 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
509 }
Jean-Michel Trivi5c233f82015-04-03 09:21:24 -0700510 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100511 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
512 }
513 if (device2 == AUDIO_DEVICE_NONE) {
514 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
515 }
516 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent904d6322017-03-17 17:20:47 -0700517 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
518 }
519 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100520 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
521 }
522 if (device2 == AUDIO_DEVICE_NONE) {
523 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
524 }
525 if (device2 == AUDIO_DEVICE_NONE) {
526 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
527 }
528 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
529 // no sonification on aux digital (e.g. HDMI)
530 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
531 }
532 if ((device2 == AUDIO_DEVICE_NONE) &&
533 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
534 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
535 }
536 if (device2 == AUDIO_DEVICE_NONE) {
537 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
538 }
539 int device3 = AUDIO_DEVICE_NONE;
540 if (strategy == STRATEGY_MEDIA) {
541 // ARC, SPDIF and AUX_LINE can co-exist with others.
542 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
543 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
544 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
545 }
546
547 device2 |= device3;
548 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
549 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
550 device |= device2;
551
552 // If hdmi system audio mode is on, remove speaker out of output list.
553 if ((strategy == STRATEGY_MEDIA) &&
554 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
555 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
556 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
557 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700558
559 // for STRATEGY_SONIFICATION:
560 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
561 if ((strategy == STRATEGY_SONIFICATION) &&
562 (device & AUDIO_DEVICE_OUT_SPEAKER) &&
563 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
564 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
565 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
566 }
François Gaffie2110e042015-03-24 08:41:51 +0100567 } break;
568
569 default:
570 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
571 break;
572 }
573
Eric Laurent5a2b6292016-04-14 18:05:57 -0700574 if (device == AUDIO_DEVICE_NONE) {
575 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
576 device = mApmObserver->getDefaultOutputDevice()->type();
577 ALOGE_IF(device == AUDIO_DEVICE_NONE,
578 "getDeviceForStrategy() no default device defined");
579 }
François Gaffie2110e042015-03-24 08:41:51 +0100580 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
581 return device;
582}
583
584
585audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
586{
587 const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
588 const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
Eric Laurentc75307b2015-03-17 15:29:32 -0700589 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100590 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
591
592 uint32_t device = AUDIO_DEVICE_NONE;
593
594 switch (inputSource) {
595 case AUDIO_SOURCE_VOICE_UPLINK:
596 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
597 device = AUDIO_DEVICE_IN_VOICE_CALL;
598 break;
599 }
600 break;
601
602 case AUDIO_SOURCE_DEFAULT:
603 case AUDIO_SOURCE_MIC:
604 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
605 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
606 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
607 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
608 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
609 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
610 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700611 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
612 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100613 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
614 device = AUDIO_DEVICE_IN_USB_DEVICE;
615 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
616 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
617 }
618 break;
619
620 case AUDIO_SOURCE_VOICE_COMMUNICATION:
621 // Allow only use of devices on primary input if in call and HAL does not support routing
622 // to voice call path.
623 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
624 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
625 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
626 availableDeviceTypes =
627 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
628 & ~AUDIO_DEVICE_BIT_IN;
629 }
630
631 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
632 case AUDIO_POLICY_FORCE_BT_SCO:
633 // if SCO device is requested but no SCO device is available, fall back to default case
634 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
635 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
636 break;
637 }
638 // FALL THROUGH
639
640 default: // FORCE_NONE
641 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
642 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700643 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
644 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100645 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
646 device = AUDIO_DEVICE_IN_USB_DEVICE;
647 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
648 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
649 }
650 break;
651
652 case AUDIO_POLICY_FORCE_SPEAKER:
653 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
654 device = AUDIO_DEVICE_IN_BACK_MIC;
655 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
656 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
657 }
658 break;
659 }
660 break;
661
662 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800663 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100664 case AUDIO_SOURCE_HOTWORD:
665 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
666 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
667 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
668 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
669 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700670 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
671 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100672 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
673 device = AUDIO_DEVICE_IN_USB_DEVICE;
674 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
675 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
676 }
677 break;
678 case AUDIO_SOURCE_CAMCORDER:
679 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
680 device = AUDIO_DEVICE_IN_BACK_MIC;
681 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
682 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
683 }
684 break;
685 case AUDIO_SOURCE_VOICE_DOWNLINK:
686 case AUDIO_SOURCE_VOICE_CALL:
687 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
688 device = AUDIO_DEVICE_IN_VOICE_CALL;
689 }
690 break;
691 case AUDIO_SOURCE_REMOTE_SUBMIX:
692 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
693 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
694 }
695 break;
696 case AUDIO_SOURCE_FM_TUNER:
697 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
698 device = AUDIO_DEVICE_IN_FM_TUNER;
699 }
700 break;
701 default:
702 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
703 break;
704 }
Eric Laurent5a2b6292016-04-14 18:05:57 -0700705 if (device == AUDIO_DEVICE_NONE) {
706 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
707 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
708 device = AUDIO_DEVICE_IN_STUB;
709 }
710 ALOGE_IF(device == AUDIO_DEVICE_NONE,
711 "getDeviceForInputSource() no default device defined");
712 }
François Gaffie2110e042015-03-24 08:41:51 +0100713 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
714 return device;
715}
716
717template <>
718AudioPolicyManagerInterface *Engine::queryInterface()
719{
720 return &mManagerInterface;
721}
722
723} // namespace audio_policy
724} // namespace android
725
726