Search in sources :

Example 6 with Commands

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.
                }
            }
        }
    });
}
Also used : MediaSessionManager(androidx.media.MediaSessionManager) IBinder(android.os.IBinder) RemoteException(android.os.RemoteException) ControllerInfo(androidx.media3.session.MediaSession.ControllerInfo) Nullable(androidx.annotation.Nullable)

Example 7 with Commands

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;
}
Also used : Bundle(android.os.Bundle) ArrayList(java.util.ArrayList) UnstableApi(androidx.media3.common.util.UnstableApi)

Example 8 with Commands

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());
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) Bundle(android.os.Bundle) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) Cue(androidx.media3.common.text.Cue) Commands(androidx.media3.common.Player.Commands) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 9 with Commands

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);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) Commands(androidx.media3.common.Player.Commands) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) LargeTest(androidx.test.filters.LargeTest) Test(org.junit.Test)

Example 10 with Commands

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);
}
Also used : RemoteMockPlayer(androidx.media3.session.RemoteMediaSession.RemoteMockPlayer) Player(androidx.media3.common.Player) Commands(androidx.media3.common.Player.Commands) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) 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