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

Commit e677ade8 authored by Jack He's avatar Jack He Committed by Automerger Merge Worker
Browse files

Merge changes from topic "broadcast_callback_fixes" am: 407f75ba

parents 603fa8f7 407f75ba
Loading
Loading
Loading
Loading
+21 −5
Original line number Diff line number Diff line
@@ -634,6 +634,12 @@ public class LeAudioService extends ProfileService {
            Log.w(TAG, "Native interface not available.");
            return;
        }
        if (!mBroadcastStateMap.containsKey(broadcastId)) {
            notifyBroadcastUpdateFailed(broadcastId,
                    BluetoothStatusCodes.ERROR_LE_BROADCAST_INVALID_BROADCAST_ID);
            return;
        }

        if (DBG) Log.d(TAG, "updateBroadcast");
        mLeAudioBroadcasterNativeInterface.updateMetadata(broadcastId, metadata.getRawMetadata());
    }
@@ -647,6 +653,12 @@ public class LeAudioService extends ProfileService {
            Log.w(TAG, "Native interface not available.");
            return;
        }
        if (!mBroadcastStateMap.containsKey(broadcastId)) {
            notifyOnBroadcastStopFailed(
                    BluetoothStatusCodes.ERROR_LE_BROADCAST_INVALID_BROADCAST_ID);
            return;
        }

        if (DBG) Log.d(TAG, "stopBroadcast");
        mLeAudioBroadcasterNativeInterface.stopBroadcast(broadcastId);
    }
@@ -660,6 +672,12 @@ public class LeAudioService extends ProfileService {
            Log.w(TAG, "Native interface not available.");
            return;
        }
        if (!mBroadcastStateMap.containsKey(broadcastId)) {
            notifyOnBroadcastStopFailed(
                    BluetoothStatusCodes.ERROR_LE_BROADCAST_INVALID_BROADCAST_ID);
            return;
        }

        if (DBG) Log.d(TAG, "destroyBroadcast");
        mLeAudioBroadcasterNativeInterface.destroyBroadcast(broadcastId);
    }
