Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 88ee224b authored by Yiyi Shen's avatar Yiyi Shen
Browse files

[MR2] Remove binder call from getSessionReleaseType

AudioManagerRouteController#getSessionReleaseType can be called on binder threads. Therefore, we should not call other classes which could incur in cross process communication. This prevents binder thread exhaustion by ensuring the current binder thread cannot block on a process waiting for binder threads.

Test: atest com.android.server.media.AudioManagerRouteControllerTest
Flag: com.android.media.flags.enable_output_switcher_personal_audio_sharing
Bug: 434734395

Change-Id: Ifff7f0c5fecfeb69e48fc2fa1e7043e90c2b2508
parent bdcd8751
Loading
Loading
Loading
Loading
+10 −4
Original line number Diff line number Diff line
@@ -129,6 +129,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
    @GuardedBy("this")
    private List<MediaRoute2Info> mSelectableRoutes = Collections.emptyList();

    @GuardedBy("this")
    private @RoutingSessionInfo.ReleaseType int mSessionReleaseType =
            RoutingSessionInfo.RELEASE_UNSUPPORTED;

    // A singleton AudioManagerRouteController.
    private static AudioManagerRouteController mInstance;

@@ -265,10 +269,8 @@ import java.util.concurrent.CopyOnWriteArrayList;
    }

    @Override
    public @RoutingSessionInfo.ReleaseType int getSessionReleaseType() {
        return currentOutputIsBLEBroadcast()
                ? RoutingSessionInfo.RELEASE_TYPE_SHARING
                : RoutingSessionInfo.RELEASE_UNSUPPORTED;
    public synchronized @RoutingSessionInfo.ReleaseType int getSessionReleaseType() {
        return mSessionReleaseType;
    }

    @Override
@@ -714,6 +716,10 @@ import java.util.concurrent.CopyOnWriteArrayList;
                                    .toList();
                }
            }
            mSessionReleaseType =
                    currentOutputIsBLEBroadcast
                            ? RoutingSessionInfo.RELEASE_TYPE_SHARING
                            : RoutingSessionInfo.RELEASE_UNSUPPORTED;
        }
    }

+9 −27
Original line number Diff line number Diff line
@@ -482,33 +482,9 @@ public class AudioManagerRouteControllerTest {
                .isEqualTo(FAKE_AUDIO_DEVICE_INFO_WIRED_HEADSET.getProductName().toString());
    }

    @Test
    public void getSessionReleaseType_returnTypeSharing() {
        setUpControllerUnderTest(/* useMockBluetoothDeviceRoutesManager= */ false);
        when(mMockAudioManager.getDevicesForAttributes(ATTRIBUTES_MEDIA))
                .thenReturn(
                        List.of(
                                createAudioDeviceAttribute(
                                        AudioDeviceInfo.TYPE_BLE_BROADCAST, /* address= */ "")));
        assertThat(mControllerUnderTest.getSessionReleaseType())
                .isEqualTo(RoutingSessionInfo.RELEASE_TYPE_SHARING);
    }

    @Test
    public void getSessionReleaseType_returnTypeUnsupported() {
        setUpControllerUnderTest(/* useMockBluetoothDeviceRoutesManager= */ false);
        when(mMockAudioManager.getDevicesForAttributes(ATTRIBUTES_MEDIA))
                .thenReturn(
                        List.of(
                                createAudioDeviceAttribute(
                                        AudioDeviceInfo.TYPE_WIRED_HEADSET, /* address= */ "")));
        assertThat(mControllerUnderTest.getSessionReleaseType())
                .isEqualTo(RoutingSessionInfo.RELEASE_UNSUPPORTED);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_OUTPUT_SWITCHER_PERSONAL_AUDIO_SHARING)
    public void getRoutes_whenLEAudioBroadcastNotSupported_returnsCorrectList() {
    public void getRoutes_whenLEAudioBroadcastNotSupported_returnsCorrectStates() {
        setUpControllerAndLEAudioMocks();
        when(mMockBluetoothDeviceRoutesManager.isLEAudioBroadcastSupported()).thenReturn(false);
        addAvailableAudioDeviceInfo(
@@ -532,11 +508,13 @@ public class AudioManagerRouteControllerTest {
        assertThat(availableRoutesNames).contains(FAKE_AUDIO_DEVICE_LE_HEADSET_2_NAME);
        assertThat(selectedRoutesNames).containsExactly(FAKE_AUDIO_DEVICE_LE_HEADSET_1_NAME);
        assertThat(deselectableRoutes).isEmpty();
        assertThat(mControllerUnderTest.getSessionReleaseType())
                .isEqualTo(RoutingSessionInfo.RELEASE_UNSUPPORTED);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_OUTPUT_SWITCHER_PERSONAL_AUDIO_SHARING)
    public void getRoutes_singleDeviceSelectedAndOutputNotBroadcast_returnsCorrectList() {
    public void getRoutes_singleDeviceSelectedAndOutputNotBroadcast_returnsCorrectStates() {
        setUpControllerAndLEAudioMocks();
        when(mMockBluetoothDeviceRoutesManager.isLEAudioBroadcastSupported()).thenReturn(true);
        addAvailableAudioDeviceInfo(
@@ -563,11 +541,13 @@ public class AudioManagerRouteControllerTest {
        assertThat(availableRoutesNames).contains(FAKE_AUDIO_DEVICE_LE_HEADSET_2_NAME);
        assertThat(selectedRoutesNames).containsExactly(FAKE_AUDIO_DEVICE_LE_HEADSET_1_NAME);
        assertThat(deselectableRoutes).isEmpty();
        assertThat(mControllerUnderTest.getSessionReleaseType())
                .isEqualTo(RoutingSessionInfo.RELEASE_UNSUPPORTED);
    }

    @Test
    @EnableFlags(Flags.FLAG_ENABLE_OUTPUT_SWITCHER_PERSONAL_AUDIO_SHARING)
    public void getRoutes_singleDeviceSelectedAndOutputIsBroadcast_returnsCorrectList() {
    public void getRoutes_singleDeviceSelectedAndOutputIsBroadcast_returnsCorrectStates() {
        setUpControllerAndLEAudioMocks();
        when(mMockBluetoothDeviceRoutesManager.isLEAudioBroadcastSupported()).thenReturn(true);
        when(mMockBluetoothDeviceRoutesManager.getBroadcastingDeviceRoutes())
@@ -602,6 +582,8 @@ public class AudioManagerRouteControllerTest {
        assertThat(availableRoutesNames).contains(FAKE_AUDIO_DEVICE_LE_HEADSET_2_NAME);
        assertThat(selectedRoutesNames).containsExactly(FAKE_AUDIO_DEVICE_LE_HEADSET_1_NAME);
        assertThat(deselectableRoutesNames).containsExactly(FAKE_AUDIO_DEVICE_LE_HEADSET_1_NAME);
        assertThat(mControllerUnderTest.getSessionReleaseType())
                .isEqualTo(RoutingSessionInfo.RELEASE_TYPE_SHARING);
    }

    // Internal methods.