1 /*
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"
28 #include <android-base/macros.h>
29 #include <AudioPolicyManagerObserver.h>
30 #include <PolicyAudioPort.h>
31 #include <IOProfile.h>
32 #include <AudioIODescriptorInterface.h>
33 #include <policy.h>
34 #include <media/AudioContainers.h>
35 #include <utils/String8.h>
36 #include <utils/Log.h>
37
38 namespace android
39 {
40 namespace audio_policy
41 {
42
43 struct legacy_strategy_map { const char *name; legacy_strategy id; };
getLegacyStrategy()44 static const std::vector<legacy_strategy_map>& getLegacyStrategy() {
45 static const std::vector<legacy_strategy_map> legacyStrategy = {
46 { "STRATEGY_NONE", STRATEGY_NONE },
47 { "STRATEGY_MEDIA", STRATEGY_MEDIA },
48 { "STRATEGY_PHONE", STRATEGY_PHONE },
49 { "STRATEGY_SONIFICATION", STRATEGY_SONIFICATION },
50 { "STRATEGY_SONIFICATION_RESPECTFUL", STRATEGY_SONIFICATION_RESPECTFUL },
51 { "STRATEGY_DTMF", STRATEGY_DTMF },
52 { "STRATEGY_ENFORCED_AUDIBLE", STRATEGY_ENFORCED_AUDIBLE },
53 { "STRATEGY_TRANSMITTED_THROUGH_SPEAKER", STRATEGY_TRANSMITTED_THROUGH_SPEAKER },
54 { "STRATEGY_ACCESSIBILITY", STRATEGY_ACCESSIBILITY },
55 { "STRATEGY_REROUTING", STRATEGY_REROUTING },
56 { "STRATEGY_PATCH", STRATEGY_REROUTING }, // boiler to manage stream patch volume
57 { "STRATEGY_CALL_ASSISTANT", STRATEGY_CALL_ASSISTANT },
58 };
59 return legacyStrategy;
60 }
61
Engine()62 Engine::Engine()
63 {
64 auto result = EngineBase::loadAudioPolicyEngineConfig();
65 ALOGE_IF(result.nbSkippedElement != 0,
66 "Policy Engine configuration is partially invalid, skipped %zu elements",
67 result.nbSkippedElement);
68
69 auto legacyStrategy = getLegacyStrategy();
70 for (const auto &strategy : legacyStrategy) {
71 mLegacyStrategyMap[getProductStrategyByName(strategy.name)] = strategy.id;
72 }
73 }
74
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)75 status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
76 {
77 switch(usage) {
78 case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
79 if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
80 config != AUDIO_POLICY_FORCE_NONE) {
81 ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
82 return BAD_VALUE;
83 }
84 break;
85 case AUDIO_POLICY_FORCE_FOR_MEDIA:
86 if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
87 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
88 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
89 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
90 config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
91 ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
92 return BAD_VALUE;
93 }
94 break;
95 case AUDIO_POLICY_FORCE_FOR_RECORD:
96 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
97 config != AUDIO_POLICY_FORCE_NONE) {
98 ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
99 return BAD_VALUE;
100 }
101 break;
102 case AUDIO_POLICY_FORCE_FOR_DOCK:
103 if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
104 config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
105 config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
106 config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
107 config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
108 ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
109 }
110 break;
111 case AUDIO_POLICY_FORCE_FOR_SYSTEM:
112 if (config != AUDIO_POLICY_FORCE_NONE &&
113 config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
114 ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
115 }
116 break;
117 case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
118 if (config != AUDIO_POLICY_FORCE_NONE &&
119 config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
120 ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
121 }
122 break;
123 case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
124 if (config != AUDIO_POLICY_FORCE_NONE &&
125 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
126 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS &&
127 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_MANUAL) {
128 ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
129 return BAD_VALUE;
130 }
131 break;
132 case AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING:
133 if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_NONE) {
134 ALOGW("setForceUse() invalid config %d for FOR_VIBRATE_RINGING", config);
135 return BAD_VALUE;
136 }
137 break;
138 default:
139 ALOGW("setForceUse() invalid usage %d", usage);
140 break; // TODO return BAD_VALUE?
141 }
142 return EngineBase::setForceUse(usage, config);
143 }
144
filterOutputDevicesForStrategy(legacy_strategy strategy,DeviceVector & availableOutputDevices,const SwAudioOutputCollection & outputs) const145 void Engine::filterOutputDevicesForStrategy(legacy_strategy strategy,
146 DeviceVector& availableOutputDevices,
147 const SwAudioOutputCollection &outputs) const
148 {
149 DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
150
151 switch (strategy) {
152 case STRATEGY_SONIFICATION_RESPECTFUL: {
153 if (!(isInCall() || outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL)))) {
154 // routing is same as media without the "remote" device
155 availableOutputDevices.remove(availableOutputDevices.getDevicesFromType(
156 AUDIO_DEVICE_OUT_REMOTE_SUBMIX));
157 }
158 } break;
159 case STRATEGY_DTMF:
160 case STRATEGY_PHONE: {
161 // Force use of only devices on primary output if:
162 // - in call AND
163 // - cannot route from voice call RX OR
164 // - audio HAL version is < 3.0 and TX device is on the primary HW module
165 if (getPhoneState() == AUDIO_MODE_IN_CALL) {
166 audio_devices_t txDevice = getDeviceForInputSource(
167 AUDIO_SOURCE_VOICE_COMMUNICATION)->type();
168 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
169 LOG_ALWAYS_FATAL_IF(primaryOutput == nullptr, "Primary output not found");
170 DeviceVector availPrimaryInputDevices =
171 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
172
173 // TODO: getPrimaryOutput return only devices from first module in
174 // audio_policy_configuration.xml, hearing aid is not there, but it's
175 // a primary device
176 // FIXME: this is not the right way of solving this problem
177 DeviceVector availPrimaryOutputDevices = availableOutputDevices.getDevicesFromTypes(
178 primaryOutput->supportedDevices().types());
179 availPrimaryOutputDevices.add(
180 availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID));
181
182 if ((availableInputDevices.getDevice(AUDIO_DEVICE_IN_TELEPHONY_RX,
183 String8(""), AUDIO_FORMAT_DEFAULT) == nullptr) ||
184 ((availPrimaryInputDevices.getDevice(
185 txDevice, String8(""), AUDIO_FORMAT_DEFAULT) != nullptr) &&
186 (primaryOutput->getPolicyAudioPort()->getModuleVersionMajor() < 3))) {
187 availableOutputDevices = availPrimaryOutputDevices;
188 }
189
190 }
191 // Do not use A2DP devices when in call but use them when not in call
192 // (e.g for voice mail playback)
193 if (isInCall()) {
194 availableOutputDevices.remove(availableOutputDevices.getDevicesFromTypes({
195 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP, AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES,
196 AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, }));
197 }
198 } break;
199 case STRATEGY_ACCESSIBILITY: {
200 // do not route accessibility prompts to a digital output currently configured with a
201 // compressed format as they would likely not be mixed and dropped.
202 for (size_t i = 0; i < outputs.size(); i++) {
203 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
204 if (desc->isActive() && !audio_is_linear_pcm(desc->getFormat())) {
205 availableOutputDevices.remove(desc->devices().getDevicesFromTypes({
206 AUDIO_DEVICE_OUT_HDMI, AUDIO_DEVICE_OUT_SPDIF,
207 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC}));
208 }
209 }
210 } break;
211 default:
212 break;
213 }
214 }
215
remapStrategyFromContext(product_strategy_t strategy,const SwAudioOutputCollection & outputs) const216 product_strategy_t Engine::remapStrategyFromContext(product_strategy_t strategy,
217 const SwAudioOutputCollection &outputs) const {
218 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
219 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
220
221 if (isInCall()) {
222 switch (legacyStrategy) {
223 case STRATEGY_ACCESSIBILITY:
224 case STRATEGY_DTMF:
225 case STRATEGY_MEDIA:
226 case STRATEGY_SONIFICATION:
227 case STRATEGY_SONIFICATION_RESPECTFUL:
228 legacyStrategy = STRATEGY_PHONE;
229 break;
230
231 default:
232 return strategy;
233 }
234 } else {
235 switch (legacyStrategy) {
236 case STRATEGY_SONIFICATION_RESPECTFUL:
237 case STRATEGY_SONIFICATION:
238 if (outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_VOICE_CALL))) {
239 legacyStrategy = STRATEGY_PHONE;
240 }
241 break;
242
243 case STRATEGY_ACCESSIBILITY:
244 if (outputs.isActive(toVolumeSource(AUDIO_STREAM_RING)) ||
245 outputs.isActive(toVolumeSource(AUDIO_STREAM_ALARM))) {
246 legacyStrategy = STRATEGY_SONIFICATION;
247 }
248 break;
249
250 default:
251 return strategy;
252 }
253 }
254 return getProductStrategyFromLegacy(legacyStrategy);
255 }
256
getDevicesForStrategyInt(legacy_strategy strategy,DeviceVector availableOutputDevices,const SwAudioOutputCollection & outputs) const257 DeviceVector Engine::getDevicesForStrategyInt(legacy_strategy strategy,
258 DeviceVector availableOutputDevices,
259 const SwAudioOutputCollection &outputs) const
260 {
261 DeviceVector devices;
262
263 switch (strategy) {
264
265 case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
266 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
267 break;
268
269 case STRATEGY_PHONE: {
270 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_HEARING_AID);
271 if (!devices.isEmpty()) break;
272 devices = availableOutputDevices.getFirstDevicesFromTypes(
273 getLastRemovableMediaDevices());
274 if (!devices.isEmpty()) break;
275 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_EARPIECE);
276 } break;
277
278 case STRATEGY_SONIFICATION:
279 case STRATEGY_ENFORCED_AUDIBLE:
280 // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
281 // except:
282 // - when in call where it doesn't default to STRATEGY_PHONE behavior
283 // - in countries where not enforced in which case it follows STRATEGY_MEDIA
284
285 if ((strategy == STRATEGY_SONIFICATION) ||
286 (getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
287 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
288 }
289
290 // if SCO headset is connected and we are told to use it, play ringtone over
291 // speaker and BT SCO
292 if (!availableOutputDevices.getDevicesFromTypes(getAudioDeviceOutAllScoSet()).isEmpty()) {
293 DeviceVector devices2;
294 devices2 = availableOutputDevices.getFirstDevicesFromTypes({
295 AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT, AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET,
296 AUDIO_DEVICE_OUT_BLUETOOTH_SCO});
297 // Use ONLY Bluetooth SCO output when ringing in vibration mode
298 if (!((getForceUse(AUDIO_POLICY_FORCE_FOR_SYSTEM) == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)
299 && (strategy == STRATEGY_ENFORCED_AUDIBLE))) {
300 if (getForceUse(AUDIO_POLICY_FORCE_FOR_VIBRATE_RINGING)
301 == AUDIO_POLICY_FORCE_BT_SCO) {
302 if (!devices2.isEmpty()) {
303 devices = devices2;
304 break;
305 }
306 }
307 }
308 // Use both Bluetooth SCO and phone default output when ringing in normal mode
309 if (audio_is_bluetooth_out_sco_device(getPreferredDeviceTypeForLegacyStrategy(
310 availableOutputDevices, STRATEGY_PHONE))) {
311 if (strategy == STRATEGY_SONIFICATION) {
312 devices.replaceDevicesByType(
313 AUDIO_DEVICE_OUT_SPEAKER,
314 availableOutputDevices.getDevicesFromType(
315 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
316 }
317 if (!devices2.isEmpty()) {
318 devices.add(devices2);
319 break;
320 }
321 }
322 }
323 // The second device used for sonification is the same as the device used by media strategy
324 FALLTHROUGH_INTENDED;
325
326 case STRATEGY_DTMF:
327 case STRATEGY_ACCESSIBILITY:
328 case STRATEGY_SONIFICATION_RESPECTFUL:
329 case STRATEGY_REROUTING:
330 case STRATEGY_MEDIA: {
331 DeviceVector devices2;
332 if (strategy != STRATEGY_SONIFICATION) {
333 // no sonification on remote submix (e.g. WFD)
334 sp<DeviceDescriptor> remoteSubmix;
335 if ((remoteSubmix = availableOutputDevices.getDevice(
336 AUDIO_DEVICE_OUT_REMOTE_SUBMIX, String8("0"),
337 AUDIO_FORMAT_DEFAULT)) != nullptr) {
338 devices2.add(remoteSubmix);
339 }
340 }
341
342 if ((devices2.isEmpty()) &&
343 (getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) == AUDIO_POLICY_FORCE_SPEAKER)) {
344 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
345 }
346 if (devices2.isEmpty() && (getLastRemovableMediaDevices().size() > 0)) {
347 if ((getForceUse(AUDIO_POLICY_FORCE_FOR_MEDIA) != AUDIO_POLICY_FORCE_NO_BT_A2DP)) {
348 // Get the last connected device of wired and bluetooth a2dp
349 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
350 getLastRemovableMediaDevices());
351 } else {
352 // Get the last connected device of wired except bluetooth a2dp
353 devices2 = availableOutputDevices.getFirstDevicesFromTypes(
354 getLastRemovableMediaDevices(GROUP_WIRED));
355 }
356 }
357 if ((devices2.isEmpty()) && (strategy != STRATEGY_SONIFICATION)) {
358 // no sonification on aux digital (e.g. HDMI)
359 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_AUX_DIGITAL);
360 }
361 if ((devices2.isEmpty()) &&
362 (getForceUse(AUDIO_POLICY_FORCE_FOR_DOCK) == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
363 devices2 = availableOutputDevices.getDevicesFromType(
364 AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET);
365 }
366 if (devices2.isEmpty()) {
367 devices2 = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER);
368 }
369 DeviceVector devices3;
370 if (strategy == STRATEGY_MEDIA) {
371 // ARC, SPDIF and AUX_LINE can co-exist with others.
372 devices3 = availableOutputDevices.getDevicesFromTypes({
373 AUDIO_DEVICE_OUT_HDMI_ARC, AUDIO_DEVICE_OUT_HDMI_EARC,
374 AUDIO_DEVICE_OUT_SPDIF, AUDIO_DEVICE_OUT_AUX_LINE,
375 });
376 }
377
378 devices2.add(devices3);
379 // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
380 // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
381 devices.add(devices2);
382
383 // If hdmi system audio mode is on, remove speaker out of output list.
384 if ((strategy == STRATEGY_MEDIA) &&
385 (getForceUse(AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO) ==
386 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
387 devices.remove(devices.getDevicesFromType(AUDIO_DEVICE_OUT_SPEAKER));
388 }
389
390 bool mediaActiveLocally =
391 outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_MUSIC),
392 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)
393 || outputs.isActiveLocally(
394 toVolumeSource(AUDIO_STREAM_ACCESSIBILITY),
395 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY);
396
397 bool ringActiveLocally = outputs.isActiveLocally(toVolumeSource(AUDIO_STREAM_RING), 0);
398 // - for STRATEGY_SONIFICATION and ringtone active:
399 // if SPEAKER was selected, and SPEAKER_SAFE is available, use SPEAKER_SAFE instead
400 // - for STRATEGY_SONIFICATION_RESPECTFUL:
401 // if no media is playing on the device, check for mandatory use of "safe" speaker
402 // when media would have played on speaker, and the safe speaker path is available
403 if (strategy == STRATEGY_SONIFICATION || ringActiveLocally
404 || (strategy == STRATEGY_SONIFICATION_RESPECTFUL && !mediaActiveLocally)) {
405 devices.replaceDevicesByType(
406 AUDIO_DEVICE_OUT_SPEAKER,
407 availableOutputDevices.getDevicesFromType(
408 AUDIO_DEVICE_OUT_SPEAKER_SAFE));
409 }
410 } break;
411
412 case STRATEGY_CALL_ASSISTANT:
413 devices = availableOutputDevices.getDevicesFromType(AUDIO_DEVICE_OUT_TELEPHONY_TX);
414 break;
415
416 case STRATEGY_NONE:
417 // Happens when internal strategies are processed ("rerouting", "patch"...)
418 break;
419
420 default:
421 ALOGW("%s unknown strategy: %d", __func__, strategy);
422 break;
423 }
424
425 if (devices.isEmpty()) {
426 ALOGV("%s no device found for strategy %d", __func__, strategy);
427 sp<DeviceDescriptor> defaultOutputDevice = getApmObserver()->getDefaultOutputDevice();
428 if (defaultOutputDevice != nullptr) {
429 devices.add(defaultOutputDevice);
430 }
431 ALOGE_IF(devices.isEmpty(),
432 "%s no default device defined", __func__);
433 }
434
435 ALOGVV("%s strategy %d, device %s", __func__,
436 strategy, dumpDeviceTypes(devices.types()).c_str());
437 return devices;
438 }
439
440
getDeviceForInputSource(audio_source_t inputSource) const441 sp<DeviceDescriptor> Engine::getDeviceForInputSource(audio_source_t inputSource) const
442 {
443 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
444 const DeviceVector availableInputDevices = getApmObserver()->getAvailableInputDevices();
445 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
446 DeviceVector availableDevices = availableInputDevices;
447 sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
448 DeviceVector availablePrimaryDevices = primaryOutput == nullptr ? DeviceVector()
449 : availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
450 sp<DeviceDescriptor> device;
451
452 // when a call is active, force device selection to match source VOICE_COMMUNICATION
453 // for most other input sources to avoid rerouting call TX audio
454 if (isInCall()) {
455 switch (inputSource) {
456 case AUDIO_SOURCE_DEFAULT:
457 case AUDIO_SOURCE_MIC:
458 case AUDIO_SOURCE_VOICE_RECOGNITION:
459 case AUDIO_SOURCE_UNPROCESSED:
460 case AUDIO_SOURCE_HOTWORD:
461 case AUDIO_SOURCE_CAMCORDER:
462 case AUDIO_SOURCE_VOICE_PERFORMANCE:
463 inputSource = AUDIO_SOURCE_VOICE_COMMUNICATION;
464 break;
465 default:
466 break;
467 }
468 }
469
470 audio_devices_t commDeviceType =
471 getPreferredDeviceTypeForLegacyStrategy(availableOutputDevices, STRATEGY_PHONE);
472
473 switch (inputSource) {
474 case AUDIO_SOURCE_DEFAULT:
475 case AUDIO_SOURCE_MIC:
476 device = availableDevices.getDevice(
477 AUDIO_DEVICE_IN_BLUETOOTH_A2DP, String8(""), AUDIO_FORMAT_DEFAULT);
478 if (device != nullptr) break;
479 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
480 device = availableDevices.getDevice(
481 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
482 if (device != nullptr) break;
483 }
484 device = availableDevices.getFirstExistingDevice({
485 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
486 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
487 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
488 break;
489
490 case AUDIO_SOURCE_VOICE_COMMUNICATION:
491 // Allow only use of devices on primary input if in call and HAL does not support routing
492 // to voice call path.
493 if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
494 (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_TELEPHONY_TX,
495 String8(""), AUDIO_FORMAT_DEFAULT)) == nullptr) {
496 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
497 availableDevices = availablePrimaryDevices;
498 }
499
500 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
501 // if SCO device is requested but no SCO device is available, fall back to default case
502 device = availableDevices.getDevice(
503 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
504 if (device != nullptr) {
505 break;
506 }
507 }
508 switch (commDeviceType) {
509 case AUDIO_DEVICE_OUT_BLE_HEADSET:
510 device = availableDevices.getDevice(
511 AUDIO_DEVICE_IN_BLE_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
512 break;
513 case AUDIO_DEVICE_OUT_SPEAKER:
514 device = availableDevices.getFirstExistingDevice({
515 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
516 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_USB_HEADSET});
517 break;
518 default: // FORCE_NONE
519 device = availableDevices.getFirstExistingDevice({
520 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
521 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
522 AUDIO_DEVICE_IN_BUILTIN_MIC});
523 break;
524
525 }
526 break;
527
528 case AUDIO_SOURCE_VOICE_RECOGNITION:
529 case AUDIO_SOURCE_UNPROCESSED:
530 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
531 device = availableDevices.getDevice(
532 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
533 if (device != nullptr) break;
534 }
535 // we need to make BLUETOOTH_BLE has higher priority than BUILTIN_MIC,
536 // because sometimes user want to do voice search by bt remote
537 // even if BUILDIN_MIC is available.
538 device = availableDevices.getFirstExistingDevice({
539 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
540 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
541 AUDIO_DEVICE_IN_BLUETOOTH_BLE, AUDIO_DEVICE_IN_BUILTIN_MIC});
542
543 break;
544 case AUDIO_SOURCE_HOTWORD:
545 // We should not use primary output criteria for Hotword but rather limit
546 // to devices attached to the same HW module as the build in mic
547 LOG_ALWAYS_FATAL_IF(availablePrimaryDevices.isEmpty(), "Primary devices not found");
548 availableDevices = availablePrimaryDevices;
549 if (audio_is_bluetooth_out_sco_device(commDeviceType)) {
550 device = availableDevices.getDevice(
551 AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET, String8(""), AUDIO_FORMAT_DEFAULT);
552 if (device != nullptr) break;
553 }
554 device = availableDevices.getFirstExistingDevice({
555 AUDIO_DEVICE_IN_BLE_HEADSET, AUDIO_DEVICE_IN_WIRED_HEADSET,
556 AUDIO_DEVICE_IN_USB_HEADSET, AUDIO_DEVICE_IN_USB_DEVICE,
557 AUDIO_DEVICE_IN_BUILTIN_MIC});
558 break;
559 case AUDIO_SOURCE_CAMCORDER:
560 // For a device without built-in mic, adding usb device
561 device = availableDevices.getFirstExistingDevice({
562 AUDIO_DEVICE_IN_BACK_MIC, AUDIO_DEVICE_IN_BUILTIN_MIC,
563 AUDIO_DEVICE_IN_USB_DEVICE});
564 break;
565 case AUDIO_SOURCE_VOICE_DOWNLINK:
566 case AUDIO_SOURCE_VOICE_CALL:
567 case AUDIO_SOURCE_VOICE_UPLINK:
568 device = availableDevices.getDevice(
569 AUDIO_DEVICE_IN_VOICE_CALL, String8(""), AUDIO_FORMAT_DEFAULT);
570 break;
571 case AUDIO_SOURCE_VOICE_PERFORMANCE:
572 device = availableDevices.getFirstExistingDevice({
573 AUDIO_DEVICE_IN_WIRED_HEADSET, AUDIO_DEVICE_IN_USB_HEADSET,
574 AUDIO_DEVICE_IN_USB_DEVICE, AUDIO_DEVICE_IN_BLUETOOTH_BLE,
575 AUDIO_DEVICE_IN_BUILTIN_MIC});
576 break;
577 case AUDIO_SOURCE_REMOTE_SUBMIX:
578 device = availableDevices.getDevice(
579 AUDIO_DEVICE_IN_REMOTE_SUBMIX, String8(""), AUDIO_FORMAT_DEFAULT);
580 break;
581 case AUDIO_SOURCE_FM_TUNER:
582 device = availableDevices.getDevice(
583 AUDIO_DEVICE_IN_FM_TUNER, String8(""), AUDIO_FORMAT_DEFAULT);
584 break;
585 case AUDIO_SOURCE_ECHO_REFERENCE:
586 device = availableDevices.getDevice(
587 AUDIO_DEVICE_IN_ECHO_REFERENCE, String8(""), AUDIO_FORMAT_DEFAULT);
588 break;
589 default:
590 ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
591 break;
592 }
593 if (device == nullptr) {
594 ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
595 device = availableDevices.getDevice(
596 AUDIO_DEVICE_IN_STUB, String8(""), AUDIO_FORMAT_DEFAULT);
597 ALOGE_IF(device == nullptr,
598 "getDeviceForInputSource() no default device defined");
599 }
600
601 ALOGV_IF(device != nullptr,
602 "getDeviceForInputSource()input source %d, device %08x",
603 inputSource, device->type());
604 return device;
605 }
606
updateDeviceSelectionCache()607 void Engine::updateDeviceSelectionCache()
608 {
609 for (const auto &iter : getProductStrategies()) {
610 const auto& strategy = iter.second;
611 auto devices = getDevicesForProductStrategy(strategy->getId());
612 mDevicesForStrategies[strategy->getId()] = devices;
613 strategy->setDeviceTypes(devices.types());
614 strategy->setDeviceAddress(devices.getFirstValidAddress().c_str());
615 }
616 }
617
getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const618 product_strategy_t Engine::getProductStrategyFromLegacy(legacy_strategy legacyStrategy) const {
619 for (const auto& strategyMap : mLegacyStrategyMap) {
620 if (strategyMap.second == legacyStrategy) {
621 return strategyMap.first;
622 }
623 }
624 return PRODUCT_STRATEGY_NONE;
625 }
626
getPreferredDeviceTypeForLegacyStrategy(const DeviceVector & availableOutputDevices,legacy_strategy legacyStrategy) const627 audio_devices_t Engine::getPreferredDeviceTypeForLegacyStrategy(
628 const DeviceVector& availableOutputDevices, legacy_strategy legacyStrategy) const {
629 product_strategy_t strategy = getProductStrategyFromLegacy(legacyStrategy);
630 DeviceVector devices = getPreferredAvailableDevicesForProductStrategy(
631 availableOutputDevices, strategy);
632 if (devices.size() > 0) {
633 return devices[0]->type();
634 }
635 return AUDIO_DEVICE_NONE;
636 }
637
getPreferredAvailableDevicesForProductStrategy(const DeviceVector & availableOutputDevices,product_strategy_t strategy) const638 DeviceVector Engine::getPreferredAvailableDevicesForProductStrategy(
639 const DeviceVector& availableOutputDevices, product_strategy_t strategy) const {
640 DeviceVector preferredAvailableDevVec = {};
641 AudioDeviceTypeAddrVector preferredStrategyDevices;
642 const status_t status = getDevicesForRoleAndStrategy(
643 strategy, DEVICE_ROLE_PREFERRED, preferredStrategyDevices);
644 if (status == NO_ERROR) {
645 // there is a preferred device, is it available?
646 preferredAvailableDevVec =
647 availableOutputDevices.getDevicesFromDeviceTypeAddrVec(preferredStrategyDevices);
648 if (preferredAvailableDevVec.size() == preferredAvailableDevVec.size()) {
649 ALOGVV("%s using pref device %s for strategy %u",
650 __func__, preferredAvailableDevVec.toString().c_str(), strategy);
651 return preferredAvailableDevVec;
652 }
653 }
654 return preferredAvailableDevVec;
655 }
656
657
getDevicesForProductStrategy(product_strategy_t strategy) const658 DeviceVector Engine::getDevicesForProductStrategy(product_strategy_t strategy) const {
659 const SwAudioOutputCollection& outputs = getApmObserver()->getOutputs();
660
661 // Take context into account to remap product strategy before
662 // checking preferred device for strategy and applying default routing rules
663 strategy = remapStrategyFromContext(strategy, outputs);
664
665 auto legacyStrategy = mLegacyStrategyMap.find(strategy) != end(mLegacyStrategyMap) ?
666 mLegacyStrategyMap.at(strategy) : STRATEGY_NONE;
667
668 DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
669
670 filterOutputDevicesForStrategy(legacyStrategy, availableOutputDevices, outputs);
671
672 // check if this strategy has a preferred device that is available,
673 // if yes, give priority to it.
674 DeviceVector preferredAvailableDevVec =
675 getPreferredAvailableDevicesForProductStrategy(availableOutputDevices, strategy);
676 if (!preferredAvailableDevVec.isEmpty()) {
677 return preferredAvailableDevVec;
678 }
679
680 return getDevicesForStrategyInt(legacyStrategy,
681 availableOutputDevices,
682 outputs);
683 }
684
getOutputDevicesForAttributes(const audio_attributes_t & attributes,const sp<DeviceDescriptor> & preferredDevice,bool fromCache) const685 DeviceVector Engine::getOutputDevicesForAttributes(const audio_attributes_t &attributes,
686 const sp<DeviceDescriptor> &preferredDevice,
687 bool fromCache) const
688 {
689 // First check for explict routing device
690 if (preferredDevice != nullptr) {
691 ALOGV("%s explicit Routing on device %s", __func__, preferredDevice->toString().c_str());
692 return DeviceVector(preferredDevice);
693 }
694 product_strategy_t strategy = getProductStrategyForAttributes(attributes);
695 const DeviceVector availableOutputDevices = getApmObserver()->getAvailableOutputDevices();
696 const SwAudioOutputCollection &outputs = getApmObserver()->getOutputs();
697 //
698 // @TODO: what is the priority of explicit routing? Shall it be considered first as it used to
699 // be by APM?
700 //
701 // Honor explicit routing requests only if all active clients have a preferred route in which
702 // case the last active client route is used
703 sp<DeviceDescriptor> device = findPreferredDevice(outputs, strategy, availableOutputDevices);
704 if (device != nullptr) {
705 return DeviceVector(device);
706 }
707
708 return fromCache? mDevicesForStrategies.at(strategy) : getDevicesForProductStrategy(strategy);
709 }
710
getOutputDevicesForStream(audio_stream_type_t stream,bool fromCache) const711 DeviceVector Engine::getOutputDevicesForStream(audio_stream_type_t stream, bool fromCache) const
712 {
713 auto attributes = getAttributesForStreamType(stream);
714 return getOutputDevicesForAttributes(attributes, nullptr, fromCache);
715 }
716
getInputDeviceForAttributes(const audio_attributes_t & attr,uid_t uid,sp<AudioPolicyMix> * mix) const717 sp<DeviceDescriptor> Engine::getInputDeviceForAttributes(const audio_attributes_t &attr,
718 uid_t uid,
719 sp<AudioPolicyMix> *mix) const
720 {
721 const auto &policyMixes = getApmObserver()->getAudioPolicyMixCollection();
722 const auto availableInputDevices = getApmObserver()->getAvailableInputDevices();
723 const auto &inputs = getApmObserver()->getInputs();
724 std::string address;
725
726 //
727 // Explicit Routing ??? what is the priority of explicit routing? Shall it be considered
728 // first as it used to be by APM?
729 //
730 // Honor explicit routing requests only if all active clients have a preferred route in which
731 // case the last active client route is used
732 sp<DeviceDescriptor> device =
733 findPreferredDevice(inputs, attr.source, availableInputDevices);
734 if (device != nullptr) {
735 return device;
736 }
737
738 device = policyMixes.getDeviceAndMixForInputSource(attr.source,
739 availableInputDevices,
740 uid,
741 mix);
742 if (device != nullptr) {
743 return device;
744 }
745
746 device = getDeviceForInputSource(attr.source);
747 if (device == nullptr || !audio_is_remote_submix_device(device->type())) {
748 // Return immediately if the device is null or it is not a remote submix device.
749 return device;
750 }
751
752 // For remote submix device, try to find the device by address.
753 address = "0";
754 std::size_t pos;
755 std::string tags { attr.tags };
756 if ((pos = tags.find("addr=")) != std::string::npos) {
757 address = tags.substr(pos + std::strlen("addr="));
758 }
759 return availableInputDevices.getDevice(device->type(),
760 String8(address.c_str()),
761 AUDIO_FORMAT_DEFAULT);
762 }
763
764 } // namespace audio_policy
765 } // namespace android
766
767
768