use of androidx.media2.session.MediaSession 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);
}
use of androidx.media2.session.MediaSession 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();
}
use of androidx.media2.session.MediaSession in project ExoPlayer by google.
the class SessionCallbackBuilderTest method allowedCommand_withoutPlaylist_disallowsSkipTo.
@Test
public void allowedCommand_withoutPlaylist_disallowsSkipTo() throws Exception {
int testRewindIncrementMs = 100;
int testFastForwardIncrementMs = 100;
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setRatingCallback((mediaSession, controller, mediaId, rating) -> SessionResult.RESULT_ERROR_BAD_VALUE).setRewindIncrementMs(testRewindIncrementMs).setFastForwardIncrementMs(testFastForwardIncrementMs).setMediaItemProvider(new SessionCallbackBuilder.MediaIdMediaItemProvider()).build())) {
assertPlayerResultSuccess(sessionPlayerConnector.setMediaItem(TestUtils.createMediaItem()));
assertPlayerResultSuccess(sessionPlayerConnector.prepare());
CountDownLatch latch = new CountDownLatch(1);
OnConnectedListener listener = (controller, allowedCommands) -> {
List<Integer> disallowedCommandCodes = Arrays.asList(SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_PREVIOUS_PLAYLIST_ITEM, SessionCommand.COMMAND_CODE_PLAYER_SKIP_TO_NEXT_PLAYLIST_ITEM);
assertDisallowedCommands(disallowedCommandCodes, allowedCommands);
latch.countDown();
};
try (MediaController controller = createConnectedController(session, listener, null)) {
assertThat(latch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
assertSessionResultFailure(controller.skipToNextPlaylistItem());
assertSessionResultFailure(controller.skipToPreviousPlaylistItem());
assertSessionResultFailure(controller.skipToPlaylistItem(0));
}
}
}
use of androidx.media2.session.MediaSession in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setSkipCallback_withSkipBackward_receivesOnSkipBackward.
@Test
public void setSkipCallback_withSkipBackward_receivesOnSkipBackward() throws Exception {
CountDownLatch skipBackwardCalledLatch = new CountDownLatch(1);
SessionCallbackBuilder.SkipCallback skipCallback = new SessionCallbackBuilder.SkipCallback() {
@Override
public int onSkipBackward(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
skipBackwardCalledLatch.countDown();
return SessionResult.RESULT_SUCCESS;
}
@Override
public int onSkipForward(MediaSession session, MediaSession.ControllerInfo controllerInfo) {
return SessionResult.RESULT_ERROR_NOT_SUPPORTED;
}
};
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setSkipCallback(skipCallback).build())) {
try (MediaController controller = createConnectedController(session)) {
assertSessionResultSuccess(controller.skipBackward(), CONTROLLER_COMMAND_WAIT_TIME_MS);
assertThat(skipBackwardCalledLatch.await(0, MILLISECONDS)).isTrue();
}
}
}
use of androidx.media2.session.MediaSession in project ExoPlayer by google.
the class SessionCallbackBuilderTest method setDisconnectedCallback_afterDisconnect_receivesOnDisconnected.
@Test
public void setDisconnectedCallback_afterDisconnect_receivesOnDisconnected() throws Exception {
CountDownLatch disconnectedLatch = new CountDownLatch(1);
SessionCallbackBuilder.DisconnectedCallback disconnectCallback = (session, controllerInfo) -> disconnectedLatch.countDown();
try (MediaSession session = createMediaSession(sessionPlayerConnector, new SessionCallbackBuilder(context, sessionPlayerConnector).setDisconnectedCallback(disconnectCallback).build())) {
try (MediaController controller = createConnectedController(session)) {
}
assertThat(disconnectedLatch.await(CONTROLLER_COMMAND_WAIT_TIME_MS, MILLISECONDS)).isTrue();
}
}
Aggregations