From 50ecfcba00f726a1aa55038c6dab13eed2bbc61f Mon Sep 17 00:00:00 2001 From: Eric Laurent Date: Mon, 11 Oct 2021 21:23:23 +0200 Subject: [PATCH] AudioDeviceBroker: fix short music mute when connecting a wired headset When a wired headset is connected while an A2DP headset is in use, the device switcher logic will disconnect the A2DP headset from the audio framework. This will cause a short music interruption just after it transfers to the wired headset. The fix consists in not muting music when disconnecting an A2DP headset and a wired headset is already active. Bug: 166744974 Test: repro steps in bug Change-Id: Id3143e185ffbe017ef76f4a0bda7fcfb1d557e52 --- .../server/audio/AudioDeviceBroker.java | 25 ++++++++++++++++--- .../server/audio/AudioDeviceInventory.java | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/audio/AudioDeviceBroker.java b/services/core/java/com/android/server/audio/AudioDeviceBroker.java index 84a3060a7f911..647d6c640852b 100644 --- a/services/core/java/com/android/server/audio/AudioDeviceBroker.java +++ b/services/core/java/com/android/server/audio/AudioDeviceBroker.java @@ -1719,19 +1719,36 @@ import java.util.concurrent.atomic.AtomicBoolean; MESSAGES_MUTE_MUSIC.add(MSG_L_A2DP_DEVICE_CONNECTION_CHANGE_EXT_CONNECTION); MESSAGES_MUTE_MUSIC.add(MSG_L_A2DP_DEVICE_CONNECTION_CHANGE_EXT_DISCONNECTION); MESSAGES_MUTE_MUSIC.add(MSG_IIL_SET_FORCE_BT_A2DP_USE); - MESSAGES_MUTE_MUSIC.add(MSG_REPORT_NEW_ROUTES_A2DP); } private AtomicBoolean mMusicMuted = new AtomicBoolean(false); + boolean messageMutesMusic(int message) { + if (message == 0) { + return false; + } + // Do not mute if we are disconnecting an A2DP device and music is playing + // on a wired headset. + if ((message == MSG_IL_SET_A2DP_SINK_CONNECTION_STATE_DISCONNECTED + || message == MSG_L_A2DP_DEVICE_CONNECTION_CHANGE_EXT_DISCONNECTION) + && AudioSystem.isStreamActive(AudioSystem.STREAM_MUSIC, 0) + && mDeviceInventory.DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET.contains( + mAudioService.getDevicesForStream(AudioSystem.STREAM_MUSIC))) { + return false; + } + return true; + } + /** Mutes or unmutes music according to pending A2DP messages */ private void checkMessagesMuteMusic(int message) { - boolean mute = message != 0; + boolean mute = messageMutesMusic(message); if (!mute) { for (int msg : MESSAGES_MUTE_MUSIC) { if (mBrokerHandler.hasMessages(msg)) { - mute = true; - break; + if (messageMutesMusic(msg)) { + mute = true; + break; + } } } } diff --git a/services/core/java/com/android/server/audio/AudioDeviceInventory.java b/services/core/java/com/android/server/audio/AudioDeviceInventory.java index 64e620eeb8a0b..bbbf45571189f 100644 --- a/services/core/java/com/android/server/audio/AudioDeviceInventory.java +++ b/services/core/java/com/android/server/audio/AudioDeviceInventory.java @@ -581,7 +581,7 @@ public class AudioDeviceInventory { mDeviceBroker.postObserveDevicesForAllStreams(); } - private static final Set DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET; + /* package */ static final Set DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET; static { DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET = new HashSet<>(); DEVICE_OVERRIDE_A2DP_ROUTE_ON_PLUG_SET.add(AudioSystem.DEVICE_OUT_WIRED_HEADSET);