use of androidx.media3.common.Player.Commands in project media by androidx.
the class MediaSessionStub method connect.
public void connect(IMediaController caller, int controllerVersion, String callingPackage, int pid, int uid, Bundle connectionHints) {
MediaSessionManager.RemoteUserInfo remoteUserInfo = new MediaSessionManager.RemoteUserInfo(callingPackage, pid, uid);
ControllerInfo controllerInfo = new ControllerInfo(remoteUserInfo, controllerVersion, sessionManager.isTrustedForMediaControl(remoteUserInfo), new Controller2Cb(caller), connectionHints);
@Nullable MediaSessionImpl sessionImpl = this.sessionImpl.get();
if (sessionImpl == null || sessionImpl.isReleased()) {
try {
caller.onDisconnected(/* seq= */
0);
} catch (RemoteException e) {
// Controller may be died prematurely.
// Not an issue because we'll ignore it anyway.
}
return;
}
pendingControllers.add(controllerInfo);
postOrRun(sessionImpl.getApplicationHandler(), () -> {
boolean connected = false;
try {
pendingControllers.remove(controllerInfo);
if (sessionImpl.isReleased()) {
return;
}
IBinder callbackBinder = checkStateNotNull((Controller2Cb) controllerInfo.getControllerCb()).getCallbackBinder();
MediaSession.ConnectionResult connectionResult = sessionImpl.onConnectOnHandler(controllerInfo);
// media keys to.
if (!connectionResult.isAccepted && !controllerInfo.isTrusted()) {
return;
}
if (!connectionResult.isAccepted) {
// For the accepted controller, send non-null allowed commands to keep connection.
connectionResult = MediaSession.ConnectionResult.accept(SessionCommands.EMPTY, Player.Commands.EMPTY);
}
SequencedFutureManager sequencedFutureManager;
if (connectedControllersManager.isConnected(controllerInfo)) {
Log.w(TAG, "Controller " + controllerInfo + " has sent connection" + " request multiple times");
}
connectedControllersManager.addController(callbackBinder, controllerInfo, connectionResult.availableSessionCommands, connectionResult.availablePlayerCommands);
sequencedFutureManager = checkStateNotNull(connectedControllersManager.getSequencedFutureManager(controllerInfo));
// If connection is accepted, notify the current state to the controller.
// It's needed because we cannot call synchronous calls between
// session/controller.
PlayerWrapper playerWrapper = sessionImpl.getPlayerWrapper();
PlayerInfo playerInfo = playerWrapper.createPlayerInfoForBundling();
ConnectionState state = new ConnectionState(MediaLibraryInfo.VERSION_INT, MediaSessionStub.this, sessionImpl.getSessionActivity(), connectionResult.availableSessionCommands, connectionResult.availablePlayerCommands, playerWrapper.getAvailableCommands(), sessionImpl.getToken().getExtras(), playerInfo);
// another thread.
if (sessionImpl.isReleased()) {
return;
}
try {
caller.onConnected(sequencedFutureManager.obtainNextSequenceNumber(), state.toBundle());
connected = true;
} catch (RemoteException e) {
// Controller may be died prematurely.
}
sessionImpl.onPostConnectOnHandler(controllerInfo);
} finally {
if (!connected) {
try {
caller.onDisconnected(/* seq= */
0);
} catch (RemoteException e) {
// Controller may be died prematurely.
// Not an issue because we'll ignore it anyway.
}
}
}
});
}
use of androidx.media3.common.Player.Commands in project media by androidx.
the class SessionCommands method toBundle.
@UnstableApi
@Override
public Bundle toBundle() {
Bundle bundle = new Bundle();
ArrayList<Bundle> sessionCommandBundleList = new ArrayList<>();
for (SessionCommand command : commands) {
sessionCommandBundleList.add(command.toBundle());
}
bundle.putParcelableArrayList(keyForField(FIELD_SESSION_COMMANDS), sessionCommandBundleList);
return bundle;
}
use of androidx.media3.common.Player.Commands in project media by androidx.
the class MediaControllerListenerTest method onCues_emptyList_whenCommandUnavailable.
@Test
public void onCues_emptyList_whenCommandUnavailable() throws Exception {
Cue testCue1 = new Cue.Builder().setText(SpannedString.valueOf("cue1")).build();
Cue testCue2 = new Cue.Builder().setText(SpannedString.valueOf("cue2")).build();
List<Cue> testCues = ImmutableList.of(testCue1, testCue2);
Bundle playerConfig = new RemoteMediaSession.MockPlayerConfigBuilder().setCurrentCues(testCues).build();
remoteSession.setPlayer(playerConfig);
MediaController controller = controllerTestRule.createController(remoteSession.getToken());
CountDownLatch latch = new CountDownLatch(1);
List<Cue> cuesFromGetter = new ArrayList<>();
List<Cue> cuesFromParam = new ArrayList<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onCues(List<Cue> cues) {
cuesFromParam.clear();
cuesFromParam.addAll(cues);
cuesFromGetter.clear();
cuesFromGetter.addAll(controller.getCurrentCues());
latch.countDown();
}
};
controller.addListener(listener);
Commands commandsWithoutGetText = createPlayerCommandsWithout(Player.COMMAND_GET_TEXT);
remoteSession.setAvailableCommands(SessionCommands.EMPTY, commandsWithoutGetText);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(cuesFromParam).isEqualTo(ImmutableList.of());
assertThat(cuesFromGetter).isEqualTo(ImmutableList.of());
}
use of androidx.media3.common.Player.Commands in project media by androidx.
the class MediaControllerListenerTest method onAvailableCommandsChanged_isCalledByPlayerChange.
/**
* This also tests {@link MediaController#getAvailableCommands()}.
*/
@Test
public void onAvailableCommandsChanged_isCalledByPlayerChange() throws Exception {
Commands commandsWithAllCommands = new Player.Commands.Builder().addAllCommands().build();
remoteSession.getMockPlayer().notifyAvailableCommandsChanged(commandsWithAllCommands);
MediaController controller = controllerTestRule.createController(remoteSession.getToken(), /* connectionHints= */
null, /* listener= */
null);
AtomicReference<Commands> availableCommandsFromParamRef = new AtomicReference<>();
AtomicReference<Commands> availableCommandsFromGetterRef = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
Player.Listener listener = new Player.Listener() {
@Override
public void onAvailableCommandsChanged(Commands availableCommands) {
availableCommandsFromParamRef.set(availableCommands);
availableCommandsFromGetterRef.set(controller.getAvailableCommands());
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
Commands commandsWithSetRepeat = createPlayerCommandsWith(COMMAND_SET_REPEAT_MODE);
remoteSession.getMockPlayer().notifyAvailableCommandsChanged(commandsWithSetRepeat);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(availableCommandsFromParamRef.get()).isEqualTo(commandsWithSetRepeat);
assertThat(availableCommandsFromGetterRef.get()).isEqualTo(commandsWithSetRepeat);
}
use of androidx.media3.common.Player.Commands in project media by androidx.
the class MediaControllerListenerTest method onAvailableCommandsChanged_isCalledBySessionChange.
/**
* This also tests {@link MediaController#getAvailableCommands()}.
*/
@Test
public void onAvailableCommandsChanged_isCalledBySessionChange() throws Exception {
Commands commandsWithAllCommands = new Player.Commands.Builder().addAllCommands().build();
remoteSession.getMockPlayer().notifyAvailableCommandsChanged(commandsWithAllCommands);
MediaController controller = controllerTestRule.createController(remoteSession.getToken(), /* connectionHints= */
null, /* listener= */
null);
CountDownLatch latch = new CountDownLatch(1);
AtomicReference<Commands> availableCommandsFromParamRef = new AtomicReference<>();
AtomicReference<Commands> availableCommandsFromGetterRef = new AtomicReference<>();
Player.Listener listener = new Player.Listener() {
@Override
public void onAvailableCommandsChanged(Commands availableCommands) {
availableCommandsFromParamRef.set(availableCommands);
availableCommandsFromGetterRef.set(controller.getAvailableCommands());
latch.countDown();
}
};
threadTestRule.getHandler().postAndSync(() -> controller.addListener(listener));
Commands commandsWithSetRepeat = createPlayerCommandsWith(COMMAND_SET_REPEAT_MODE);
remoteSession.setAvailableCommands(SessionCommands.EMPTY, commandsWithSetRepeat);
assertThat(latch.await(TIMEOUT_MS, MILLISECONDS)).isTrue();
assertThat(availableCommandsFromParamRef.get()).isEqualTo(commandsWithSetRepeat);
assertThat(availableCommandsFromGetterRef.get()).isEqualTo(commandsWithSetRepeat);
}
Aggregations