blob: b6fff8cb7b2b8a1a59ab61554f30ff69aa2b8356 [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(
280 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
François Gaffie2110e042015-03-24 08:41:51 +0100281 // while media is playing (or has recently played), use the same device
Eric Laurent28d09f02016-03-08 10:43:05 -0800282 device = getDeviceForStrategyInt(
283 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100284 } else {
285 // when media is not playing anymore, fall back on the sonification behavior
Eric Laurent28d09f02016-03-08 10:43:05 -0800286 device = getDeviceForStrategyInt(
287 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100288 //user "safe" speaker if available instead of normal speaker to avoid triggering
289 //other acoustic safety mechanisms for notification
Eric Laurent9a7d9222015-07-02 15:30:23 -0700290 if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
291 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
292 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
293 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
294 }
François Gaffie2110e042015-03-24 08:41:51 +0100295 }
296 break;
297
298 case STRATEGY_DTMF:
299 if (!isInCall()) {
300 // when off call, DTMF strategy follows the same rules as MEDIA strategy
Eric Laurent28d09f02016-03-08 10:43:05 -0800301 device = getDeviceForStrategyInt(
302 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100303 break;
304 }
305 // when in call, DTMF and PHONE strategies follow the same rules
306 // FALL THROUGH
307
308 case STRATEGY_PHONE:
309 // Force use of only devices on primary output if:
310 // - in call AND
311 // - cannot route from voice call RX OR
312 // - audio HAL version is < 3.0 and TX device is on the primary HW module
313 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
314 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
315 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
316 audio_devices_t availPrimaryInputDevices =
317 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
318 audio_devices_t availPrimaryOutputDevices =
319 primaryOutput->supportedDevices() & availableOutputDevices.types();
320
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 Gaffie2110e042015-03-24 08:41:51 +0100330 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
331 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
341 // FALL THROUGH
342
343 default: // FORCE_NONE
344 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
345 if (!isInCall() &&
346 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
347 (outputs.getA2dpOutput() != 0)) {
348 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
349 if (device) break;
350 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
351 if (device) break;
352 }
353 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
354 if (device) break;
355 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
356 if (device) break;
Eric Laurenta0b18ce2016-03-08 11:05:00 -0800357 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
358 if (device) break;
Eric Laurent904d6322017-03-17 17:20:47 -0700359 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
360 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100361 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
362 if (device) break;
363 if (!isInCall()) {
364 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
365 if (device) break;
366 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
367 if (device) break;
368 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
369 if (device) break;
370 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
371 if (device) break;
372 }
373 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
François Gaffie2110e042015-03-24 08:41:51 +0100374 break;
375
376 case AUDIO_POLICY_FORCE_SPEAKER:
377 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
378 // A2DP speaker when forcing to speaker output
379 if (!isInCall() &&
380 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
381 (outputs.getA2dpOutput() != 0)) {
382 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
383 if (device) break;
384 }
385 if (!isInCall()) {
386 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
387 if (device) break;
388 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
389 if (device) break;
390 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
391 if (device) break;
392 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
393 if (device) break;
394 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
395 if (device) break;
396 }
François Gaffie2110e042015-03-24 08:41:51 +0100397 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100398 break;
399 }
400 break;
401
402 case STRATEGY_SONIFICATION:
403
404 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
405 // handleIncallSonification().
406 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800407 device = getDeviceForStrategyInt(
408 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100409 break;
410 }
411 // FALL THROUGH
412
413 case STRATEGY_ENFORCED_AUDIBLE:
414 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
415 // except:
416 // - when in call where it doesn't default to STRATEGY_PHONE behavior
417 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
418
419 if ((strategy == STRATEGY_SONIFICATION) ||
420 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
421 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100422 }
Eric Laurenta8e0f022017-01-27 17:41:53 -0800423
424 // if SCO headset is connected and we are told to use it, play ringtone over
425 // speaker and BT SCO
426 if (((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) &&
427 (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO)) {
428 uint32_t device2 = AUDIO_DEVICE_NONE;
429 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
430 if (device2 == AUDIO_DEVICE_NONE) {
431 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
432 }
433 if (device2 == AUDIO_DEVICE_NONE) {
434 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
435 }
436
437 if (device2 != AUDIO_DEVICE_NONE) {
438 device |= device2;
439 break;
440 }
441 }
François Gaffie2110e042015-03-24 08:41:51 +0100442 // The second device used for sonification is the same as the device used by media strategy
443 // FALL THROUGH
444
François Gaffie2110e042015-03-24 08:41:51 +0100445 case STRATEGY_ACCESSIBILITY:
446 if (strategy == STRATEGY_ACCESSIBILITY) {
447 // do not route accessibility prompts to a digital output currently configured with a
448 // compressed format as they would likely not be mixed and dropped.
449 for (size_t i = 0; i < outputs.size(); i++) {
450 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
451 audio_devices_t devices = desc->device() &
452 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
453 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
454 devices != AUDIO_DEVICE_NONE) {
455 availableOutputDevicesType = availableOutputDevices.types() & ~devices;
456 }
457 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800458 availableOutputDevices =
459 availableOutputDevices.getDevicesFromType(availableOutputDevicesType);
460 if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
461 outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
462 return getDeviceForStrategyInt(
463 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
464 }
465 if (isInCall()) {
466 return getDeviceForStrategyInt(
467 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
468 }
François Gaffie2110e042015-03-24 08:41:51 +0100469 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800470 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
François Gaffie2110e042015-03-24 08:41:51 +0100471 // FALL THROUGH
472
Eric Laurent28d09f02016-03-08 10:43:05 -0800473 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100474 case STRATEGY_REROUTING:
475 case STRATEGY_MEDIA: {
476 uint32_t device2 = AUDIO_DEVICE_NONE;
477 if (strategy != STRATEGY_SONIFICATION) {
478 // no sonification on remote submix (e.g. WFD)
Eric Laurent28d09f02016-03-08 10:43:05 -0800479 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
480 String8("0")) != 0) {
François Gaffie2110e042015-03-24 08:41:51 +0100481 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
482 }
483 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700484 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800485 device = getDeviceForStrategyInt(
486 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700487 break;
488 }
François Gaffie2110e042015-03-24 08:41:51 +0100489 if ((device2 == AUDIO_DEVICE_NONE) &&
490 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
491 (outputs.getA2dpOutput() != 0)) {
492 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
493 if (device2 == AUDIO_DEVICE_NONE) {
494 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
495 }
496 if (device2 == AUDIO_DEVICE_NONE) {
497 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
498 }
499 }
500 if ((device2 == AUDIO_DEVICE_NONE) &&
501 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
502 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
503 }
504 if (device2 == AUDIO_DEVICE_NONE) {
505 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
506 }
Jean-Michel Trivi5c233f82015-04-03 09:21:24 -0700507 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100508 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
509 }
510 if (device2 == AUDIO_DEVICE_NONE) {
511 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
512 }
513 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent904d6322017-03-17 17:20:47 -0700514 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
515 }
516 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100517 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
518 }
519 if (device2 == AUDIO_DEVICE_NONE) {
520 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
521 }
522 if (device2 == AUDIO_DEVICE_NONE) {
523 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
524 }
525 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
526 // no sonification on aux digital (e.g. HDMI)
527 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
528 }
529 if ((device2 == AUDIO_DEVICE_NONE) &&
530 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
531 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
532 }
533 if (device2 == AUDIO_DEVICE_NONE) {
534 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
535 }
536 int device3 = AUDIO_DEVICE_NONE;
537 if (strategy == STRATEGY_MEDIA) {
538 // ARC, SPDIF and AUX_LINE can co-exist with others.
539 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
540 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
541 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
542 }
543
544 device2 |= device3;
545 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
546 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
547 device |= device2;
548
549 // If hdmi system audio mode is on, remove speaker out of output list.
550 if ((strategy == STRATEGY_MEDIA) &&
551 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
552 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
553 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
554 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700555
556 // for STRATEGY_SONIFICATION:
557 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
558 if ((strategy == STRATEGY_SONIFICATION) &&
559 (device & AUDIO_DEVICE_OUT_SPEAKER) &&
560 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
561 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
562 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
563 }
François Gaffie2110e042015-03-24 08:41:51 +0100564 } break;
565
566 default:
567 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
568 break;
569 }
570
Eric Laurent5a2b6292016-04-14 18:05:57 -0700571 if (device == AUDIO_DEVICE_NONE) {
572 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
573 device = mApmObserver->getDefaultOutputDevice()->type();
574 ALOGE_IF(device == AUDIO_DEVICE_NONE,
575 "getDeviceForStrategy() no default device defined");
576 }
François Gaffie2110e042015-03-24 08:41:51 +0100577 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
578 return device;
579}
580
581
582audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
583{
584 const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
585 const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
Eric Laurentc75307b2015-03-17 15:29:32 -0700586 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100587 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
588
589 uint32_t device = AUDIO_DEVICE_NONE;
590
591 switch (inputSource) {
592 case AUDIO_SOURCE_VOICE_UPLINK:
593 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
594 device = AUDIO_DEVICE_IN_VOICE_CALL;
595 break;
596 }
597 break;
598
599 case AUDIO_SOURCE_DEFAULT:
600 case AUDIO_SOURCE_MIC:
601 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
602 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
603 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
604 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
605 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
606 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
607 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700608 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
609 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100610 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
611 device = AUDIO_DEVICE_IN_USB_DEVICE;
612 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
613 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
614 }
615 break;
616
617 case AUDIO_SOURCE_VOICE_COMMUNICATION:
618 // Allow only use of devices on primary input if in call and HAL does not support routing
619 // to voice call path.
620 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
621 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
622 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
623 availableDeviceTypes =
624 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
625 & ~AUDIO_DEVICE_BIT_IN;
626 }
627
628 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
629 case AUDIO_POLICY_FORCE_BT_SCO:
630 // if SCO device is requested but no SCO device is available, fall back to default case
631 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
632 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
633 break;
634 }
635 // FALL THROUGH
636
637 default: // FORCE_NONE
638 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
639 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700640 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
641 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100642 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
643 device = AUDIO_DEVICE_IN_USB_DEVICE;
644 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
645 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
646 }
647 break;
648
649 case AUDIO_POLICY_FORCE_SPEAKER:
650 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
651 device = AUDIO_DEVICE_IN_BACK_MIC;
652 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
653 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
654 }
655 break;
656 }
657 break;
658
659 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800660 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100661 case AUDIO_SOURCE_HOTWORD:
662 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
663 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
664 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
665 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
666 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700667 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
668 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100669 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
670 device = AUDIO_DEVICE_IN_USB_DEVICE;
671 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
672 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
673 }
674 break;
675 case AUDIO_SOURCE_CAMCORDER:
676 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
677 device = AUDIO_DEVICE_IN_BACK_MIC;
678 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
679 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
680 }
681 break;
682 case AUDIO_SOURCE_VOICE_DOWNLINK:
683 case AUDIO_SOURCE_VOICE_CALL:
684 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
685 device = AUDIO_DEVICE_IN_VOICE_CALL;
686 }
687 break;
688 case AUDIO_SOURCE_REMOTE_SUBMIX:
689 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
690 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
691 }
692 break;
693 case AUDIO_SOURCE_FM_TUNER:
694 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
695 device = AUDIO_DEVICE_IN_FM_TUNER;
696 }
697 break;
698 default:
699 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
700 break;
701 }
Eric Laurent5a2b6292016-04-14 18:05:57 -0700702 if (device == AUDIO_DEVICE_NONE) {
703 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
704 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
705 device = AUDIO_DEVICE_IN_STUB;
706 }
707 ALOGE_IF(device == AUDIO_DEVICE_NONE,
708 "getDeviceForInputSource() no default device defined");
709 }
François Gaffie2110e042015-03-24 08:41:51 +0100710 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
711 return device;
712}
713
714template <>
715AudioPolicyManagerInterface *Engine::queryInterface()
716{
717 return &mManagerInterface;
718}
719
720} // namespace audio_policy
721} // namespace android
722
723