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);
}
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);
}
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();
}
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();
}
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();
}
Aggregations