From a19941f09c6e01e46ec4eebd9541bcc732df7ef2 Mon Sep 17 00:00:00 2001 From: veronicaradu Date: Tue, 17 Nov 2020 14:19:01 +0000 Subject: [PATCH] Add methods in TrackSelection to get details about the playback state. Determine whether a rebuffer occurred and if the playback is paused or resumed. PiperOrigin-RevId: 342849010 --- .../exoplayer2/ExoPlayerImplInternal.java | 30 ++++++++++++ .../trackselection/TrackSelection.java | 47 ++++++++++--------- 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java index f5cdcdc8c8..fde6869d2d 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java @@ -724,6 +724,19 @@ import java.util.concurrent.atomic.AtomicBoolean; handleMediaSourceListInfoRefreshed(timeline); } + private void notifyTrackSelectionPlayWhenReadyChanged(boolean playWhenReady) { + MediaPeriodHolder periodHolder = queue.getPlayingPeriod(); + while (periodHolder != null) { + TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll(); + for (TrackSelection trackSelection : trackSelections) { + if (trackSelection != null) { + trackSelection.onPlayWhenReadyChanged(playWhenReady); + } + } + periodHolder = periodHolder.getNext(); + } + } + private void setPlayWhenReadyInternal( boolean playWhenReady, @PlaybackSuppressionReason int playbackSuppressionReason, @@ -734,6 +747,7 @@ import java.util.concurrent.atomic.AtomicBoolean; playbackInfoUpdate.setPlayWhenReadyChangeReason(reason); playbackInfo = playbackInfo.copyWithPlayWhenReady(playWhenReady, playbackSuppressionReason); isRebuffering = false; + notifyTrackSelectionPlayWhenReadyChanged(playWhenReady); if (!shouldPlayWhenReady()) { stopRenderers(); updatePlaybackPositions(); @@ -880,6 +894,19 @@ import java.util.concurrent.atomic.AtomicBoolean; } } + private void notifyTrackSelectionRebuffer() { + MediaPeriodHolder periodHolder = queue.getPlayingPeriod(); + while (periodHolder != null) { + TrackSelection[] trackSelections = periodHolder.getTrackSelectorResult().selections.getAll(); + for (TrackSelection trackSelection : trackSelections) { + if (trackSelection != null) { + trackSelection.onRebuffer(); + } + } + periodHolder = periodHolder.getNext(); + } + } + private void doSomeWork() throws ExoPlaybackException, IOException { long operationStartTimeMs = clock.uptimeMillis(); updatePeriods(); @@ -964,6 +991,9 @@ import java.util.concurrent.atomic.AtomicBoolean; && !(enabledRendererCount == 0 ? isTimelineReady() : renderersAllowPlayback)) { isRebuffering = shouldPlayWhenReady(); setState(Player.STATE_BUFFERING); + if (isRebuffering) { + notifyTrackSelectionRebuffer(); + } livePlaybackSpeedControl.notifyRebuffer(); stopRenderers(); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java index c41d19f083..5d643bd0eb 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/TrackSelection.java @@ -73,9 +73,7 @@ public interface TrackSelection { } } - /** - * Factory for {@link TrackSelection} instances. - */ + /** Factory for {@link TrackSelection} instances. */ interface Factory { /** @@ -118,16 +116,12 @@ public interface TrackSelection { */ void disable(); - /** - * Returns the {@link TrackGroup} to which the selected tracks belong. - */ + /** Returns the {@link TrackGroup} to which the selected tracks belong. */ TrackGroup getTrackGroup(); // Static subset of selected tracks. - /** - * Returns the number of tracks in the selection. - */ + /** Returns the number of tracks in the selection. */ int length(); /** @@ -168,28 +162,21 @@ public interface TrackSelection { // Individual selected track. - /** - * Returns the {@link Format} of the individual selected track. - */ + /** Returns the {@link Format} of the individual selected track. */ Format getSelectedFormat(); - /** - * Returns the index in the track group of the individual selected track. - */ + /** Returns the index in the track group of the individual selected track. */ int getSelectedIndexInTrackGroup(); - /** - * Returns the index of the selected track. - */ + /** Returns the index of the selected track. */ int getSelectedIndex(); - /** - * Returns the reason for the current track selection. - */ + /** Returns the reason for the current track selection. */ int getSelectionReason(); /** Returns optional data associated with the current track selection. */ - @Nullable Object getSelectionData(); + @Nullable + Object getSelectionData(); // Adaptation. @@ -208,6 +195,22 @@ public interface TrackSelection { */ default void onDiscontinuity() {} + /** + * Called to notify when a rebuffer occurred. + * + *

A rebuffer is defined to be caused by buffer depletion rather than a user action. Hence this + * method is not called during initial buffering or when buffering as a result of a seek + * operation. + */ + default void onRebuffer() {} + + /** + * Called to notify when the playback is paused or resumed. + * + * @param playWhenReady Whether playback will proceed when ready. + */ + default void onPlayWhenReadyChanged(boolean playWhenReady) {} + /** * Updates the selected track for sources that load media in discrete {@link MediaChunk}s. *