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
This commit is contained in:
veronicaradu 2020-11-17 14:19:01 +00:00 committed by Ian Baker
parent e4d693ebb9
commit a19941f09c
2 changed files with 55 additions and 22 deletions

View File

@ -724,6 +724,19 @@ import java.util.concurrent.atomic.AtomicBoolean;
handleMediaSourceListInfoRefreshed(timeline); 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( private void setPlayWhenReadyInternal(
boolean playWhenReady, boolean playWhenReady,
@PlaybackSuppressionReason int playbackSuppressionReason, @PlaybackSuppressionReason int playbackSuppressionReason,
@ -734,6 +747,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
playbackInfoUpdate.setPlayWhenReadyChangeReason(reason); playbackInfoUpdate.setPlayWhenReadyChangeReason(reason);
playbackInfo = playbackInfo.copyWithPlayWhenReady(playWhenReady, playbackSuppressionReason); playbackInfo = playbackInfo.copyWithPlayWhenReady(playWhenReady, playbackSuppressionReason);
isRebuffering = false; isRebuffering = false;
notifyTrackSelectionPlayWhenReadyChanged(playWhenReady);
if (!shouldPlayWhenReady()) { if (!shouldPlayWhenReady()) {
stopRenderers(); stopRenderers();
updatePlaybackPositions(); 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 { private void doSomeWork() throws ExoPlaybackException, IOException {
long operationStartTimeMs = clock.uptimeMillis(); long operationStartTimeMs = clock.uptimeMillis();
updatePeriods(); updatePeriods();
@ -964,6 +991,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
&& !(enabledRendererCount == 0 ? isTimelineReady() : renderersAllowPlayback)) { && !(enabledRendererCount == 0 ? isTimelineReady() : renderersAllowPlayback)) {
isRebuffering = shouldPlayWhenReady(); isRebuffering = shouldPlayWhenReady();
setState(Player.STATE_BUFFERING); setState(Player.STATE_BUFFERING);
if (isRebuffering) {
notifyTrackSelectionRebuffer();
}
livePlaybackSpeedControl.notifyRebuffer(); livePlaybackSpeedControl.notifyRebuffer();
stopRenderers(); stopRenderers();
} }

View File

@ -73,9 +73,7 @@ public interface TrackSelection {
} }
} }
/** /** Factory for {@link TrackSelection} instances. */
* Factory for {@link TrackSelection} instances.
*/
interface Factory { interface Factory {
/** /**
@ -118,16 +116,12 @@ public interface TrackSelection {
*/ */
void disable(); void disable();
/** /** Returns the {@link TrackGroup} to which the selected tracks belong. */
* Returns the {@link TrackGroup} to which the selected tracks belong.
*/
TrackGroup getTrackGroup(); TrackGroup getTrackGroup();
// Static subset of selected tracks. // Static subset of selected tracks.
/** /** Returns the number of tracks in the selection. */
* Returns the number of tracks in the selection.
*/
int length(); int length();
/** /**
@ -168,28 +162,21 @@ public interface TrackSelection {
// Individual selected track. // Individual selected track.
/** /** Returns the {@link Format} of the individual selected track. */
* Returns the {@link Format} of the individual selected track.
*/
Format getSelectedFormat(); 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(); int getSelectedIndexInTrackGroup();
/** /** Returns the index of the selected track. */
* Returns the index of the selected track.
*/
int getSelectedIndex(); int getSelectedIndex();
/** /** Returns the reason for the current track selection. */
* Returns the reason for the current track selection.
*/
int getSelectionReason(); int getSelectionReason();
/** Returns optional data associated with the current track selection. */ /** Returns optional data associated with the current track selection. */
@Nullable Object getSelectionData(); @Nullable
Object getSelectionData();
// Adaptation. // Adaptation.
@ -208,6 +195,22 @@ public interface TrackSelection {
*/ */
default void onDiscontinuity() {} default void onDiscontinuity() {}
/**
* Called to notify when a rebuffer occurred.
*
* <p>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. * Updates the selected track for sources that load media in discrete {@link MediaChunk}s.
* *