Search in sources :

Example 11 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerListenerTest method onTimelineChanged_emptyMediaItemAndMediaMetadata_whenCommandUnavailableFromSession.

@Test
public void onTimelineChanged_emptyMediaItemAndMediaMetadata_whenCommandUnavailableFromSession() throws Exception {
    int testMediaItemsSize = 2;
    List<MediaItem> testMediaItemList = MediaTestUtils.createMediaItems(testMediaItemsSize);
    Timeline testTimeline = new PlaylistTimeline(testMediaItemList);
    Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setTimeline(testTimeline).build();
    remoteSession.setPlayer(playerConfig);
    MediaController controller = controllerTestRule.createController(remoteSession.getToken());
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<Timeline> timelineFromParamRef = new AtomicReference<>();
    AtomicReference<Timeline> timelineFromGetterRef = new AtomicReference<>();
    AtomicReference<MediaMetadata> metadataFromGetterRef = new AtomicReference<>();
    AtomicReference<MediaItem> currentMediaItemGetterRef = new AtomicReference<>();
    Player.Listener listener = new Player.Listener() {

        @Override
        public void onTimelineChanged(Timeline timeline, int reason) {
            timelineFromParamRef.set(timeline);
            timelineFromGetterRef.set(controller.getCurrentTimeline());
            metadataFromGetterRef.set(controller.getMediaMetadata());
            currentMediaItemGetterRef.set(controller.getCurrentMediaItem());
            latch.countDown();
        }
    };
    controller.addListener(listener);
    Commands commandsWithoutGetTimeline = createPlayerCommandsWithout(Player.COMMAND_GET_TIMELINE);
    remoteSession.setAvailableCommands(SessionCommands.EMPTY, commandsWithoutGetTimeline);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(timelineFromParamRef.get().getWindowCount()).isEqualTo(testMediaItemsSize);
    for (int i = 0; i < timelineFromParamRef.get().getWindowCount(); i++) {
        assertThat(timelineFromParamRef.get().getWindow(/* windowIndex= */
        i, new Timeline.Window()).mediaItem).isEqualTo(MediaItem.EMPTY);
    }
    assertThat(timelineFromGetterRef.get().getWindowCount()).isEqualTo(testMediaItemsSize);
    for (int i = 0; i < timelineFromGetterRef.get().getWindowCount(); i++) {
        assertThat(timelineFromGetterRef.get().getWindow(/* windowIndex= */
        i, new Timeline.Window()).mediaItem).isEqualTo(MediaItem.EMPTY);
    }
    assertThat(metadataFromGetterRef.get()).isEqualTo(MediaMetadata.EMPTY);
    assertThat(currentMediaItemGetterRef.get()).isEqualTo(MediaItem.EMPTY);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) Bundle(android.os.Bundle) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) MediaTestUtils.createTimeline(androidx.media3.session.MediaTestUtils.createTimeline) Timeline(androidx.media3.common.Timeline) MediaItem(androidx.media3.common.MediaItem) Commands(androidx.media3.common.Player.Commands) MediaMetadata(androidx.media3.common.MediaMetadata) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 12 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method onCommandRequest.

@Test
public void onCommandRequest() throws Exception {
    ArrayList<Integer> commands = new ArrayList<>();
    CountDownLatch latchForPause = new CountDownLatch(1);
    SessionCallback callback = new TestSessionCallback() {

        @Override
        public int onPlayerCommandRequest(MediaSession session, ControllerInfo controllerInfo, @Player.Command int command) {
            assertThat(controllerInfo.isTrusted()).isFalse();
            commands.add(command);
            if (command == COMMAND_PLAY_PAUSE) {
                latchForPause.countDown();
                return RESULT_ERROR_INVALID_STATE;
            }
            return RESULT_SUCCESS;
        }
    };
    session = new MediaSession.Builder(context, player).setId("onPlayerCommandRequest").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().pause();
    assertThat(latchForPause.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
    assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
    assertThat(commands).hasSize(1);
    assertThat(commands.get(0)).isEqualTo(COMMAND_PLAY_PAUSE);
    controller.getTransportControls().prepare();
    player.awaitMethodCalled(MockPlayer.METHOD_PREPARE, TIMEOUT_MS);
    assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PAUSE)).isFalse();
    assertThat(commands).hasSize(2);
    assertThat(commands.get(1)).isEqualTo(COMMAND_PREPARE);
}
Also used : ArrayList(java.util.ArrayList) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 13 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method controllerCallback_sessionRejects.

