Search in sources :

Example 1 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerImplLegacy method buildNewControllerInfo.

private static ControllerInfo buildNewControllerInfo(boolean initialUpdate, LegacyPlayerInfo oldLegacyPlayerInfo, ControllerInfo oldControllerInfo, LegacyPlayerInfo newLegacyPlayerInfo, long sessionFlags, boolean isSessionReady, @RatingCompat.Style int ratingType, long timeDiffMs) {
    QueueTimeline currentTimeline;
    MediaMetadata mediaMetadata;
    int currentMediaItemIndex;
    MediaMetadata playlistMetadata;
    @RepeatMode int repeatMode;
    boolean shuffleModeEnabled;
    SessionCommands availableSessionCommands;
    Commands availablePlayerCommands;
    ImmutableList<CommandButton> customLayout;
    boolean isQueueChanged = oldLegacyPlayerInfo.queue != newLegacyPlayerInfo.queue;
    currentTimeline = isQueueChanged ? QueueTimeline.create(newLegacyPlayerInfo.queue) : new QueueTimeline((QueueTimeline) oldControllerInfo.playerInfo.timeline);
    boolean isMetadataCompatChanged = oldLegacyPlayerInfo.mediaMetadataCompat != newLegacyPlayerInfo.mediaMetadataCompat || initialUpdate;
    long oldActiveQueueId = getActiveQueueId(oldLegacyPlayerInfo.playbackStateCompat);
    long newActiveQueueId = getActiveQueueId(newLegacyPlayerInfo.playbackStateCompat);
    boolean isCurrentActiveQueueIdChanged = (oldActiveQueueId != newActiveQueueId) || initialUpdate;
    if (isMetadataCompatChanged || isCurrentActiveQueueIdChanged || isQueueChanged) {
        currentMediaItemIndex = findQueueItemIndex(newLegacyPlayerInfo.queue, newActiveQueueId);
        boolean hasMediaMetadataCompat = newLegacyPlayerInfo.mediaMetadataCompat != null;
        if (hasMediaMetadataCompat && isMetadataCompatChanged) {
            mediaMetadata = MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
        } else if (!hasMediaMetadataCompat && isCurrentActiveQueueIdChanged) {
            mediaMetadata = (currentMediaItemIndex == C.INDEX_UNSET) ? MediaMetadata.EMPTY : MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.queue.get(currentMediaItemIndex).getDescription(), ratingType);
        } else {
            mediaMetadata = oldControllerInfo.playerInfo.mediaMetadata;
        }
        if (currentMediaItemIndex == C.INDEX_UNSET && isMetadataCompatChanged) {
            if (hasMediaMetadataCompat) {
                Log.w(TAG, "Adding a fake MediaItem at the end of the list because there's no QueueItem with" + " the active queue id and current Timeline should have currently playing" + " MediaItem.");
                MediaItem fakeMediaItem = MediaUtils.convertToMediaItem(newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
                currentTimeline = currentTimeline.copyWithFakeMediaItem(fakeMediaItem);
                currentMediaItemIndex = currentTimeline.getWindowCount() - 1;
            } else {
                currentTimeline = currentTimeline.copyWithFakeMediaItem(/* fakeMediaItem= */
                null);
                // Shouldn't be C.INDEX_UNSET to make getCurrentMediaItemIndex() return masked index.
                // In other words, this index is either the currently playing media item index or the
                // would-be playing index when playing.
                currentMediaItemIndex = 0;
            }
        } else if (currentMediaItemIndex != C.INDEX_UNSET) {
            currentTimeline = currentTimeline.copyWithFakeMediaItem(/* fakeMediaItem= */
            null);
            if (hasMediaMetadataCompat) {
                MediaItem mediaItem = MediaUtils.convertToMediaItem(currentTimeline.getMediaItemAt(currentMediaItemIndex).mediaId, newLegacyPlayerInfo.mediaMetadataCompat, ratingType);
                currentTimeline = currentTimeline.copyWithNewMediaItem(/* replaceIndex= */
                currentMediaItemIndex, mediaItem);
            }
        } else {
            // There's queue, but no valid queue item ID nor current media item metadata.
            // Fallback to use 0 as current media item index to mask current item index.
            currentMediaItemIndex = 0;
        }
    } else {
        currentMediaItemIndex = oldControllerInfo.playerInfo.sessionPositionInfo.positionInfo.mediaItemIndex;
        mediaMetadata = oldControllerInfo.playerInfo.mediaMetadata;
    }
    playlistMetadata = oldLegacyPlayerInfo.queueTitle == newLegacyPlayerInfo.queueTitle ? oldControllerInfo.playerInfo.playlistMetadata : MediaUtils.convertToMediaMetadata(newLegacyPlayerInfo.queueTitle);
    repeatMode = MediaUtils.convertToRepeatMode(newLegacyPlayerInfo.repeatMode);
    shuffleModeEnabled = MediaUtils.convertToShuffleModeEnabled(newLegacyPlayerInfo.shuffleMode);
    if (oldLegacyPlayerInfo.playbackStateCompat != newLegacyPlayerInfo.playbackStateCompat) {
        availableSessionCommands = MediaUtils.convertToSessionCommands(newLegacyPlayerInfo.playbackStateCompat, isSessionReady);
        customLayout = MediaUtils.convertToCustomLayout(newLegacyPlayerInfo.playbackStateCompat);
    } else {
        availableSessionCommands = oldControllerInfo.availableSessionCommands;
        customLayout = oldControllerInfo.customLayout;
    }
    // Note: Sets the available player command here although it can be obtained before session is
    // ready. It's to follow the decision on MediaController to disallow any commands before
    // connection is made.
    availablePlayerCommands = (oldControllerInfo.availablePlayerCommands == Commands.EMPTY) ? MediaUtils.convertToPlayerCommands(sessionFlags, isSessionReady) : oldControllerInfo.availablePlayerCommands;
    PlaybackException playerError = MediaUtils.convertToPlaybackException(newLegacyPlayerInfo.playbackStateCompat);
    long durationMs = MediaUtils.convertToDurationMs(newLegacyPlayerInfo.mediaMetadataCompat);
    long currentPositionMs = MediaUtils.convertToCurrentPositionMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    long bufferedPositionMs = MediaUtils.convertToBufferedPositionMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    int bufferedPercentage = MediaUtils.convertToBufferedPercentage(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    long totalBufferedDurationMs = MediaUtils.convertToTotalBufferedDurationMs(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    boolean isPlayingAd = MediaUtils.convertToIsPlayingAd(newLegacyPlayerInfo.mediaMetadataCompat);
    PlaybackParameters playbackParameters = MediaUtils.convertToPlaybackParameters(newLegacyPlayerInfo.playbackStateCompat);
    AudioAttributes audioAttributes = MediaUtils.convertToAudioAttributes(newLegacyPlayerInfo.playbackInfoCompat);
    boolean playWhenReady = MediaUtils.convertToPlayWhenReady(newLegacyPlayerInfo.playbackStateCompat);
    @State int playbackState = MediaUtils.convertToPlaybackState(newLegacyPlayerInfo.playbackStateCompat, newLegacyPlayerInfo.mediaMetadataCompat, timeDiffMs);
    boolean isPlaying = MediaUtils.convertToIsPlaying(newLegacyPlayerInfo.playbackStateCompat);
    DeviceInfo deviceInfo = MediaUtils.convertToDeviceInfo(newLegacyPlayerInfo.playbackInfoCompat);
    int deviceVolume = MediaUtils.convertToDeviceVolume(newLegacyPlayerInfo.playbackInfoCompat);
    boolean deviceMuted = MediaUtils.convertToIsDeviceMuted(newLegacyPlayerInfo.playbackInfoCompat);
    long seekBackIncrementMs = oldControllerInfo.playerInfo.seekBackIncrementMs;
    long seekForwardIncrementMs = oldControllerInfo.playerInfo.seekForwardIncrementMs;
    return createControllerInfo(currentTimeline, mediaMetadata, currentMediaItemIndex, playlistMetadata, repeatMode, shuffleModeEnabled, availableSessionCommands, availablePlayerCommands, customLayout, playerError, durationMs, currentPositionMs, bufferedPositionMs, bufferedPercentage, totalBufferedDurationMs, isPlayingAd, playbackParameters, audioAttributes, playWhenReady, playbackState, isPlaying, deviceInfo, deviceVolume, deviceMuted, seekBackIncrementMs, seekForwardIncrementMs);
}
Also used : PlaybackException(androidx.media3.common.PlaybackException) AudioAttributes(androidx.media3.common.AudioAttributes) RepeatMode(androidx.media3.common.Player.RepeatMode) MediaItem(androidx.media3.common.MediaItem) State(androidx.media3.common.Player.State) Commands(androidx.media3.common.Player.Commands) DeviceInfo(androidx.media3.common.DeviceInfo) MediaMetadata(androidx.media3.common.MediaMetadata) PlaybackParameters(androidx.media3.common.PlaybackParameters)

Example 2 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerImplLegacy method createControllerInfo.

private static ControllerInfo createControllerInfo(QueueTimeline currentTimeline, MediaMetadata mediaMetadata, int currentMediaItemIndex, MediaMetadata playlistMetadata, @RepeatMode int repeatMode, boolean shuffleModeEnabled, SessionCommands availableSessionCommands, Commands availablePlayerCommands, ImmutableList<CommandButton> customLayout, @Nullable PlaybackException playerError, long durationMs, long currentPositionMs, long bufferedPositionMs, int bufferedPercentage, long totalBufferedDurationMs, boolean isPlayingAd, PlaybackParameters playbackParameters, AudioAttributes audioAttributes, boolean playWhenReady, int playbackState, boolean isPlaying, DeviceInfo deviceInfo, int deviceVolume, boolean deviceMuted, long seekBackIncrementMs, long seekForwardIncrementMs) {
    @Nullable MediaItem currentMediaItem = currentTimeline.getMediaItemAt(currentMediaItemIndex);
    PositionInfo positionInfo = createPositionInfo(currentMediaItemIndex, currentMediaItem, currentPositionMs);
    SessionPositionInfo sessionPositionInfo = new SessionPositionInfo(/* positionInfo= */
    positionInfo, /* isPlayingAd= */
    isPlayingAd, /* eventTimeMs= */
    C.TIME_UNSET, /* durationMs= */
    durationMs, /* bufferedPositionMs= */
    bufferedPositionMs, /* bufferedPercentage= */
    bufferedPercentage, /* totalBufferedDurationMs= */
    totalBufferedDurationMs, /* currentLiveOffsetMs= */
    C.TIME_UNSET, /* contentDurationMs= */
    durationMs, /* contentBufferedPositionMs= */
    bufferedPositionMs);
    PlayerInfo playerInfo = new PlayerInfo(/* playerError= */
    playerError, /* mediaItemTransitionReason= */
    PlayerInfo.MEDIA_ITEM_TRANSITION_REASON_DEFAULT, /* sessionPositionInfo= */
    sessionPositionInfo, /* oldPositionInfo= */
    SessionPositionInfo.DEFAULT_POSITION_INFO, /* newPositionInfo= */
    SessionPositionInfo.DEFAULT_POSITION_INFO, /* discontinuityReason= */
    PlayerInfo.DISCONTINUITY_REASON_DEFAULT, /* playbackParameters= */
    playbackParameters, /* repeatMode= */
    repeatMode, /* shuffleModeEnabled= */
    shuffleModeEnabled, /* videoSize= */
    VideoSize.UNKNOWN, /* timeline= */
    currentTimeline, /* playlistMetadata= */
    playlistMetadata, /* volume= */
    1.0f, /* audioAttributes= */
    audioAttributes, /* cues= */
    Collections.emptyList(), /* deviceInfo= */
    deviceInfo, /* deviceVolume= */
    deviceVolume, /* deviceMuted= */
    deviceMuted, /* playWhenReady= */
    playWhenReady, /* playWhenReadyChangedReason= */
    PlayerInfo.PLAY_WHEN_READY_CHANGE_REASON_DEFAULT, /* playbackSuppressionReason= */
    Player.PLAYBACK_SUPPRESSION_REASON_NONE, /* playbackState= */
    playbackState, /* isPlaying= */
    isPlaying, /* isLoading= */
    false, /* mediaMetadata= */
    mediaMetadata, seekBackIncrementMs, seekForwardIncrementMs, /* maxSeekToPreviousPositionMs= */
    0L, /* parameters= */
    TrackSelectionParameters.DEFAULT_WITHOUT_CONTEXT);
    return new ControllerInfo(playerInfo, availableSessionCommands, availablePlayerCommands, customLayout);
}
Also used : MediaItem(androidx.media3.common.MediaItem) PositionInfo(androidx.media3.common.Player.PositionInfo) Nullable(androidx.annotation.Nullable)

Example 3 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerStub method onAvailableCommandsChangedFromPlayer.

@Override
public void onAvailableCommandsChangedFromPlayer(int seq, Bundle commandsBundle) {
    Commands commandsFromPlayer;
    try {
        commandsFromPlayer = Commands.CREATOR.fromBundle(commandsBundle);
    } catch (RuntimeException e) {
        Log.w(TAG, "Ignoring malformed Bundle for Commands", e);
        return;
    }
    dispatchControllerTaskOnHandler(controller -> controller.onAvailableCommandsChangedFromPlayer(commandsFromPlayer));
}
Also used : Commands(androidx.media3.common.Player.Commands)

Example 4 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerStub method onAvailableCommandsChangedFromSession.

@Override
public void onAvailableCommandsChangedFromSession(int seq, Bundle sessionCommandsBundle, Bundle playerCommandsBundle) {
    SessionCommands sessionCommands;
    try {
        sessionCommands = SessionCommands.CREATOR.fromBundle(sessionCommandsBundle);
    } catch (RuntimeException e) {
        Log.w(TAG, "Ignoring malformed Bundle for SessionCommands", e);
        return;
    }
    Commands playerCommands;
    try {
        playerCommands = Commands.CREATOR.fromBundle(playerCommandsBundle);
    } catch (RuntimeException e) {
        Log.w(TAG, "Ignoring malformed Bundle for Commands", e);
        return;
    }
    dispatchControllerTaskOnHandler(controller -> controller.onAvailableCommandsChangedFromSession(sessionCommands, playerCommands));
}
Also used : Commands(androidx.media3.common.Player.Commands)

Example 5 with Commands

use of androidx.media3.common.Player.Commands in project media by androidx.

the class MediaControllerImplBase method onAvailableCommandsChangedFromPlayer.

void onAvailableCommandsChangedFromPlayer(Commands commandsFromPlayer) {
    if (!isConnected()) {
        return;
    }
    if (Util.areEqual(playerCommandsFromPlayer, commandsFromPlayer)) {
        return;
    }
    playerCommandsFromPlayer = commandsFromPlayer;
    Commands prevIntersectedPlayerCommands = intersectedPlayerCommands;
    intersectedPlayerCommands = intersect(playerCommandsFromSession, playerCommandsFromPlayer);
    boolean intersectedPlayerCommandsChanged = !Util.areEqual(intersectedPlayerCommands, prevIntersectedPlayerCommands);
    if (intersectedPlayerCommandsChanged) {
        listeners.sendEvent(EVENT_AVAILABLE_COMMANDS_CHANGED, listener -> listener.onAvailableCommandsChanged(intersectedPlayerCommands));
    }
}
Also used : Commands(androidx.media3.common.Player.Commands)

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