Search in sources :

Example 1 with SessionCommandGroup

use of androidx.media2.session.SessionCommandGroup in project ExoPlayer by google.

the class SessionCallback method onConnect.

@Override
@Nullable
public SessionCommandGroup onConnect(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
    sessions.add(session);
    if (!allowedCommandProvider.acceptConnection(session, controllerInfo)) {
        return null;
    }
    SessionCommandGroup baseAllowedCommands = buildAllowedCommands(session, controllerInfo);
    return allowedCommandProvider.getAllowedCommands(session, controllerInfo, baseAllowedCommands);
}
Also used : SessionCommandGroup(androidx.media2.session.SessionCommandGroup) Nullable(androidx.annotation.Nullable)

Example 2 with SessionCommandGroup

use of androidx.media2.session.SessionCommandGroup in project ExoPlayer by google.

the class SessionCallback method buildAllowedCommands.

private SessionCommandGroup buildAllowedCommands(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
    SessionCommandGroup.Builder build;
    @Nullable SessionCommandGroup commands = (customCommandProvider != null) ? customCommandProvider.getCustomCommands(session, controllerInfo) : null;
    if (commands != null) {
        build = new SessionCommandGroup.Builder(commands);
    } else {
        build = new SessionCommandGroup.Builder();
    }
    build.addAllPredefinedCommands(SessionCommand.COMMAND_VERSION_1);
    build.addCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_MOVE_PLAYLIST_ITEM));
    // TODO(internal b/142848015): Use removeCommand(int) when it's added.
    if (mediaItemProvider == null) {
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SET_MEDIA_ITEM));
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SET_PLAYLIST));
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_ADD_PLAYLIST_ITEM));
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_REPLACE_PLAYLIST_ITEM));
    }
    if (ratingCallback == null) {
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SET_RATING));
    }
    if (skipCallback == null) {
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SKIP_BACKWARD));
        build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_SKIP_FORWARD));
    }
    // Check whether the session has unexpectedly changed the player.
    if (session.getPlayer() instanceof SessionPlayerConnector) {
        SessionPlayerConnector sessionPlayerConnector = (SessionPlayerConnector) session.getPlayer();
        // Check whether skipTo* works.
        if (!sessionPlayerConnector.canSkipToPlaylistItem()) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM));
        }
        if (!sessionPlayerConnector.canSkipToPreviousPlaylistItem()) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM));
        }
        if (!sessionPlayerConnector.canSkipToNextPlaylistItem()) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM));
        }
        // Check whether seekTo/rewind/fastForward works.
        if (!sessionPlayerConnector.isCurrentMediaItemSeekable()) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SEEK_TO));
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD));
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_REWIND));
        } else {
            if (fastForwardMs <= 0) {
                build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD));
            }
            if (rewindMs <= 0) {
                build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_REWIND));
            }
        }
    } else {
        if (!loggedUnexpectedSessionPlayerWarning) {
            // This can happen if MediaSession#updatePlayer() is called.
            Log.e(TAG, "SessionPlayer isn't a SessionPlayerConnector. Guess the allowed command.");
            loggedUnexpectedSessionPlayerWarning = true;
        }
        if (fastForwardMs <= 0) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_FAST_FORWARD));
        }
        if (rewindMs <= 0) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_SESSION_REWIND));
        }
        @Nullable List<MediaItem> playlist = sessionPlayer.getPlaylist();
        if (playlist == null) {
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM));
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM));
            build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM));
        } else {
            if (playlist.isEmpty() && (sessionPlayer.getRepeatMode() == SessionPlayer.REPEAT_MODE_NONE || sessionPlayer.getRepeatMode() == SessionPlayer.REPEAT_MODE_ONE)) {
                build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM));
            }
            if (playlist.size() == sessionPlayer.getCurrentMediaItemIndex() + 1 && (sessionPlayer.getRepeatMode() == SessionPlayer.REPEAT_MODE_NONE || sessionPlayer.getRepeatMode() == SessionPlayer.REPEAT_MODE_ONE)) {
                build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM));
            }
            if (playlist.size() <= 1) {
                build.removeCommand(new SessionCommand(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM));
            }
        }
    }
    return build.build();
}
Also used : SessionCommandGroup(androidx.media2.session.SessionCommandGroup) MediaItem(androidx.media2.common.MediaItem) SessionCommand(androidx.media2.session.SessionCommand) Nullable(androidx.annotation.Nullable)

Example 3 with SessionCommandGroup

use of androidx.media2.session.SessionCommandGroup in project ExoPlayer by google.

the class SessionCallbackBuilderTest method createConnectedController.

private MediaController createConnectedController(MediaSession session, OnConnectedListener onConnectedListener, OnAllowedCommandsChangedListener onAllowedCommandsChangedListener) throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    MediaController.ControllerCallback callback = new MediaController.ControllerCallback() {

        @Override
        public void onAllowedCommandsChanged(MediaController controller, SessionCommandGroup commands) {
            if (onAllowedCommandsChangedListener != null) {
                onAllowedCommandsChangedListener.onAllowedCommandsChanged(controller, commands);
            }
        }

        @Override
        public void onConnected(MediaController controller, SessionCommandGroup allowedCommands) {
            if (onConnectedListener != null) {
                onConnectedListener.onConnected(controller, allowedCommands);
            }
            latch.countDown();
        }
    };
    MediaController controller = new MediaController.Builder(context).setSessionToken(session.getToken()).setControllerCallback(ContextCompat.getMainExecutor(context), callback).build();
    latch.await();
    return controller;
}
Also used : MediaController(androidx.media2.session.MediaController) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with SessionCommandGroup