@@ -1141,6 +1159,9 @@ public class LeAudioService extends ProfileService {
            boolean success = stackEvent.valueBool1;
            if (success) {
                Log.d(TAG, "Broadcast broadcastId: " + broadcastId + " created.");
                notifyBroadcastStarted(broadcastId, BluetoothStatusCodes.REASON_LOCAL_APP_REQUEST);

                // Start sending the actual stream
                startBroadcast(broadcastId);
            } else {
                // TODO: Improve reason reporting or extend the native stack event with reason code
@@ -1204,11 +1225,6 @@ public class LeAudioService extends ProfileService {
            } else if (state == LeAudioStackEvent.BROADCAST_STATE_STREAMING) {
                if (DBG) Log.d(TAG, "Broadcast broadcastId: " + broadcastId + " streaming.");

                if (!mBroadcastsPlaybackMap.containsKey(broadcastId)) {
                    notifyBroadcastStarted(broadcastId,
                            BluetoothStatusCodes.REASON_LOCAL_APP_REQUEST);
                }

                // Stream resumed
                mBroadcastsPlaybackMap.put(broadcastId, true);
                notifyPlaybackStarted(broadcastId, BluetoothStatusCodes.REASON_LOCAL_STACK_REQUEST);
+148 −31
Original line number Diff line number Diff line
@@ -89,6 +89,65 @@ public class LeAudioBroadcastServiceTest {
    // German language code in ISO 639-3
    private static final String TEST_LANGUAGE = "deu";

    private boolean mOnBroadcastStartedCalled = false;
    private boolean mOnBroadcastStartFailedCalled = false;
    private boolean mOnBroadcastStoppedCalled = false;
    private boolean mOnBroadcastStopFailedCalled = false;
    private boolean mOnPlaybackStartedCalled = false;
    private boolean mOnPlaybackStoppedCalled = false;
    private boolean mOnBroadcastUpdatedCalled = false;
    private boolean mOnBroadcastUpdateFailedCalled = false;
    private boolean mOnBroadcastMetadataChangedCalled = false;

    private final IBluetoothLeBroadcastCallback mCallbacks =
            new IBluetoothLeBroadcastCallback.Stub() {
        @Override
        public void onBroadcastStarted(int reason, int broadcastId) {
            mOnBroadcastStartedCalled = true;
        }

        @Override
        public void onBroadcastStartFailed(int reason) {
            mOnBroadcastStartFailedCalled = true;
        }

        @Override
        public void onBroadcastStopped(int reason, int broadcastId) {
            mOnBroadcastStoppedCalled = true;
        }

        @Override
        public void onBroadcastStopFailed(int reason) {
            mOnBroadcastStopFailedCalled = true;
        }

        @Override
        public void onPlaybackStarted(int reason, int broadcastId) {
            mOnPlaybackStartedCalled = true;
        }

        @Override
        public void onPlaybackStopped(int reason, int broadcastId) {
            mOnPlaybackStoppedCalled = true;
        }

        @Override
        public void onBroadcastUpdated(int reason, int broadcastId) {
            mOnBroadcastUpdatedCalled = true;
        }

        @Override
        public void onBroadcastUpdateFailed(int reason, int broadcastId) {
            mOnBroadcastUpdateFailedCalled = true;
        }

        @Override
        public void onBroadcastMetadataChanged(int broadcastId,
                BluetoothLeBroadcastMetadata metadata) {
            mOnBroadcastMetadataChangedCalled = true;
        }
    };

    @Before
    public void setUp() throws Exception {
        mTargetContext = InstrumentationRegistry.getTargetContext();
@@ -103,6 +162,7 @@ public class LeAudioBroadcastServiceTest {
        TestUtils.setAdapterService(mAdapterService);
        doReturn(mDatabaseManager).when(mAdapterService).getDatabase();
        doReturn(true, false).when(mAdapterService).isStartedProfile(anyString());
        doReturn(true).when(mAdapterService).isLeAudioBroadcastSourceSupported();

        mAdapter = BluetoothAdapter.getDefaultAdapter();

@@ -166,17 +226,8 @@ public class LeAudioBroadcastServiceTest {
        });
    }

    @Test
    public void testCreateBroadcastNative() {
        int broadcastId = 243;
        int broadcast_profile = 0;
        byte[] code = {0x00, 0x01, 0x00};

        BluetoothLeAudioContentMetadata.Builder meta_builder =
                new BluetoothLeAudioContentMetadata.Builder();
        meta_builder.setLanguage("deu");
        meta_builder.setProgramInfo("Public broadcast info");
        BluetoothLeAudioContentMetadata meta = meta_builder.build();
    void verifyBroadcastStarted(int broadcastId, byte[] code,
            BluetoothLeAudioContentMetadata meta) {
        mService.createBroadcast(meta, code);

        verify(mNativeInterface, times(1)).createBroadcast(eq(meta.getRawMetadata()), eq(1),
@@ -188,6 +239,8 @@ public class LeAudioBroadcastServiceTest {
        create_event.valueInt1 = broadcastId;
        create_event.valueBool1 = true;
        mService.messageFromNative(create_event);

        // Verify if broadcast is auto-started on start
        verify(mNativeInterface, times(1)).startBroadcast(eq(broadcastId));

        // Notify initial paused state
@@ -205,43 +258,108 @@ public class LeAudioBroadcastServiceTest {

        // Check if metadata is requested when the broadcast starts to stream
        verify(mNativeInterface, times(1)).getBroadcastMetadata(eq(broadcastId));
        Assert.assertFalse(mOnBroadcastStartFailedCalled);
        Assert.assertTrue(mOnBroadcastStartedCalled);
    }

    void verifyBroadcastStopped(int broadcastId) {
        mService.stopBroadcast(broadcastId);
        verify(mNativeInterface, times(1)).stopBroadcast(eq(broadcastId));

        LeAudioStackEvent state_event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_BROADCAST_STATE);
        state_event.valueInt1 = broadcastId;
        state_event.valueInt2 = LeAudioStackEvent.BROADCAST_STATE_STOPPED;
        mService.messageFromNative(state_event);

        // Verify if broadcast is auto-destroyed on stop
        verify(mNativeInterface, times(1)).destroyBroadcast(eq(broadcastId));

        state_event = new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_BROADCAST_DESTROYED);
        state_event.valueInt1 = broadcastId;
        mService.messageFromNative(state_event);

        Assert.assertTrue(mOnBroadcastStoppedCalled);
        Assert.assertFalse(mOnBroadcastStopFailedCalled);
    }

    @Test
    public void testStartStopBroadcastNative() {
        int broadcast_profile = 0;
    public void testCreateBroadcastNative() {
        int broadcastId = 243;
        byte[] code = {0x00, 0x01, 0x00};

        mService.mBroadcastCallbacks.register(mCallbacks);

        BluetoothLeAudioContentMetadata.Builder meta_builder =
                new BluetoothLeAudioContentMetadata.Builder();
        meta_builder.setLanguage("eng");
        meta_builder.setLanguage("deu");
        meta_builder.setProgramInfo("Public broadcast info");

        verifyBroadcastStarted(broadcastId, code, meta_builder.build());
    }

    @Test
    public void testCreateBroadcastNativeFailed() {
        int broadcastId = 243;
        byte[] code = {0x00, 0x01, 0x00};

        mService.mBroadcastCallbacks.register(mCallbacks);

        BluetoothLeAudioContentMetadata.Builder meta_builder =
                new BluetoothLeAudioContentMetadata.Builder();
        meta_builder.setLanguage("deu");
        meta_builder.setProgramInfo("Public broadcast info");
        BluetoothLeAudioContentMetadata meta = meta_builder.build();
        mService.createBroadcast(meta, code);

        int instance_id = 243;
        mService.startBroadcast(instance_id);
        verify(mNativeInterface, times(1)).startBroadcast(eq(instance_id));
        verify(mNativeInterface, times(1)).createBroadcast(eq(meta.getRawMetadata()), eq(1),
                eq(code));

        LeAudioStackEvent create_event =
                new LeAudioStackEvent(LeAudioStackEvent.EVENT_TYPE_BROADCAST_CREATED);
        create_event.valueInt1 = broadcastId;
        create_event.valueBool1 = false;
        mService.messageFromNative(create_event);

        mService.stopBroadcast(instance_id);
        verify(mNativeInterface, times(1)).stopBroadcast(eq(instance_id));
        Assert.assertFalse(mOnBroadcastStartedCalled);
        Assert.assertTrue(mOnBroadcastStartFailedCalled);
    }

    @Test
    public void testDestroyBroadcastNative() {
        int broadcast_profile = 0;
    public void testStartStopBroadcastNative() {
        int broadcastId = 243;
        byte[] code = {0x00, 0x01, 0x00};

        mService.mBroadcastCallbacks.register(mCallbacks);

        BluetoothLeAudioContentMetadata.Builder meta_builder =
        new BluetoothLeAudioContentMetadata.Builder();
        meta_builder.setLanguage("ENG");
        meta_builder.setLanguage("eng");
        meta_builder.setProgramInfo("Public broadcast info");
        BluetoothLeAudioContentMetadata meta = meta_builder.build();
        mService.createBroadcast(meta, code);

        int broadcast_id = 243;
        mService.destroyBroadcast(broadcast_id);
        verify(mNativeInterface, times(1)).destroyBroadcast(eq(broadcast_id));
        verifyBroadcastStarted(broadcastId, code, meta_builder.build());
        verifyBroadcastStopped(broadcastId);
    }

    @Test
    public void testBroadcastInvalidBroadcastIdRequest() {
        int broadcastId = 243;

        mService.mBroadcastCallbacks.register(mCallbacks);

        // Stop non-existing broadcast
        mService.stopBroadcast(broadcastId);
        Assert.assertFalse(mOnBroadcastStoppedCalled);
        Assert.assertTrue(mOnBroadcastStopFailedCalled);

        // Update metadata for non-existing broadcast
        BluetoothLeAudioContentMetadata.Builder meta_builder =
        new BluetoothLeAudioContentMetadata.Builder();
        meta_builder.setLanguage("eng");
        meta_builder.setProgramInfo("Public broadcast info");
        mService.updateBroadcast(broadcastId, meta_builder.build());
        Assert.assertFalse(mOnBroadcastUpdatedCalled);
        Assert.assertTrue(mOnBroadcastUpdateFailedCalled);
    }

    private BluetoothLeBroadcastSubgroup createBroadcastSubgroup() {
@@ -291,7 +409,6 @@ public class LeAudioBroadcastServiceTest {
    @Test
    public void testGetAllBroadcastMetadata() {
        int broadcastId = 243;
        int broadcast_profile = 0;
        byte[] code = {0x00, 0x01, 0x00};

        BluetoothLeAudioContentMetadata.Builder meta_builder =