Commit 846e0666 by tonihei Committed by Oliver Woodman

Restructure updatePeriods code for better readability.

Moved update of reading and playing periods in their own respective method.
This is a no-op change.

PiperOrigin-RevId: 260463668
parent 09835c45
......@@ -1490,80 +1490,86 @@ import java.util.concurrent.atomic.AtomicBoolean;
mediaSource.maybeThrowSourceInfoRefreshError();
return;
}
// Update the loading period if required.
maybeUpdateLoadingPeriod();
maybeUpdatePlayingPeriod();
maybeUpdateReadingPeriod();
}
private void maybeUpdateLoadingPeriod() throws IOException {
queue.reevaluateBuffer(rendererPositionUs);
if (queue.shouldLoadNextMediaPeriod()) {
MediaPeriodInfo info = queue.getNextMediaPeriodInfo(rendererPositionUs, playbackInfo);
if (info == null) {
maybeThrowSourceInfoRefreshError();
} else {
MediaPeriod mediaPeriod =
queue.enqueueNextMediaPeriod(
rendererCapabilities, trackSelector, loadControl.getAllocator(), mediaSource, info);
mediaPeriod.prepare(this, info.startPositionUs);
setIsLoading(true);
handleLoadingMediaPeriodChanged(/* loadingTrackSelectionChanged= */ false);
}
}
MediaPeriodHolder loadingPeriodHolder = queue.getLoadingPeriod();
if (loadingPeriodHolder == null || loadingPeriodHolder.isFullyBuffered()) {
setIsLoading(false);
} else if (!playbackInfo.isLoading) {
maybeContinueLoading();
}
}
if (!queue.hasPlayingPeriod()) {
// We're waiting for the first period to be prepared.
return;
}
// Advance the playing period if necessary.
MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
MediaPeriodHolder readingPeriodHolder = queue.getReadingPeriod();
private void maybeUpdatePlayingPeriod() throws ExoPlaybackException {
boolean advancedPlayingPeriod = false;
while (playWhenReady
&& playingPeriodHolder != readingPeriodHolder
&& rendererPositionUs >= playingPeriodHolder.getNext().getStartPositionRendererTime()) {
// All enabled renderers' streams have been read to the end, and the playback position reached
// the end of the playing period, so advance playback to the next period.
while (shouldAdvancePlayingPeriod()) {
if (advancedPlayingPeriod) {
// If we advance more than one period at a time, notify listeners after each update.
maybeNotifyPlaybackInfoChanged();
}
int discontinuityReason =
playingPeriodHolder.info.isLastInTimelinePeriod
? Player.DISCONTINUITY_REASON_PERIOD_TRANSITION
: Player.DISCONTINUITY_REASON_AD_INSERTION;
MediaPeriodHolder oldPlayingPeriodHolder = playingPeriodHolder;
playingPeriodHolder = queue.advancePlayingPeriod();
MediaPeriodHolder oldPlayingPeriodHolder = queue.getPlayingPeriod();
MediaPeriodHolder newPlayingPeriodHolder = queue.advancePlayingPeriod();
updatePlayingPeriodRenderers(oldPlayingPeriodHolder);
playbackInfo =
playbackInfo.copyWithNewPosition(
playingPeriodHolder.info.id,
playingPeriodHolder.info.startPositionUs,
playingPeriodHolder.info.contentPositionUs,
newPlayingPeriodHolder.info.id,
newPlayingPeriodHolder.info.startPositionUs,
newPlayingPeriodHolder.info.contentPositionUs,
getTotalBufferedDurationUs());
int discontinuityReason =
oldPlayingPeriodHolder.info.isLastInTimelinePeriod
? Player.DISCONTINUITY_REASON_PERIOD_TRANSITION
: Player.DISCONTINUITY_REASON_AD_INSERTION;
playbackInfoUpdate.setPositionDiscontinuity(discontinuityReason);
updatePlaybackPositions();
advancedPlayingPeriod = true;
}
}
if (readingPeriodHolder.info.isFinal) {
for (int i = 0; i < renderers.length; i++) {
Renderer renderer = renderers[i];
SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
// Defer setting the stream as final until the renderer has actually consumed the whole
// stream in case of playlist changes that cause the stream to be no longer final.
if (sampleStream != null && renderer.getStream() == sampleStream
&& renderer.hasReadStreamToEnd()) {
renderer.setCurrentStreamFinal();
}
}
private void maybeUpdateReadingPeriod() throws ExoPlaybackException, IOException {
MediaPeriodHolder readingPeriodHolder = queue.getReadingPeriod();
if (readingPeriodHolder == null) {
return;
}
// Advance the reading period if necessary.
if (readingPeriodHolder.getNext() == null) {
// We don't have a successor to advance the reading period to.
if (readingPeriodHolder.info.isFinal) {
for (int i = 0; i < renderers.length; i++) {
Renderer renderer = renderers[i];
SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
// Defer setting the stream as final until the renderer has actually consumed the whole
// stream in case of playlist changes that cause the stream to be no longer final.
if (sampleStream != null
&& renderer.getStream() == sampleStream
&& renderer.hasReadStreamToEnd()) {
renderer.setCurrentStreamFinal();
}
}
}
return;
}
for (int i = 0; i < renderers.length; i++) {
Renderer renderer = renderers[i];
SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
if (renderer.getStream() != sampleStream
|| (sampleStream != null && !renderer.hasReadStreamToEnd())) {
// The current reading period is still being read by at least one renderer.
return;
}
if (!hasReadingPeriodFinishedReading()) {
return;
}
if (!readingPeriodHolder.getNext().prepared) {
......@@ -1576,18 +1582,18 @@ import java.util.concurrent.atomic.AtomicBoolean;
readingPeriodHolder = queue.advanceReadingPeriod();
TrackSelectorResult newTrackSelectorResult = readingPeriodHolder.getTrackSelectorResult();
boolean initialDiscontinuity =
readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET;
if (readingPeriodHolder.mediaPeriod.readDiscontinuity() != C.TIME_UNSET) {
// The new period starts with a discontinuity, so the renderers will play out all data, then
// be disabled and re-enabled when they start playing the next period.
setAllRendererStreamsFinal();
return;
}
for (int i = 0; i < renderers.length; i++) {
Renderer renderer = renderers[i];
boolean rendererWasEnabled = oldTrackSelectorResult.isRendererEnabled(i);
if (!rendererWasEnabled) {
// The renderer was disabled and will be enabled when we play the next period.
} else if (initialDiscontinuity) {
// The new period starts with a discontinuity, so the renderer will play out all data then
// be disabled and re-enabled when it starts playing the next period.
renderer.setCurrentStreamFinal();
} else if (!renderer.isCurrentStreamFinal()) {
if (rendererWasEnabled && !renderer.isCurrentStreamFinal()) {
// The renderer is enabled and its stream is not final, so we still have a chance to replace
// the sample streams.
TrackSelection newSelection = newTrackSelectorResult.selections.get(i);
boolean newRendererEnabled = newTrackSelectorResult.isRendererEnabled(i);
boolean isNoSampleRenderer = rendererCapabilities[i].getTrackType() == C.TRACK_TYPE_NONE;
......@@ -1615,23 +1621,41 @@ import java.util.concurrent.atomic.AtomicBoolean;
}
}
private void maybeUpdateLoadingPeriod() throws IOException {
queue.reevaluateBuffer(rendererPositionUs);
if (queue.shouldLoadNextMediaPeriod()) {
MediaPeriodInfo info = queue.getNextMediaPeriodInfo(rendererPositionUs, playbackInfo);
if (info == null) {
maybeThrowSourceInfoRefreshError();
} else {
MediaPeriod mediaPeriod =
queue.enqueueNextMediaPeriod(
rendererCapabilities,
trackSelector,
loadControl.getAllocator(),
mediaSource,
info);
mediaPeriod.prepare(this, info.startPositionUs);
setIsLoading(true);
handleLoadingMediaPeriodChanged(/* loadingTrackSelectionChanged= */ false);
private boolean shouldAdvancePlayingPeriod() {
if (!playWhenReady) {
return false;
}
MediaPeriodHolder playingPeriodHolder = queue.getPlayingPeriod();
if (playingPeriodHolder == null) {
return false;
}
MediaPeriodHolder readingPeriodHolder = queue.getReadingPeriod();
if (playingPeriodHolder == readingPeriodHolder) {
return false;
}
MediaPeriodHolder nextPlayingPeriodHolder =
Assertions.checkNotNull(playingPeriodHolder.getNext());
return rendererPositionUs >= nextPlayingPeriodHolder.getStartPositionRendererTime();
}
private boolean hasReadingPeriodFinishedReading() {
MediaPeriodHolder readingPeriodHolder = queue.getReadingPeriod();
for (int i = 0; i < renderers.length; i++) {
Renderer renderer = renderers[i];
SampleStream sampleStream = readingPeriodHolder.sampleStreams[i];
if (renderer.getStream() != sampleStream
|| (sampleStream != null && !renderer.hasReadStreamToEnd())) {
// The current reading period is still being read by at least one renderer.
return false;
}
}
return true;
}
private void setAllRendererStreamsFinal() {
for (Renderer renderer : renderers) {
if (renderer.getStream() != null) {
renderer.setCurrentStreamFinal();
}
}
}
......
......@@ -170,6 +170,7 @@ import com.google.android.exoplayer2.util.Assertions;
* Returns the loading period holder which is at the end of the queue, or null if the queue is
* empty.
*/
@Nullable
public MediaPeriodHolder getLoadingPeriod() {
return loading;
}
......@@ -178,6 +179,7 @@ import com.google.android.exoplayer2.util.Assertions;
* Returns the playing period holder which is at the front of the queue, or null if the queue is
* empty or hasn't started playing.
*/
@Nullable
public MediaPeriodHolder getPlayingPeriod() {
return playing;
}
......@@ -186,6 +188,7 @@ import com.google.android.exoplayer2.util.Assertions;
* Returns the reading period holder, or null if the queue is empty or the player hasn't started
* reading.
*/
@Nullable
public MediaPeriodHolder getReadingPeriod() {
return reading;
}
......@@ -194,6 +197,7 @@ import com.google.android.exoplayer2.util.Assertions;
* Returns the period holder in the front of the queue which is the playing period holder when
* playing, or null if the queue is empty.
*/
@Nullable
public MediaPeriodHolder getFrontPeriod() {
return hasPlayingPeriod() ? playing : loading;
}
......@@ -221,6 +225,7 @@ import com.google.android.exoplayer2.util.Assertions;
*
* @return The updated playing period holder, or null if the queue is or becomes empty.
*/
@Nullable
public MediaPeriodHolder advancePlayingPeriod() {
if (playing != null) {
if (playing == reading) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment