blob: 5f0557c816af8af874837f9cfcf6952c8eed6651 [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:
205 case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
206 case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
207 return STRATEGY_MEDIA;
208
209 case AUDIO_USAGE_VOICE_COMMUNICATION:
210 return STRATEGY_PHONE;
211
212 case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
213 return STRATEGY_DTMF;
214
215 case AUDIO_USAGE_ALARM:
216 case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
217 return STRATEGY_SONIFICATION;
218
219 case AUDIO_USAGE_NOTIFICATION:
220 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
221 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
222 case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
223 case AUDIO_USAGE_NOTIFICATION_EVENT:
224 return STRATEGY_SONIFICATION_RESPECTFUL;
225
226 case AUDIO_USAGE_UNKNOWN:
227 default:
228 return STRATEGY_MEDIA;
229 }
230}
231
232audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const
233{
Eric Laurent0f928fa2016-03-21 12:06:20 -0700234 DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices();
235 DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices();
François Gaffie2110e042015-03-24 08:41:51 +0100236
Eric Laurentc75307b2015-03-17 15:29:32 -0700237 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100238
Eric Laurent0f928fa2016-03-21 12:06:20 -0700239 return getDeviceForStrategyInt(strategy, availableOutputDevices,
Eric Laurent28d09f02016-03-08 10:43:05 -0800240 availableInputDevices, outputs);
241}
242
243
244
245audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy,
Eric Laurent0f928fa2016-03-21 12:06:20 -0700246 DeviceVector availableOutputDevices,
247 DeviceVector availableInputDevices,
Eric Laurent28d09f02016-03-08 10:43:05 -0800248 const SwAudioOutputCollection &outputs) const
249{
François Gaffie2110e042015-03-24 08:41:51 +0100250 uint32_t device = AUDIO_DEVICE_NONE;
251 uint32_t availableOutputDevicesType = availableOutputDevices.types();
252
253 switch (strategy) {
254
255 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
256 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100257 break;
258
259 case STRATEGY_SONIFICATION_RESPECTFUL:
260 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800261 device = getDeviceForStrategyInt(
262 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100263 } else if (outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
264 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
265 // while media is playing on a remote device, use the the sonification behavior.
266 // Note that we test this usecase before testing if media is playing because
267 // the isStreamActive() method only informs about the activity of a stream, not
268 // if it's for local playback. Note also that we use the same delay between both tests
Eric Laurent28d09f02016-03-08 10:43:05 -0800269 device = getDeviceForStrategyInt(
270 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100271 //user "safe" speaker if available instead of normal speaker to avoid triggering
272 //other acoustic safety mechanisms for notification
Eric Laurent9a7d9222015-07-02 15:30:23 -0700273 if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
274 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
275 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
276 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
277 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800278 } else if (outputs.isStreamActive(
279 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
François Gaffie2110e042015-03-24 08:41:51 +0100280 // while media is playing (or has recently played), use the same device
Eric Laurent28d09f02016-03-08 10:43:05 -0800281 device = getDeviceForStrategyInt(
282 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100283 } else {
284 // when media is not playing anymore, fall back on the sonification behavior
Eric Laurent28d09f02016-03-08 10:43:05 -0800285 device = getDeviceForStrategyInt(
286 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100287 //user "safe" speaker if available instead of normal speaker to avoid triggering
288 //other acoustic safety mechanisms for notification
Eric Laurent9a7d9222015-07-02 15:30:23 -0700289 if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
290 (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
291 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
292 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
293 }
François Gaffie2110e042015-03-24 08:41:51 +0100294 }
295 break;
296
297 case STRATEGY_DTMF:
298 if (!isInCall()) {
299 // when off call, DTMF strategy follows the same rules as MEDIA strategy
Eric Laurent28d09f02016-03-08 10:43:05 -0800300 device = getDeviceForStrategyInt(
301 STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100302 break;
303 }
304 // when in call, DTMF and PHONE strategies follow the same rules
305 // FALL THROUGH
306
307 case STRATEGY_PHONE:
308 // Force use of only devices on primary output if:
309 // - in call AND
310 // - cannot route from voice call RX OR
311 // - audio HAL version is < 3.0 and TX device is on the primary HW module
312 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
313 audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
314 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
315 audio_devices_t availPrimaryInputDevices =
316 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
317 audio_devices_t availPrimaryOutputDevices =
318 primaryOutput->supportedDevices() & availableOutputDevices.types();
319
320 if (((availableInputDevices.types() &
321 AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
322 (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
Mikhail Naganov9ee05402016-10-13 15:58:17 -0700323 (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
François Gaffie2110e042015-03-24 08:41:51 +0100324 availableOutputDevicesType = availPrimaryOutputDevices;
325 }
326 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800327 // for phone strategy, we first consider the forced use and then the available devices by
328 // order of priority
François Gaffie2110e042015-03-24 08:41:51 +0100329 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
330 case AUDIO_POLICY_FORCE_BT_SCO:
331 if (!isInCall() || strategy != STRATEGY_DTMF) {
332 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
333 if (device) break;
334 }
335 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
336 if (device) break;
337 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
338 if (device) break;
339 // if SCO device is requested but no SCO device is available, fall back to default case
340 // FALL THROUGH
341
342 default: // FORCE_NONE
343 // 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;
François Gaffie2110e042015-03-24 08:41:51 +0100358 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
359 if (device) break;
360 if (!isInCall()) {
361 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
362 if (device) break;
363 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
364 if (device) break;
365 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
366 if (device) break;
367 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
368 if (device) break;
369 }
370 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
François Gaffie2110e042015-03-24 08:41:51 +0100371 break;
372
373 case AUDIO_POLICY_FORCE_SPEAKER:
374 // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
375 // A2DP speaker when forcing to speaker output
376 if (!isInCall() &&
377 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
378 (outputs.getA2dpOutput() != 0)) {
379 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
380 if (device) break;
381 }
382 if (!isInCall()) {
383 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
384 if (device) break;
385 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
386 if (device) break;
387 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
388 if (device) break;
389 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
390 if (device) break;
391 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
392 if (device) break;
393 }
François Gaffie2110e042015-03-24 08:41:51 +0100394 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100395 break;
396 }
397 break;
398
399 case STRATEGY_SONIFICATION:
400
401 // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
402 // handleIncallSonification().
403 if (isInCall()) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800404 device = getDeviceForStrategyInt(
405 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
François Gaffie2110e042015-03-24 08:41:51 +0100406 break;
407 }
408 // FALL THROUGH
409
410 case STRATEGY_ENFORCED_AUDIBLE:
411 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
412 // except:
413 // - when in call where it doesn't default to STRATEGY_PHONE behavior
414 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
415
416 if ((strategy == STRATEGY_SONIFICATION) ||
417 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
418 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
François Gaffie2110e042015-03-24 08:41:51 +0100419 }
420 // The second device used for sonification is the same as the device used by media strategy
421 // FALL THROUGH
422
François Gaffie2110e042015-03-24 08:41:51 +0100423 case STRATEGY_ACCESSIBILITY:
424 if (strategy == STRATEGY_ACCESSIBILITY) {
425 // do not route accessibility prompts to a digital output currently configured with a
426 // compressed format as they would likely not be mixed and dropped.
427 for (size_t i = 0; i < outputs.size(); i++) {
428 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
429 audio_devices_t devices = desc->device() &
430 (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
431 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
432 devices != AUDIO_DEVICE_NONE) {
433 availableOutputDevicesType = availableOutputDevices.types() & ~devices;
434 }
435 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800436 availableOutputDevices =
437 availableOutputDevices.getDevicesFromType(availableOutputDevicesType);
438 if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
439 outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
440 return getDeviceForStrategyInt(
441 STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
442 }
443 if (isInCall()) {
444 return getDeviceForStrategyInt(
445 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
446 }
François Gaffie2110e042015-03-24 08:41:51 +0100447 }
Eric Laurent28d09f02016-03-08 10:43:05 -0800448 // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
François Gaffie2110e042015-03-24 08:41:51 +0100449 // FALL THROUGH
450
Eric Laurent28d09f02016-03-08 10:43:05 -0800451 // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
François Gaffie2110e042015-03-24 08:41:51 +0100452 case STRATEGY_REROUTING:
453 case STRATEGY_MEDIA: {
454 uint32_t device2 = AUDIO_DEVICE_NONE;
455 if (strategy != STRATEGY_SONIFICATION) {
456 // no sonification on remote submix (e.g. WFD)
Eric Laurent28d09f02016-03-08 10:43:05 -0800457 if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
458 String8("0")) != 0) {
François Gaffie2110e042015-03-24 08:41:51 +0100459 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
460 }
461 }
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700462 if (isInCall() && (strategy == STRATEGY_MEDIA)) {
Eric Laurent28d09f02016-03-08 10:43:05 -0800463 device = getDeviceForStrategyInt(
464 STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
Eric Laurenta20d4fa2015-06-04 18:39:28 -0700465 break;
466 }
François Gaffie2110e042015-03-24 08:41:51 +0100467 if ((device2 == AUDIO_DEVICE_NONE) &&
468 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
469 (outputs.getA2dpOutput() != 0)) {
470 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
471 if (device2 == AUDIO_DEVICE_NONE) {
472 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
473 }
474 if (device2 == AUDIO_DEVICE_NONE) {
475 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
476 }
477 }
478 if ((device2 == AUDIO_DEVICE_NONE) &&
479 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
480 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
481 }
482 if (device2 == AUDIO_DEVICE_NONE) {
483 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
484 }
Jean-Michel Trivi5c233f82015-04-03 09:21:24 -0700485 if (device2 == AUDIO_DEVICE_NONE) {
François Gaffie2110e042015-03-24 08:41:51 +0100486 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
487 }
488 if (device2 == AUDIO_DEVICE_NONE) {
489 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
490 }
491 if (device2 == AUDIO_DEVICE_NONE) {
492 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
493 }
494 if (device2 == AUDIO_DEVICE_NONE) {
495 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
496 }
497 if (device2 == AUDIO_DEVICE_NONE) {
498 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
499 }
500 if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
501 // no sonification on aux digital (e.g. HDMI)
502 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
503 }
504 if ((device2 == AUDIO_DEVICE_NONE) &&
505 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
506 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
507 }
508 if (device2 == AUDIO_DEVICE_NONE) {
509 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
510 }
511 int device3 = AUDIO_DEVICE_NONE;
512 if (strategy == STRATEGY_MEDIA) {
513 // ARC, SPDIF and AUX_LINE can co-exist with others.
514 device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
515 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
516 device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
517 }
518
519 device2 |= device3;
520 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
521 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
522 device |= device2;
523
524 // If hdmi system audio mode is on, remove speaker out of output list.
525 if ((strategy == STRATEGY_MEDIA) &&
526 (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
527 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
528 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
529 }
François Gaffie2110e042015-03-24 08:41:51 +0100530 } break;
531
532 default:
533 ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
534 break;
535 }
536
Eric Laurent5a2b6292016-04-14 18:05:57 -0700537 if (device == AUDIO_DEVICE_NONE) {
538 ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
539 device = mApmObserver->getDefaultOutputDevice()->type();
540 ALOGE_IF(device == AUDIO_DEVICE_NONE,
541 "getDeviceForStrategy() no default device defined");
542 }
François Gaffie2110e042015-03-24 08:41:51 +0100543 ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
544 return device;
545}
546
547
548audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
549{
550 const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
551 const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
Eric Laurentc75307b2015-03-17 15:29:32 -0700552 const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
François Gaffie2110e042015-03-24 08:41:51 +0100553 audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
554
555 uint32_t device = AUDIO_DEVICE_NONE;
556
557 switch (inputSource) {
558 case AUDIO_SOURCE_VOICE_UPLINK:
559 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
560 device = AUDIO_DEVICE_IN_VOICE_CALL;
561 break;
562 }
563 break;
564
565 case AUDIO_SOURCE_DEFAULT:
566 case AUDIO_SOURCE_MIC:
567 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
568 device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
569 } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
570 (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
571 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
572 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
573 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
574 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
575 device = AUDIO_DEVICE_IN_USB_DEVICE;
576 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
577 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
578 }
579 break;
580
581 case AUDIO_SOURCE_VOICE_COMMUNICATION:
582 // Allow only use of devices on primary input if in call and HAL does not support routing
583 // to voice call path.
584 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
585 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
586 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
587 availableDeviceTypes =
588 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
589 & ~AUDIO_DEVICE_BIT_IN;
590 }
591
592 switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
593 case AUDIO_POLICY_FORCE_BT_SCO:
594 // if SCO device is requested but no SCO device is available, fall back to default case
595 if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
596 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
597 break;
598 }
599 // FALL THROUGH
600
601 default: // FORCE_NONE
602 if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
603 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
604 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
605 device = AUDIO_DEVICE_IN_USB_DEVICE;
606 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
607 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
608 }
609 break;
610
611 case AUDIO_POLICY_FORCE_SPEAKER:
612 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
613 device = AUDIO_DEVICE_IN_BACK_MIC;
614 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
615 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
616 }
617 break;
618 }
619 break;
620
621 case AUDIO_SOURCE_VOICE_RECOGNITION:
rago8a397d52015-12-02 11:27:57 -0800622 case AUDIO_SOURCE_UNPROCESSED:
François Gaffie2110e042015-03-24 08:41:51 +0100623 case AUDIO_SOURCE_HOTWORD:
624 if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
625 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
626 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
627 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
628 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
629 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
630 device = AUDIO_DEVICE_IN_USB_DEVICE;
631 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
632 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
633 }
634 break;
635 case AUDIO_SOURCE_CAMCORDER:
636 if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
637 device = AUDIO_DEVICE_IN_BACK_MIC;
638 } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
639 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
640 }
641 break;
642 case AUDIO_SOURCE_VOICE_DOWNLINK:
643 case AUDIO_SOURCE_VOICE_CALL:
644 if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
645 device = AUDIO_DEVICE_IN_VOICE_CALL;
646 }
647 break;
648 case AUDIO_SOURCE_REMOTE_SUBMIX:
649 if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
650 device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
651 }
652 break;
653 case AUDIO_SOURCE_FM_TUNER:
654 if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
655 device = AUDIO_DEVICE_IN_FM_TUNER;
656 }
657 break;
658 default:
659 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
660 break;
661 }
Eric Laurent5a2b6292016-04-14 18:05:57 -0700662 if (device == AUDIO_DEVICE_NONE) {
663 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
664 if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
665 device = AUDIO_DEVICE_IN_STUB;
666 }
667 ALOGE_IF(device == AUDIO_DEVICE_NONE,
668 "getDeviceForInputSource() no default device defined");
669 }
François Gaffie2110e042015-03-24 08:41:51 +0100670 ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
671 return device;
672}
673
674template <>
675AudioPolicyManagerInterface *Engine::queryInterface()
676{
677 return &mManagerInterface;
678}
679
680} // namespace audio_policy
681} // namespace android
682
683