use of androidx.media2.session.SessionCommandGroup in project ExoPlayer by google.

the class SessionCallbackBuilderTest method setCustomCommandProvider_withCustomCommandProvider_receivesCustomCommand.

@Test
public void setCustomCommandProvider_withCustomCommandProvider_receivesCustomCommand() throws Exception {
    SessionCommand testCommand = new SessionCommand("exo.ext.media2.COMMAND", null);
    CountDownLatch latch = new CountDownLatch(1);
    SessionCallbackBuilder.CustomCommandProvider provider = new SessionCallbackBuilder.CustomCommandProvider() {

        @Override
        public SessionResult onCustomCommand(MediaSession session, MediaSession.ControllerInfo controllerInfo, SessionCommand customCommand, @Nullable Bundle args) {
            assertThat(customCommand.getCustomAction()).isEqualTo(testCommand.getCustomAction());
            assertThat(args).isNull();
            latch.countDown();
            return new SessionResult(SessionResult.RESULT_SUCCESS, null);
        }

        @Override
        public SessionCommandGroup getCustomCommands(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
            return new SessionCommandGroup.Builder().addCommand(testCommand).build();
        }
    };
    try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setCustomCommandProvider(provider).build())) {
        OnAllowedCommandsChangedListener listener = (controller, allowedCommands) -> {
            boolean foundCustomCommand = false;
            for (SessionCommand command : allowedCommands.getCommands()) {
                if (TextUtils.equals(testCommand.getCustomAction(), command.getCustomAction())) {
                    foundCustomCommand = true;
                    break;
                }
            }
            assertThat(foundCustomCommand).isTrue();
        };
        try (MediaController controller = createConnectedController(session, null, listener)) {
            assertSessionResultSuccess(controller.sendCustomCommand(testCommand, null), CONTROLLER_COMMAND_WAIT_TIME_MS);
            assertThat(latch.await(0, MILLISECONDS)).isTrue();
        }
    }
}
Also used : Context(android.content.Context) HeartRating(androidx.media2.session.HeartRating) Arrays(java.util.Arrays) Bundle(android.os.Bundle) Uri(android.net.Uri) MediaSession(androidx.media2.session.MediaSession) RunWith(org.junit.runner.RunWith) SessionResult(androidx.media2.session.SessionResult) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestUtils.assertPlayerResultSuccess(com.google.android.exoplayer2.ext.media2.TestUtils.assertPlayerResultSuccess) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SessionCommand(androidx.media2.session.SessionCommand) ContextCompat(androidx.core.content.ContextCompat) LargeTest(androidx.test.filters.LargeTest) Before(org.junit.Before) SessionPlayer(androidx.media2.common.SessionPlayer) MediaMetadata(androidx.media2.common.MediaMetadata) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Executor(java.util.concurrent.Executor) TextUtils(android.text.TextUtils) UriMediaItem(androidx.media2.common.UriMediaItem) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MediaItem(androidx.media2.common.MediaItem) Rating(androidx.media2.common.Rating) R(com.google.android.exoplayer2.ext.media2.test.R) CountDownLatch(java.util.concurrent.CountDownLatch) SessionCommandGroup(androidx.media2.session.SessionCommandGroup) List(java.util.List) Nullable(androidx.annotation.Nullable) Rule(org.junit.Rule) MediaController(androidx.media2.session.MediaController) RawResourceDataSource(com.google.android.exoplayer2.upstream.RawResourceDataSource) MediaController(androidx.media2.session.MediaController) Bundle(android.os.Bundle) CountDownLatch(java.util.concurrent.CountDownLatch) SessionResult(androidx.media2.session.SessionResult) MediaSession(androidx.media2.session.MediaSession) SessionCommand(androidx.media2.session.SessionCommand) Nullable(androidx.annotation.Nullable) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Aggregations

SessionCommandGroup (androidx.media2.session.SessionCommandGroup)4 Nullable (androidx.annotation.Nullable)3 MediaItem (androidx.media2.common.MediaItem)2 MediaController (androidx.media2.session.MediaController)2 SessionCommand (androidx.media2.session.SessionCommand)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 Context (android.content.Context)1 Uri (android.net.Uri)1 Bundle (android.os.Bundle)1 TextUtils (android.text.TextUtils)1 ContextCompat (androidx.core.content.ContextCompat)1 MediaMetadata (androidx.media2.common.MediaMetadata)1 Rating (androidx.media2.common.Rating)1 SessionPlayer (androidx.media2.common.SessionPlayer)1 UriMediaItem (androidx.media2.common.UriMediaItem)1 HeartRating (androidx.media2.session.HeartRating)1 MediaSession (androidx.media2.session.MediaSession)1 SessionResult (androidx.media2.session.SessionResult)1 ApplicationProvider (androidx.test.core.app.ApplicationProvider)1 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)1