blob: e08583c8502217aba6f0fa6efedc671c95742476 [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,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800241 availableInputDevices, outputs, (uint32_t)AUDIO_DEVICE_NONE);
Eric Laurent28d09f02016-03-08 10:43:05 -0800242}
243
244
Eric Laurent28d09f02016-03-08 10:43:05 -0800245audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy,
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800246 DeviceVector availableOutputDevices,
247 DeviceVector availableInputDevices,
248 const SwAudioOutputCollection &outputs,
249 uint32_t outputDeviceTypesToIgnore) const
Eric Laurent28d09f02016-03-08 10:43:05 -0800250{
François Gaffie2110e042015-03-24 08:41:51 +0100251 uint32_t device = AUDIO_DEVICE_NONE;
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800252 uint32_t availableOutputDevicesType =
253 availableOutputDevices.types() & ~outputDeviceTypesToIgnore;
François Gaffie2110e042015-03-24 08:41:51 +0100254
255 switch (strategy) {
256
257 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
258 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100259 break;
260
261 case STRATEGY_SONIFICATION_RESPECTFUL:
262 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800263 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800264 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs,
265 outputDeviceTypesToIgnore);
François Gaffie2110e042015-03-24 08:41:51 +0100266 } else {
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800267 bool media_active_locally =
268 outputs.isStreamActiveLocally(
269 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
270 || outputs.isStreamActiveLocally(
271 AUDIO_STREAM_ACCESSIBILITY, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
272 // routing is same as media without the "remote" device
273 device = getDeviceForStrategyInt(STRATEGY_MEDIA,
274 availableOutputDevices,
275 availableInputDevices, outputs,
276 AUDIO_DEVICE_OUT_REMOTE_SUBMIX | outputDeviceTypesToIgnore);
277 // if no media is playing on the device, check for mandatory use of "safe" speaker
278 // when media would have played on speaker, and the safe speaker path is available
279 if (!media_active_locally
280 && (device & AUDIO_DEVICE_OUT_SPEAKER)
281 && (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
Eric Laurent9a7d9222015-07-02 15:30:23 -0700282 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
283 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
284 }
François Gaffie2110e042015-03-24 08:41:51 +0100285 }
286 break;
287
288 case STRATEGY_DTMF:
289 if (!isInCall()) {
290 // when off call, DTMF strategy follows the same rules as MEDIA strategy
Eric Laurent28d09f02016-03-08 10:43:05 -0800291 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800292 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs,
293 outputDeviceTypesToIgnore);
François Gaffie2110e042015-03-24 08:41:51 +0100294 break;
295 }
296 // when in call, DTMF and PHONE strategies follow the same rules
297 // FALL THROUGH
298
299 case STRATEGY_PHONE:
300 // Force use of only devices on primary output if:
301 // - in call AND
302 // - cannot route from voice call RX OR
303 // - audio HAL version is < 3.0 and TX device is on the primary HW module
304 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
305 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
306 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
307 audio_devices_t availPrimaryInputDevices =
308 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
Eric Laurent58a73fc2018-02-21 18:46:13 -0800309
310 // TODO: getPrimaryOutput return only devices from first module in
311 // audio_policy_configuration.xml, hearing aid is not there, but it's
312 // a primary device
313 // FIXME: this is not the right way of solving this problem
François Gaffie2110e042015-03-24 08:41:51 +0100314 audio_devices_t availPrimaryOutputDevices =
Eric Laurent58a73fc2018-02-21 18:46:13 -0800315 (primaryOutput->supportedDevices() | AUDIO_DEVICE_OUT_HEARING_AID) &
316 availableOutputDevices.types();
François Gaffie2110e042015-03-24 08:41:51 +0100317
318 if (((availableInputDevices.types() &
319 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
320 (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700321 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
François Gaffie2110e042015-03-24 08:41:51 +0100322 availableOutputDevicesType = availPrimaryOutputDevices;
323 }
324 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800325 // for phone strategy, we first consider the forced use and then the available devices by
326 // order of priority
François Gaffie2110e042015-03-24 08:41:51 +0100327 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
328 case AUDIO_POLICY_FORCE_BT_SCO:
329 if (!isInCall() || strategy != STRATEGY_DTMF) {
330 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
331 if (device) break;
332 }
333 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
334 if (device) break;
335 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
336 if (device) break;
337 // if SCO device is requested but no SCO device is available, fall back to default case
338 // FALL THROUGH
339
340 default: // FORCE_NONE
Eric Laurent58a73fc2018-02-21 18:46:13 -0800341 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID;
342 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100343 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
344 if (!isInCall() &&
345 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
346 (outputs.getA2dpOutput() != 0)) {
347 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
348 if (device) break;
349 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
350 if (device) break;
351 }
352 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
353 if (device) break;
354 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
355 if (device) break;
Eric Laurenta0b18ce2016-03-08 11:05:00 -0800356 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
357 if (device) break;
Eric Laurent904d6322017-03-17 17:20:47 -0700358 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
359 if (device) break;
François Gaffie2110e042015-03-24 08:41:51 +0100360 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
361 if (device) break;
362 if (!isInCall()) {
363 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
364 if (device) break;
365 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
366 if (device) break;
367 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
368 if (device) break;
369 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
370 if (device) break;
371 }
372 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
François Gaffie2110e042015-03-24 08:41:51 +0100373 break;
374
375 case AUDIO_POLICY_FORCE_SPEAKER:
376 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
377 // A2DP speaker when forcing to speaker output
378 if (!isInCall() &&
379 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
380 (outputs.getA2dpOutput() != 0)) {
381 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
382 if (device) break;
383 }
384 if (!isInCall()) {
385 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
386 if (device) break;
387 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
388 if (device) break;
389 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
390 if (device) break;
391 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
392 if (device) break;
393 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
394 if (device) break;
395 }
François Gaffie2110e042015-03-24 08:41:51 +0100396 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100397 break;
398 }
399 break;
400
401 case STRATEGY_SONIFICATION:
402
403 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
404 // handleIncallSonification().
405 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800406 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800407 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
408 outputDeviceTypesToIgnore);
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(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800463 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs,
464 outputDeviceTypesToIgnore);
Eric Laurent28d09f02016-03-08 10:43:05 -0800465 }
466 if (isInCall()) {
467 return getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800468 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
469 outputDeviceTypesToIgnore);
Eric Laurent28d09f02016-03-08 10:43:05 -0800470 }
François Gaffie2110e042015-03-24 08:41:51 +0100471 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800472 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
François Gaffie2110e042015-03-24 08:41:51 +0100473 // FALL THROUGH
474
Eric Laurent28d09f02016-03-08 10:43:05 -0800475 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100476 case STRATEGY_REROUTING:
477 case STRATEGY_MEDIA: {
478 uint32_t device2 = AUDIO_DEVICE_NONE;
479 if (strategy != STRATEGY_SONIFICATION) {
480 // no sonification on remote submix (e.g. WFD)
Eric Laurent28d09f02016-03-08 10:43:05 -0800481 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
482 String8("0")) != 0) {
François Gaffie2110e042015-03-24 08:41:51 +0100483 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
484 }
485 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700486 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800487 device = getDeviceForStrategyInt(
Jean-Michel Trivi61309522018-01-23 09:58:17 -0800488 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs,
489 outputDeviceTypesToIgnore);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700490 break;
491 }
Eric Laurent58a73fc2018-02-21 18:46:13 -0800492 if (device2 == AUDIO_DEVICE_NONE) {
493 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HEARING_AID;
494 }
François Gaffie2110e042015-03-24 08:41:51 +0100495 if ((device2 == AUDIO_DEVICE_NONE) &&
496 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
497 (outputs.getA2dpOutput() != 0)) {
498 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
499 if (device2 == AUDIO_DEVICE_NONE) {
500 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
501 }
502 if (device2 == AUDIO_DEVICE_NONE) {
503 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
504 }
505 }
506 if ((device2 == AUDIO_DEVICE_NONE) &&
507 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
508 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
509 }
510 if (device2 == AUDIO_DEVICE_NONE) {
511 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
512 }
Jean-Michel Trivi5c233f82015-04-03 09:21:24 -0700513 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100514 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
515 }
516 if (device2 == AUDIO_DEVICE_NONE) {
517 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
518 }
519 if (device2 == AUDIO_DEVICE_NONE) {
Eric Laurent904d6322017-03-17 17:20:47 -0700520 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
521 }
522 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100523 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
524 }
525 if (device2 == AUDIO_DEVICE_NONE) {
526 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
527 }
528 if (device2 == AUDIO_DEVICE_NONE) {
529 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
530 }
531 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
532 // no sonification on aux digital (e.g. HDMI)
533 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
534 }
535 if ((device2 == AUDIO_DEVICE_NONE) &&
536 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
537 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
538 }
539 if (device2 == AUDIO_DEVICE_NONE) {
540 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
541 }
542 int device3 = AUDIO_DEVICE_NONE;
543 if (strategy == STRATEGY_MEDIA) {
544 // ARC, SPDIF and AUX_LINE can co-exist with others.
545 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
546 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
547 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
548 }
549
550 device2 |= device3;
551 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
552 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
553 device |= device2;
554
555 // If hdmi system audio mode is on, remove speaker out of output list.
556 if ((strategy == STRATEGY_MEDIA) &&
557 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
558 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
559 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
560 }
Jean-Michel Trivi654afa02017-05-11 14:12:33 -0700561
562 // for STRATEGY_SONIFICATION:
563 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
564 if ((strategy == STRATEGY_SONIFICATION) &&
565 (device & AUDIO_DEVICE_OUT_SPEAKER) &&
566 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
567 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
568 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
569 }
François Gaffie2110e042015-03-24 08:41:51 +0100570 } break;
571
572 default:
573 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
574 break;
575 }
576
Eric Laurent5a2b6292016-04-14 18:05:57 -0700577 if (device == AUDIO_DEVICE_NONE) {
578 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
579 device = mApmObserver->getDefaultOutputDevice()->type();
580 ALOGE_IF(device == AUDIO_DEVICE_NONE,
581 "getDeviceForStrategy() no default device defined");
582 }
François Gaffie2110e042015-03-24 08:41:51 +0100583 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
584 return device;
585}
586
587
588audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
589{
590 const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
591 const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
Eric Laurentc75307b2015-03-17 15:29:32 -0700592 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100593 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
594
595 uint32_t device = AUDIO_DEVICE_NONE;
596
597 switch (inputSource) {
598 case AUDIO_SOURCE_VOICE_UPLINK:
599 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
600 device = AUDIO_DEVICE_IN_VOICE_CALL;
601 break;
602 }
603 break;
604
605 case AUDIO_SOURCE_DEFAULT:
606 case AUDIO_SOURCE_MIC:
607 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
608 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
609 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
610 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
611 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
612 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
613 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700614 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
615 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100616 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
617 device = AUDIO_DEVICE_IN_USB_DEVICE;
618 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
619 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
620 }
621 break;
622
623 case AUDIO_SOURCE_VOICE_COMMUNICATION:
624 // Allow only use of devices on primary input if in call and HAL does not support routing
625 // to voice call path.
626 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
627 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
628 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
629 availableDeviceTypes =
630 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
631 & ~AUDIO_DEVICE_BIT_IN;
632 }
633
634 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
635 case AUDIO_POLICY_FORCE_BT_SCO:
636 // if SCO device is requested but no SCO device is available, fall back to default case
637 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
638 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
639 break;
640 }
641 // FALL THROUGH
642
643 default: // FORCE_NONE
644 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
645 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700646 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
647 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100648 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
649 device = AUDIO_DEVICE_IN_USB_DEVICE;
650 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
651 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
652 }
653 break;
654
655 case AUDIO_POLICY_FORCE_SPEAKER:
656 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
657 device = AUDIO_DEVICE_IN_BACK_MIC;
658 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
659 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
660 }
661 break;
662 }
663 break;
664
665 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800666 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100667 case AUDIO_SOURCE_HOTWORD:
668 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
669 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
670 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
671 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
672 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
Eric Laurent904d6322017-03-17 17:20:47 -0700673 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
674 device = AUDIO_DEVICE_IN_USB_HEADSET;
François Gaffie2110e042015-03-24 08:41:51 +0100675 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
676 device = AUDIO_DEVICE_IN_USB_DEVICE;
677 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
678 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
679 }
680 break;
681 case AUDIO_SOURCE_CAMCORDER:
682 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
683 device = AUDIO_DEVICE_IN_BACK_MIC;
684 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
685 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
686 }
687 break;
688 case AUDIO_SOURCE_VOICE_DOWNLINK:
689 case AUDIO_SOURCE_VOICE_CALL:
690 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
691 device = AUDIO_DEVICE_IN_VOICE_CALL;
692 }
693 break;
694 case AUDIO_SOURCE_REMOTE_SUBMIX:
695 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
696 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
697 }
698 break;
699 case AUDIO_SOURCE_FM_TUNER:
700 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
701 device = AUDIO_DEVICE_IN_FM_TUNER;
702 }
703 break;
704 default:
705 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
706 break;
707 }
Eric Laurent5a2b6292016-04-14 18:05:57 -0700708 if (device == AUDIO_DEVICE_NONE) {
709 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
710 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
711 device = AUDIO_DEVICE_IN_STUB;
712 }
713 ALOGE_IF(device == AUDIO_DEVICE_NONE,
714 "getDeviceForInputSource() no default device defined");
715 }
François Gaffie2110e042015-03-24 08:41:51 +0100716 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
717 return device;
718}
719
720template <>
721AudioPolicyManagerInterface *Engine::queryInterface()
722{
723 return &mManagerInterface;
724}
725
726} // namespace audio_policy
727} // namespace android
728
729