Simplify ExoPlayerImplInternal

- Remove playingPeriodEndPositionUs. It doesn't look like it's
  required.
- Rename time variables to make it clearer what they are.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=131027164
This commit is contained in:
olly 2016-08-23 00:55:21 -07:00 committed by Oliver Woodman
parent 94c908e89c
commit 458c7bfc05

View File

@ -121,7 +121,7 @@ import java.io.IOException;
private int customMessagesProcessed; private int customMessagesProcessed;
private long elapsedRealtimeUs; private long elapsedRealtimeUs;
private long internalPositionUs; private long rendererPositionUs;
private boolean isTimelineReady; private boolean isTimelineReady;
private boolean isTimelineEnded; private boolean isTimelineEnded;
@ -129,7 +129,6 @@ import java.io.IOException;
private Period playingPeriod; private Period playingPeriod;
private Period readingPeriod; private Period readingPeriod;
private Period loadingPeriod; private Period loadingPeriod;
private long playingPeriodEndPositionUs;
private MediaTimeline timeline; private MediaTimeline timeline;
@ -149,11 +148,8 @@ import java.io.IOException;
renderers[i].setIndex(i); renderers[i].setIndex(i);
rendererCapabilities[i] = renderers[i].getCapabilities(); rendererCapabilities[i] = renderers[i].getCapabilities();
} }
playingPeriodEndPositionUs = C.TIME_UNSET;
standaloneMediaClock = new StandaloneMediaClock(); standaloneMediaClock = new StandaloneMediaClock();
enabledRenderers = new Renderer[0]; enabledRenderers = new Renderer[0];
trackSelector.init(this); trackSelector.init(this);
// Note: The documentation for Process.THREAD_PRIORITY_AUDIO that states "Applications can // Note: The documentation for Process.THREAD_PRIORITY_AUDIO that states "Applications can
@ -385,19 +381,19 @@ import java.io.IOException;
MediaPeriod mediaPeriod = playingPeriod.mediaPeriod; MediaPeriod mediaPeriod = playingPeriod.mediaPeriod;
// Update the playback position. // Update the playback position.
long positionUs = mediaPeriod.readDiscontinuity(); long periodPositionUs = mediaPeriod.readDiscontinuity();
if (positionUs != C.TIME_UNSET) { if (periodPositionUs != C.TIME_UNSET) {
resetInternalPosition(positionUs); resetRendererPosition(periodPositionUs);
} else { } else {
if (rendererMediaClockSource != null && !rendererMediaClockSource.isEnded()) { if (rendererMediaClockSource != null && !rendererMediaClockSource.isEnded()) {
internalPositionUs = rendererMediaClock.getPositionUs(); rendererPositionUs = rendererMediaClock.getPositionUs();
standaloneMediaClock.setPositionUs(internalPositionUs); standaloneMediaClock.setPositionUs(rendererPositionUs);
} else { } else {
internalPositionUs = standaloneMediaClock.getPositionUs(); rendererPositionUs = standaloneMediaClock.getPositionUs();
} }
positionUs = internalPositionUs - playingPeriod.offsetUs; periodPositionUs = rendererPositionUs - playingPeriod.rendererPositionOffsetUs;
} }
playbackInfo.positionUs = positionUs; playbackInfo.positionUs = periodPositionUs;
elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000; elapsedRealtimeUs = SystemClock.elapsedRealtime() * 1000;
// Update the buffered position. // Update the buffered position.
@ -427,7 +423,7 @@ import java.io.IOException;
// TODO: Each renderer should return the maximum delay before which it wishes to be called // TODO: Each renderer should return the maximum delay before which it wishes to be called
// again. The minimum of these values should then be used as the delay before the next // again. The minimum of these values should then be used as the delay before the next
// invocation of this method. // invocation of this method.
renderer.render(internalPositionUs, elapsedRealtimeUs); renderer.render(rendererPositionUs, elapsedRealtimeUs);
allRenderersEnded = allRenderersEnded && renderer.isEnded(); allRenderersEnded = allRenderersEnded && renderer.isEnded();
// Determine whether the renderer is ready (or ended). If it's not, throw an error that's // Determine whether the renderer is ready (or ended). If it's not, throw an error that's
// preventing the renderer from making progress, if such an error exists. // preventing the renderer from making progress, if such an error exists.
@ -493,42 +489,43 @@ import java.io.IOException;
} }
} }
private void seekToInternal(int periodIndex, long positionUs) throws ExoPlaybackException { private void seekToInternal(int periodIndex, long periodPositionUs) throws ExoPlaybackException {
try { try {
if (positionUs == C.TIME_UNSET && timeline != null if (periodPositionUs == C.TIME_UNSET && timeline != null
&& periodIndex < timeline.getPeriodCount()) { && periodIndex < timeline.getPeriodCount()) {
// We know about the window, so seek to its default initial position now. // We know about the window, so seek to its default initial position now.
Pair<Integer, Long> defaultPosition = getDefaultPosition(periodIndex); Pair<Integer, Long> defaultPosition = getDefaultPosition(periodIndex);
periodIndex = defaultPosition.first; periodIndex = defaultPosition.first;
positionUs = defaultPosition.second; periodPositionUs = defaultPosition.second;
} }
if (periodIndex == playbackInfo.periodIndex if (periodIndex == playbackInfo.periodIndex
&& ((positionUs == C.TIME_UNSET && playbackInfo.positionUs == C.TIME_UNSET) && ((periodPositionUs == C.TIME_UNSET && playbackInfo.positionUs == C.TIME_UNSET)
|| ((positionUs / 1000) == (playbackInfo.positionUs / 1000)))) { || ((periodPositionUs / 1000) == (playbackInfo.positionUs / 1000)))) {
// Seek position equals the current position. Do nothing. // Seek position equals the current position. Do nothing.
return; return;
} }
positionUs = seekToPeriodPosition(periodIndex, positionUs); periodPositionUs = seekToPeriodPosition(periodIndex, periodPositionUs);
} finally { } finally {
playbackInfo = new PlaybackInfo(periodIndex, positionUs); playbackInfo = new PlaybackInfo(periodIndex, periodPositionUs);
eventHandler.obtainMessage(MSG_SEEK_ACK, playbackInfo).sendToTarget(); eventHandler.obtainMessage(MSG_SEEK_ACK, playbackInfo).sendToTarget();
} }
} }
private long seekToPeriodPosition(int periodIndex, long positionUs) throws ExoPlaybackException { private long seekToPeriodPosition(int periodIndex, long periodPositionUs)
throws ExoPlaybackException {
if (mediaSource == null) { if (mediaSource == null) {
if (positionUs != C.TIME_UNSET) { if (periodPositionUs != C.TIME_UNSET) {
resetInternalPosition(positionUs); resetRendererPosition(periodPositionUs);
} }
return positionUs; return periodPositionUs;
} }
stopRenderers(); stopRenderers();
rebuffering = false; rebuffering = false;
setState(ExoPlayer.STATE_BUFFERING); setState(ExoPlayer.STATE_BUFFERING);
if (positionUs == C.TIME_UNSET if (periodPositionUs == C.TIME_UNSET
|| (readingPeriod != playingPeriod && (periodIndex == playingPeriod.index || (readingPeriod != playingPeriod && (periodIndex == playingPeriod.index
|| (readingPeriod != null && periodIndex == readingPeriod.index)))) { || (readingPeriod != null && periodIndex == readingPeriod.index)))) {
// Clear the timeline because either the seek position is not known, or a renderer is reading // Clear the timeline because either the seek position is not known, or a renderer is reading
@ -567,29 +564,29 @@ import java.io.IOException;
readingPeriod = playingPeriod; readingPeriod = playingPeriod;
loadingPeriod = playingPeriod; loadingPeriod = playingPeriod;
if (playingPeriod.hasEnabledTracks) { if (playingPeriod.hasEnabledTracks) {
positionUs = playingPeriod.mediaPeriod.seekToUs(positionUs); periodPositionUs = playingPeriod.mediaPeriod.seekToUs(periodPositionUs);
} }
resetInternalPosition(positionUs); resetRendererPosition(periodPositionUs);
maybeContinueLoading(); maybeContinueLoading();
} else { } else {
playingPeriod = null; playingPeriod = null;
readingPeriod = null; readingPeriod = null;
loadingPeriod = null; loadingPeriod = null;
if (positionUs != C.TIME_UNSET) { if (periodPositionUs != C.TIME_UNSET) {
resetInternalPosition(positionUs); resetRendererPosition(periodPositionUs);
} }
} }
updatePlaybackPositions(); updatePlaybackPositions();
handler.sendEmptyMessage(MSG_DO_SOME_WORK); handler.sendEmptyMessage(MSG_DO_SOME_WORK);
return positionUs; return periodPositionUs;
} }
private void resetInternalPosition(long periodPositionUs) throws ExoPlaybackException { private void resetRendererPosition(long periodPositionUs) throws ExoPlaybackException {
long periodOffsetUs = playingPeriod == null ? 0 : playingPeriod.offsetUs; long periodOffsetUs = playingPeriod == null ? 0 : playingPeriod.rendererPositionOffsetUs;
internalPositionUs = periodOffsetUs + periodPositionUs; rendererPositionUs = periodOffsetUs + periodPositionUs;
standaloneMediaClock.setPositionUs(internalPositionUs); standaloneMediaClock.setPositionUs(rendererPositionUs);
for (Renderer renderer : enabledRenderers) { for (Renderer renderer : enabledRenderers) {
renderer.resetPosition(internalPositionUs); renderer.resetPosition(rendererPositionUs);
} }
} }
@ -628,7 +625,6 @@ import java.io.IOException;
mediaSource = null; mediaSource = null;
} }
releasePeriodsFrom(playingPeriod != null ? playingPeriod : loadingPeriod); releasePeriodsFrom(playingPeriod != null ? playingPeriod : loadingPeriod);
playingPeriodEndPositionUs = C.TIME_UNSET;
isTimelineReady = false; isTimelineReady = false;
isTimelineEnded = false; isTimelineEnded = false;
playingPeriod = null; playingPeriod = null;
@ -694,17 +690,16 @@ import java.io.IOException;
playingPeriod.nextPeriod = null; playingPeriod.nextPeriod = null;
readingPeriod = playingPeriod; readingPeriod = playingPeriod;
loadingPeriod = playingPeriod; loadingPeriod = playingPeriod;
playingPeriodEndPositionUs = C.TIME_UNSET;
bufferAheadPeriodCount = 0; bufferAheadPeriodCount = 0;
// Update streams for the new selection, recreating all streams if reading ahead. // Update streams for the new selection, recreating all streams if reading ahead.
boolean recreateStreams = readingPeriod != playingPeriod; boolean recreateStreams = readingPeriod != playingPeriod;
boolean[] streamResetFlags = new boolean[renderers.length]; boolean[] streamResetFlags = new boolean[renderers.length];
long positionUs = playingPeriod.updatePeriodTrackSelection(playbackInfo.positionUs, long periodPositionUs = playingPeriod.updatePeriodTrackSelection(playbackInfo.positionUs,
loadControl, recreateStreams, streamResetFlags); loadControl, recreateStreams, streamResetFlags);
if (positionUs != playbackInfo.positionUs) { if (periodPositionUs != playbackInfo.positionUs) {
playbackInfo.positionUs = positionUs; playbackInfo.positionUs = periodPositionUs;
resetInternalPosition(positionUs); resetRendererPosition(periodPositionUs);
} }
int enabledRendererCount = 0; int enabledRendererCount = 0;
@ -749,8 +744,9 @@ import java.io.IOException;
bufferAheadPeriodCount--; bufferAheadPeriodCount--;
} }
loadingPeriod.nextPeriod = null; loadingPeriod.nextPeriod = null;
long positionUs = Math.max(0, internalPositionUs - loadingPeriod.offsetUs); long loadingPeriodPositionUs = Math.max(0,
loadingPeriod.updatePeriodTrackSelection(positionUs, loadControl, false); rendererPositionUs - loadingPeriod.rendererPositionOffsetUs);
loadingPeriod.updatePeriodTrackSelection(loadingPeriodPositionUs, loadControl, false);
} }
maybeContinueLoading(); maybeContinueLoading();
updatePlaybackPositions(); updatePlaybackPositions();
@ -761,16 +757,17 @@ import java.io.IOException;
if (loadingPeriod == null) { if (loadingPeriod == null) {
return false; return false;
} }
long positionUs = internalPositionUs - loadingPeriod.offsetUs; long loadingPeriodPositionUs = rendererPositionUs - loadingPeriod.rendererPositionOffsetUs;
long bufferedPositionUs = long loadingPeriodBufferedPositionUs =
!loadingPeriod.prepared ? 0 : loadingPeriod.mediaPeriod.getBufferedPositionUs(); !loadingPeriod.prepared ? 0 : loadingPeriod.mediaPeriod.getBufferedPositionUs();
if (bufferedPositionUs == C.TIME_END_OF_SOURCE) { if (loadingPeriodBufferedPositionUs == C.TIME_END_OF_SOURCE) {
if (loadingPeriod.isLast) { if (loadingPeriod.isLast) {
return true; return true;
} }
bufferedPositionUs = timeline.getPeriodDurationUs(loadingPeriod.index); loadingPeriodBufferedPositionUs = timeline.getPeriodDurationUs(loadingPeriod.index);
} }
return loadControl.shouldStartPlayback(bufferedPositionUs - positionUs, rebuffering); return loadControl.shouldStartPlayback(
loadingPeriodBufferedPositionUs - loadingPeriodPositionUs, rebuffering);
} }
private void maybeThrowPeriodPrepareError() throws IOException { private void maybeThrowPeriodPrepareError() throws IOException {
@ -899,13 +896,13 @@ import java.io.IOException;
int windowIndex = timeline.getPeriodWindowIndex(periodIndex); int windowIndex = timeline.getPeriodWindowIndex(periodIndex);
periodIndex = timeline.getWindowFirstPeriodIndex(windowIndex); periodIndex = timeline.getWindowFirstPeriodIndex(windowIndex);
int maxPeriodIndex = timeline.getWindowLastPeriodIndex(windowIndex); int maxPeriodIndex = timeline.getWindowLastPeriodIndex(windowIndex);
long windowPositionUs = timeline.getWindowOffsetInFirstPeriodUs(windowIndex) long periodPositionUs = timeline.getWindowOffsetInFirstPeriodUs(windowIndex)
+ timeline.getWindow(windowIndex).defaultStartPositionUs; + timeline.getWindow(windowIndex).defaultStartPositionUs;
while (periodIndex < maxPeriodIndex while (periodIndex < maxPeriodIndex
&& windowPositionUs > timeline.getPeriodDurationUs(periodIndex)) { && periodPositionUs > timeline.getPeriodDurationUs(periodIndex)) {
windowPositionUs -= timeline.getPeriodDurationUs(periodIndex++); periodPositionUs -= timeline.getPeriodDurationUs(periodIndex++);
} }
return Pair.create(periodIndex, windowPositionUs); return Pair.create(periodIndex, periodPositionUs);
} }
private void updatePeriods() throws ExoPlaybackException, IOException { private void updatePeriods() throws ExoPlaybackException, IOException {
@ -924,24 +921,24 @@ import java.io.IOException;
// The period is not available yet. // The period is not available yet.
mediaSource.maybeThrowSourceInfoRefreshError(); mediaSource.maybeThrowSourceInfoRefreshError();
} else { } else {
long startPositionUs = loadingPeriod == null ? playbackInfo.positionUs long periodStartPositionUs = loadingPeriod == null ? playbackInfo.positionUs
: (newLoadingPeriodIndex == timeline.getWindowFirstPeriodIndex(newLoadingPeriodIndex) : (newLoadingPeriodIndex == timeline.getWindowFirstPeriodIndex(newLoadingPeriodIndex)
? C.TIME_UNSET : 0); ? C.TIME_UNSET : 0);
if (startPositionUs == C.TIME_UNSET) { if (periodStartPositionUs == C.TIME_UNSET) {
// This is the first period of a new window or we don't have a start position, so seek to // This is the first period of a new window or we don't have a start position, so seek to
// the default position for the window. // the default position for the window.
Pair<Integer, Long> defaultPosition = getDefaultPosition(newLoadingPeriodIndex); Pair<Integer, Long> defaultPosition = getDefaultPosition(newLoadingPeriodIndex);
newLoadingPeriodIndex = defaultPosition.first; newLoadingPeriodIndex = defaultPosition.first;
startPositionUs = defaultPosition.second; periodStartPositionUs = defaultPosition.second;
} }
MediaPeriod mediaPeriod = mediaSource.createPeriod(newLoadingPeriodIndex, this, MediaPeriod mediaPeriod = mediaSource.createPeriod(newLoadingPeriodIndex, this,
loadControl.getAllocator(), startPositionUs); loadControl.getAllocator(), periodStartPositionUs);
Period newPeriod = new Period(renderers, rendererCapabilities, trackSelector, mediaSource, Period newPeriod = new Period(renderers, rendererCapabilities, trackSelector, mediaSource,
mediaPeriod, timeline.getPeriodId(newLoadingPeriodIndex), startPositionUs); mediaPeriod, timeline.getPeriodId(newLoadingPeriodIndex), periodStartPositionUs);
newPeriod.setIndex(timeline, newLoadingPeriodIndex); newPeriod.setIndex(timeline, newLoadingPeriodIndex);
if (loadingPeriod != null) { if (loadingPeriod != null) {
loadingPeriod.setNextPeriod(newPeriod); loadingPeriod.setNextPeriod(newPeriod);
newPeriod.offsetUs = loadingPeriod.offsetUs newPeriod.rendererPositionOffsetUs = loadingPeriod.rendererPositionOffsetUs
+ timeline.getPeriodDurationUs(loadingPeriod.index); + timeline.getPeriodDurationUs(loadingPeriod.index);
} }
bufferAheadPeriodCount++; bufferAheadPeriodCount++;
@ -962,12 +959,8 @@ import java.io.IOException;
} }
// Update the playing and reading periods. // Update the playing and reading periods.
if (playingPeriodEndPositionUs == C.TIME_UNSET && playingPeriod.isFullyBuffered()) {
playingPeriodEndPositionUs = playingPeriod.offsetUs
+ timeline.getPeriodDurationUs(playingPeriod.index);
}
while (playingPeriod != readingPeriod && playingPeriod.nextPeriod != null while (playingPeriod != readingPeriod && playingPeriod.nextPeriod != null
&& internalPositionUs >= playingPeriod.nextPeriod.offsetUs) { && rendererPositionUs >= playingPeriod.nextPeriod.rendererPositionOffsetUs) {
// All enabled renderers' streams have been read to the end, and the playback position reached // 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. // the end of the playing period, so advance playback to the next period.
playingPeriod.release(); playingPeriod.release();
@ -1003,7 +996,8 @@ import java.io.IOException;
for (int j = 0; j < formats.length; j++) { for (int j = 0; j < formats.length; j++) {
formats[j] = newSelection.getFormat(j); formats[j] = newSelection.getFormat(j);
} }
renderer.replaceStream(formats, readingPeriod.sampleStreams[i], readingPeriod.offsetUs); renderer.replaceStream(formats, readingPeriod.sampleStreams[i],
readingPeriod.rendererPositionOffsetUs);
} else { } else {
// The renderer will be disabled when transitioning to playing the next period. Mark the // The renderer will be disabled when transitioning to playing the next period. Mark the
// SampleStream as final to play out any remaining data. // SampleStream as final to play out any remaining data.
@ -1032,7 +1026,7 @@ import java.io.IOException;
if (playbackInfo.startPositionUs == C.TIME_UNSET) { if (playbackInfo.startPositionUs == C.TIME_UNSET) {
// Update the playback info when seeking to a default position. // Update the playback info when seeking to a default position.
playbackInfo = new PlaybackInfo(playingPeriod.index, playingPeriod.startPositionUs); playbackInfo = new PlaybackInfo(playingPeriod.index, playingPeriod.startPositionUs);
resetInternalPosition(playbackInfo.startPositionUs); resetRendererPosition(playbackInfo.startPositionUs);
updatePlaybackPositions(); updatePlaybackPositions();
eventHandler.obtainMessage(MSG_POSITION_DISCONTINUITY, playbackInfo).sendToTarget(); eventHandler.obtainMessage(MSG_POSITION_DISCONTINUITY, playbackInfo).sendToTarget();
} }
@ -1051,13 +1045,14 @@ import java.io.IOException;
private void maybeContinueLoading() { private void maybeContinueLoading() {
long nextLoadPositionUs = loadingPeriod.mediaPeriod.getNextLoadPositionUs(); long nextLoadPositionUs = loadingPeriod.mediaPeriod.getNextLoadPositionUs();
if (nextLoadPositionUs != C.TIME_END_OF_SOURCE) { if (nextLoadPositionUs != C.TIME_END_OF_SOURCE) {
long positionUs = internalPositionUs - loadingPeriod.offsetUs + loadingPeriod.startPositionUs; long loadingPeriodPositionUs = rendererPositionUs - loadingPeriod.rendererPositionOffsetUs
long bufferedDurationUs = nextLoadPositionUs - positionUs; + loadingPeriod.startPositionUs;
long bufferedDurationUs = nextLoadPositionUs - loadingPeriodPositionUs;
boolean continueLoading = loadControl.shouldContinueLoading(bufferedDurationUs); boolean continueLoading = loadControl.shouldContinueLoading(bufferedDurationUs);
setIsLoading(continueLoading); setIsLoading(continueLoading);
if (continueLoading) { if (continueLoading) {
loadingPeriod.needsContinueLoading = false; loadingPeriod.needsContinueLoading = false;
loadingPeriod.mediaPeriod.continueLoading(positionUs); loadingPeriod.mediaPeriod.continueLoading(loadingPeriodPositionUs);
} else { } else {
loadingPeriod.needsContinueLoading = true; loadingPeriod.needsContinueLoading = true;
} }
@ -1098,13 +1093,13 @@ import java.io.IOException;
trackSelector.onSelectionActivated(period.trackSelectionData); trackSelector.onSelectionActivated(period.trackSelectionData);
playingPeriod = period; playingPeriod = period;
playingPeriodEndPositionUs = C.TIME_UNSET;
enableRenderers(rendererWasEnabledFlags, enabledRendererCount); enableRenderers(rendererWasEnabledFlags, enabledRendererCount);
} }
private void updateTimelineState() { private void updateTimelineState() {
isTimelineReady = playingPeriodEndPositionUs == C.TIME_UNSET long playingPeriodDurationUs = timeline.getPeriodDurationUs(playingPeriod.index);
|| internalPositionUs < playingPeriodEndPositionUs isTimelineReady = playingPeriodDurationUs == C.TIME_UNSET
|| playbackInfo.positionUs < playingPeriodDurationUs
|| (playingPeriod.nextPeriod != null && playingPeriod.nextPeriod.prepared); || (playingPeriod.nextPeriod != null && playingPeriod.nextPeriod.prepared);
isTimelineEnded = playingPeriod.isLast; isTimelineEnded = playingPeriod.isLast;
} }
@ -1129,8 +1124,8 @@ import java.io.IOException;
formats[j] = newSelection.getFormat(j); formats[j] = newSelection.getFormat(j);
} }
// Enable the renderer. // Enable the renderer.
renderer.enable(formats, playingPeriod.sampleStreams[i], internalPositionUs, joining, renderer.enable(formats, playingPeriod.sampleStreams[i], rendererPositionUs, joining,
playingPeriod.offsetUs); playingPeriod.rendererPositionOffsetUs);
MediaClock mediaClock = renderer.getMediaClock(); MediaClock mediaClock = renderer.getMediaClock();
if (mediaClock != null) { if (mediaClock != null) {
if (rendererMediaClock != null) { if (rendererMediaClock != null) {
@ -1165,7 +1160,7 @@ import java.io.IOException;
public boolean isLast; public boolean isLast;
public boolean prepared; public boolean prepared;
public boolean hasEnabledTracks; public boolean hasEnabledTracks;
public long offsetUs; public long rendererPositionOffsetUs;
public Period nextPeriod; public Period nextPeriod;
public boolean needsContinueLoading; public boolean needsContinueLoading;