@Test
public void controllerCallback_sessionRejects() throws Exception {
    SessionCallback sessionCallback = new SessionCallback() {

        @Override
        public MediaSession.ConnectionResult onConnect(MediaSession session, ControllerInfo controller) {
            return MediaSession.ConnectionResult.reject();
        }
    };
    session = new MediaSession.Builder(context, player).setId("controllerCallback_sessionRejects").setSessionCallback(sessionCallback).build();
    // Session will not accept the controller's commands.
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.getTransportControls().play();
    Thread.sleep(NO_RESPONSE_TIMEOUT_MS);
    assertThat(player.hasMethodBeenCalled(MockPlayer.METHOD_PLAY)).isFalse();
}
Also used : SessionCallback(androidx.media3.session.MediaSession.SessionCallback) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 14 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaSessionPermissionTest method changingPermissionForSessionCommandWithSetAvailableCommands.

@Test
public void changingPermissionForSessionCommandWithSetAvailableCommands() throws Exception {
    String mediaId = "testSetRating";
    Rating rating = new StarRating(5, 3.5f);
    createSessionWithAvailableCommands(createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_RATING)), Player.Commands.EMPTY);
    RemoteMediaController controller = controllerTestRule.createRemoteController(session.getToken());
    controller.setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
    assertThat(callback.onSetRatingCalled).isTrue();
    callback.reset();
    // Change allowed commands.
    session.setAvailableCommands(getTestControllerInfo(), createSessionCommandsWith(new SessionCommand(COMMAND_CODE_SESSION_SET_MEDIA_URI)), Player.Commands.EMPTY);
    controller.setRating(mediaId, rating);
    assertThat(callback.countDownLatch.await(NO_RESPONSE_TIMEOUT_MS, MILLISECONDS)).isFalse();
}
Also used : Rating(androidx.media3.common.Rating) StarRating(androidx.media3.common.StarRating) StarRating(androidx.media3.common.StarRating) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 15 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaSessionCallbackWithMediaControllerCompatTest method sendCommand.

@Test
public void sendCommand() throws Exception {
    // TODO(jaewan): Need to revisit with the permission.
    String testCommand = "test_command";
    Bundle testArgs = new Bundle();
    testArgs.putString("args", "test_args");
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallback callback = new SessionCallback() {

        @Override
        public MediaSession.ConnectionResult onConnect(MediaSession session, ControllerInfo controller) {
            if (EXPECTED_CONTROLLER_PACKAGE_NAME.equals(controller.getPackageName())) {
                MediaSession.ConnectionResult commands = SessionCallback.super.onConnect(session, controller);
                SessionCommands.Builder builder = commands.availableSessionCommands.buildUpon();
                builder.add(new SessionCommand(testCommand, /* extras= */
                Bundle.EMPTY));
                return MediaSession.ConnectionResult.accept(/* availableSessionCommands= */
                builder.build(), commands.availablePlayerCommands);
            } else {
                return MediaSession.ConnectionResult.reject();
            }
        }

        @Override
        public ListenableFuture<SessionResult> onCustomCommand(MediaSession session, ControllerInfo controller, SessionCommand sessionCommand, Bundle args) {
            assertThat(sessionCommand.customAction).isEqualTo(testCommand);
            assertThat(TestUtils.equals(testArgs, args)).isTrue();
            latch.countDown();
            return Futures.immediateFuture(new SessionResult(RESULT_SUCCESS));
        }
    };
    session = new MediaSession.Builder(context, player).setId("sendCommand").setSessionCallback(callback).build();
    controller = new RemoteMediaControllerCompat(context, session.getSessionCompat().getSessionToken(), /* waitForConnection= */
    true);
    controller.sendCommand(testCommand, testArgs, /* cb= */
    null);
    assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
}
Also used : Bundle(android.os.Bundle) SessionCallback(androidx.media3.session.MediaSession.SessionCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

Commands (androidx.media3.common.Player.Commands)10 LargeTest (androidx.test.filters.LargeTest)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 Bundle (android.os.Bundle)5 Player (androidx.media3.common.Player)5 ControllerInfo (androidx.media3.session.MediaSession.ControllerInfo)5 RemoteMockPlayer (androidx.media3.session.RemoteMediaSession.RemoteMockPlayer)5 MediaItem (androidx.media3.common.MediaItem)4 MediaMetadata (androidx.media3.common.MediaMetadata)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Nullable (androidx.annotation.Nullable)3 SessionCallback (androidx.media3.session.MediaSession.SessionCallback)3 ArrayList (java.util.ArrayList)3 Timeline (androidx.media3.common.Timeline)2 MediaTestUtils.createTimeline (androidx.media3.session.MediaTestUtils.createTimeline)2 Notification (android.app.Notification)1 Bitmap (android.graphics.Bitmap)1 IBinder (android.os.IBinder)1 RemoteException (android.os.RemoteException)1