Commit 047e0eb6 by olly Committed by Oliver Woodman

Renames to prepare for upcoming media buffer changes

Currently, media is discarded from DefaultTrackOutput
and SampleMetadataQueue as soon as it's been read. In
upcoming changes we'll decouple discard and read. This
will make it possible to retain already-read media in
these buffer classes, and allow the read position to
be moved backward as far as media is retained. This is
important for fixing an edge case around 608/EMSG
tracks, and could also underpin future features like
allowing retaining of X-seconds past media in the
buffer.

This change renames some variables and methods to
prepare for the upcoming changes. read/write indices
are renamed to start/end. The upcoming changes will
add a read index that's between the two. isEmpty is
inverted and renamed to hasNextSample, since it will
be possible to not have a next sample (because the
read index == end index) but for the buffer to not
be empty (because start index < read index).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=158409630
parent b7b0fef6
...@@ -180,10 +180,10 @@ public final class DefaultTrackOutput implements TrackOutput { ...@@ -180,10 +180,10 @@ public final class DefaultTrackOutput implements TrackOutput {
} }
/** /**
* Returns whether the buffer is empty. * Returns whether a sample is available to be read.
*/ */
public boolean isEmpty() { public boolean hasNextSample() {
return metadataQueue.isEmpty(); return metadataQueue.hasNextSample();
} }
/** /**
......
...@@ -51,10 +51,10 @@ import com.google.android.exoplayer2.util.Util; ...@@ -51,10 +51,10 @@ import com.google.android.exoplayer2.util.Util;
private CryptoData[] cryptoDatas; private CryptoData[] cryptoDatas;
private Format[] formats; private Format[] formats;
private int queueSize; private int length;
private int absoluteReadIndex; private int absoluteStartIndex;
private int relativeReadIndex; private int relativeStartIndex;
private int relativeWriteIndex; private int relativeEndIndex;
private long largestDequeuedTimestampUs; private long largestDequeuedTimestampUs;
private long largestQueuedTimestampUs; private long largestQueuedTimestampUs;
...@@ -79,10 +79,10 @@ import com.google.android.exoplayer2.util.Util; ...@@ -79,10 +79,10 @@ import com.google.android.exoplayer2.util.Util;
} }
public void clearSampleData() { public void clearSampleData() {
absoluteReadIndex = 0; absoluteStartIndex = 0;
relativeReadIndex = 0; relativeStartIndex = 0;
relativeWriteIndex = 0; relativeEndIndex = 0;
queueSize = 0; length = 0;
upstreamKeyframeRequired = true; upstreamKeyframeRequired = true;
} }
...@@ -97,7 +97,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -97,7 +97,7 @@ import com.google.android.exoplayer2.util.Util;
* Returns the current absolute write index. * Returns the current absolute write index.
*/ */
public int getWriteIndex() { public int getWriteIndex() {
return absoluteReadIndex + queueSize; return absoluteStartIndex + length;
} }
/** /**
...@@ -108,30 +108,30 @@ import com.google.android.exoplayer2.util.Util; ...@@ -108,30 +108,30 @@ import com.google.android.exoplayer2.util.Util;
*/ */
public long discardUpstreamSamples(int discardFromIndex) { public long discardUpstreamSamples(int discardFromIndex) {
int discardCount = getWriteIndex() - discardFromIndex; int discardCount = getWriteIndex() - discardFromIndex;
Assertions.checkArgument(0 <= discardCount && discardCount <= queueSize); Assertions.checkArgument(0 <= discardCount && discardCount <= length);
if (discardCount == 0) { if (discardCount == 0) {
if (absoluteReadIndex == 0) { if (absoluteStartIndex == 0) {
// queueSize == absoluteReadIndex == 0, so nothing has been written to the queue. // length == absoluteStartIndex == 0, so nothing has been written to the queue.
return 0; return 0;
} }
int lastWriteIndex = (relativeWriteIndex == 0 ? capacity : relativeWriteIndex) - 1; int lastWriteIndex = (relativeEndIndex == 0 ? capacity : relativeEndIndex) - 1;
return offsets[lastWriteIndex] + sizes[lastWriteIndex]; return offsets[lastWriteIndex] + sizes[lastWriteIndex];
} }
queueSize -= discardCount; length -= discardCount;
relativeWriteIndex = (relativeWriteIndex + capacity - discardCount) % capacity; relativeEndIndex = (relativeEndIndex + capacity - discardCount) % capacity;
// Update the largest queued timestamp, assuming that the timestamps prior to a keyframe are // Update the largest queued timestamp, assuming that the timestamps prior to a keyframe are
// always less than the timestamp of the keyframe itself, and of subsequent frames. // always less than the timestamp of the keyframe itself, and of subsequent frames.
largestQueuedTimestampUs = Long.MIN_VALUE; largestQueuedTimestampUs = Long.MIN_VALUE;
for (int i = queueSize - 1; i >= 0; i--) { for (int i = length - 1; i >= 0; i--) {
int sampleIndex = (relativeReadIndex + i) % capacity; int sampleIndex = (relativeStartIndex + i) % capacity;
largestQueuedTimestampUs = Math.max(largestQueuedTimestampUs, timesUs[sampleIndex]); largestQueuedTimestampUs = Math.max(largestQueuedTimestampUs, timesUs[sampleIndex]);
if ((flags[sampleIndex] & C.BUFFER_FLAG_KEY_FRAME) != 0) { if ((flags[sampleIndex] & C.BUFFER_FLAG_KEY_FRAME) != 0) {
break; break;
} }
} }
return offsets[relativeWriteIndex]; return offsets[relativeEndIndex];
} }
public void sourceId(int sourceId) { public void sourceId(int sourceId) {
...@@ -144,22 +144,22 @@ import com.google.android.exoplayer2.util.Util; ...@@ -144,22 +144,22 @@ import com.google.android.exoplayer2.util.Util;
* Returns the current absolute read index. * Returns the current absolute read index.
*/ */
public int getReadIndex() { public int getReadIndex() {
return absoluteReadIndex; return absoluteStartIndex;
} }
/** /**
* Peeks the source id of the next sample, or the current upstream source id if the queue is * Peeks the source id of the next sample, or the current upstream source id if
* empty. * {@link #hasNextSample()} is {@code false}.
*/ */
public int peekSourceId() { public int peekSourceId() {
return queueSize == 0 ? upstreamSourceId : sourceIds[relativeReadIndex]; return hasNextSample() ? sourceIds[relativeStartIndex] : upstreamSourceId;
} }
/** /**
* Returns whether the queue is empty. * Returns whether a sample is available to be read.
*/ */
public synchronized boolean isEmpty() { public synchronized boolean hasNextSample() {
return queueSize == 0; return length != 0;
} }
/** /**
...@@ -209,7 +209,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -209,7 +209,7 @@ import com.google.android.exoplayer2.util.Util;
public synchronized int readData(FormatHolder formatHolder, DecoderInputBuffer buffer, public synchronized int readData(FormatHolder formatHolder, DecoderInputBuffer buffer,
boolean formatRequired, boolean loadingFinished, Format downstreamFormat, boolean formatRequired, boolean loadingFinished, Format downstreamFormat,
SampleExtrasHolder extrasHolder) { SampleExtrasHolder extrasHolder) {
if (queueSize == 0) { if (!hasNextSample()) {
if (loadingFinished) { if (loadingFinished) {
buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM); buffer.setFlags(C.BUFFER_FLAG_END_OF_STREAM);
return C.RESULT_BUFFER_READ; return C.RESULT_BUFFER_READ;
...@@ -222,8 +222,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -222,8 +222,8 @@ import com.google.android.exoplayer2.util.Util;
} }
} }
if (formatRequired || formats[relativeReadIndex] != downstreamFormat) { if (formatRequired || formats[relativeStartIndex] != downstreamFormat) {
formatHolder.format = formats[relativeReadIndex]; formatHolder.format = formats[relativeStartIndex];
return C.RESULT_FORMAT_READ; return C.RESULT_FORMAT_READ;
} }
...@@ -231,22 +231,22 @@ import com.google.android.exoplayer2.util.Util; ...@@ -231,22 +231,22 @@ import com.google.android.exoplayer2.util.Util;
return C.RESULT_NOTHING_READ; return C.RESULT_NOTHING_READ;
} }
buffer.timeUs = timesUs[relativeReadIndex]; buffer.timeUs = timesUs[relativeStartIndex];
buffer.setFlags(flags[relativeReadIndex]); buffer.setFlags(flags[relativeStartIndex]);
extrasHolder.size = sizes[relativeReadIndex]; extrasHolder.size = sizes[relativeStartIndex];
extrasHolder.offset = offsets[relativeReadIndex]; extrasHolder.offset = offsets[relativeStartIndex];
extrasHolder.cryptoData = cryptoDatas[relativeReadIndex]; extrasHolder.cryptoData = cryptoDatas[relativeStartIndex];
largestDequeuedTimestampUs = Math.max(largestDequeuedTimestampUs, buffer.timeUs); largestDequeuedTimestampUs = Math.max(largestDequeuedTimestampUs, buffer.timeUs);
queueSize--; length--;
relativeReadIndex++; relativeStartIndex++;
absoluteReadIndex++; absoluteStartIndex++;
if (relativeReadIndex == capacity) { if (relativeStartIndex == capacity) {
// Wrap around. // Wrap around.
relativeReadIndex = 0; relativeStartIndex = 0;
} }
extrasHolder.nextOffset = queueSize > 0 ? offsets[relativeReadIndex] extrasHolder.nextOffset = length > 0 ? offsets[relativeStartIndex]
: extrasHolder.offset + extrasHolder.size; : extrasHolder.offset + extrasHolder.size;
return C.RESULT_BUFFER_READ; return C.RESULT_BUFFER_READ;
} }
...@@ -258,14 +258,14 @@ import com.google.android.exoplayer2.util.Util; ...@@ -258,14 +258,14 @@ import com.google.android.exoplayer2.util.Util;
* dropping of data is required. * dropping of data is required.
*/ */
public synchronized long skipAll() { public synchronized long skipAll() {
if (queueSize == 0) { if (!hasNextSample()) {
return C.POSITION_UNSET; return C.POSITION_UNSET;
} }
int lastSampleIndex = (relativeReadIndex + queueSize - 1) % capacity; int lastSampleIndex = (relativeStartIndex + length - 1) % capacity;
relativeReadIndex = (relativeReadIndex + queueSize) % capacity; relativeStartIndex = (relativeStartIndex + length) % capacity;
absoluteReadIndex += queueSize; absoluteStartIndex += length;
queueSize = 0; length = 0;
return offsets[lastSampleIndex] + sizes[lastSampleIndex]; return offsets[lastSampleIndex] + sizes[lastSampleIndex];
} }
...@@ -281,7 +281,7 @@ import com.google.android.exoplayer2.util.Util; ...@@ -281,7 +281,7 @@ import com.google.android.exoplayer2.util.Util;
* {@link C#POSITION_UNSET} otherwise. * {@link C#POSITION_UNSET} otherwise.
*/ */
public synchronized long skipToKeyframeBefore(long timeUs, boolean allowTimeBeyondBuffer) { public synchronized long skipToKeyframeBefore(long timeUs, boolean allowTimeBeyondBuffer) {
if (queueSize == 0 || timeUs < timesUs[relativeReadIndex]) { if (!hasNextSample() || timeUs < timesUs[relativeStartIndex]) {
return C.POSITION_UNSET; return C.POSITION_UNSET;
} }
...@@ -294,8 +294,8 @@ import com.google.android.exoplayer2.util.Util; ...@@ -294,8 +294,8 @@ import com.google.android.exoplayer2.util.Util;
// a binary search would yield any real benefit. // a binary search would yield any real benefit.
int sampleCount = 0; int sampleCount = 0;
int sampleCountToKeyframe = -1; int sampleCountToKeyframe = -1;
int searchIndex = relativeReadIndex; int searchIndex = relativeStartIndex;
while (searchIndex != relativeWriteIndex) { while (searchIndex != relativeEndIndex) {
if (timesUs[searchIndex] > timeUs) { if (timesUs[searchIndex] > timeUs) {
// We've gone too far. // We've gone too far.
break; break;
...@@ -311,10 +311,10 @@ import com.google.android.exoplayer2.util.Util; ...@@ -311,10 +311,10 @@ import com.google.android.exoplayer2.util.Util;
return C.POSITION_UNSET; return C.POSITION_UNSET;
} }
relativeReadIndex = (relativeReadIndex + sampleCountToKeyframe) % capacity; relativeStartIndex = (relativeStartIndex + sampleCountToKeyframe) % capacity;
absoluteReadIndex += sampleCountToKeyframe; absoluteStartIndex += sampleCountToKeyframe;
queueSize -= sampleCountToKeyframe; length -= sampleCountToKeyframe;
return offsets[relativeReadIndex]; return offsets[relativeStartIndex];
} }
// Called by the loading thread. // Called by the loading thread.
...@@ -344,16 +344,16 @@ import com.google.android.exoplayer2.util.Util; ...@@ -344,16 +344,16 @@ import com.google.android.exoplayer2.util.Util;
} }
Assertions.checkState(!upstreamFormatRequired); Assertions.checkState(!upstreamFormatRequired);
commitSampleTimestamp(timeUs); commitSampleTimestamp(timeUs);
timesUs[relativeWriteIndex] = timeUs; timesUs[relativeEndIndex] = timeUs;
offsets[relativeWriteIndex] = offset; offsets[relativeEndIndex] = offset;
sizes[relativeWriteIndex] = size; sizes[relativeEndIndex] = size;
flags[relativeWriteIndex] = sampleFlags; flags[relativeEndIndex] = sampleFlags;
cryptoDatas[relativeWriteIndex] = cryptoData; cryptoDatas[relativeEndIndex] = cryptoData;
formats[relativeWriteIndex] = upstreamFormat; formats[relativeEndIndex] = upstreamFormat;
sourceIds[relativeWriteIndex] = upstreamSourceId; sourceIds[relativeEndIndex] = upstreamSourceId;
// Increment the write index. // Increment the write index.
queueSize++; length++;
if (queueSize == capacity) { if (length == capacity) {
// Increase the capacity. // Increase the capacity.
int newCapacity = capacity + SAMPLE_CAPACITY_INCREMENT; int newCapacity = capacity + SAMPLE_CAPACITY_INCREMENT;
int[] newSourceIds = new int[newCapacity]; int[] newSourceIds = new int[newCapacity];
...@@ -363,15 +363,15 @@ import com.google.android.exoplayer2.util.Util; ...@@ -363,15 +363,15 @@ import com.google.android.exoplayer2.util.Util;
int[] newSizes = new int[newCapacity]; int[] newSizes = new int[newCapacity];
CryptoData[] newCryptoDatas = new CryptoData[newCapacity]; CryptoData[] newCryptoDatas = new CryptoData[newCapacity];
Format[] newFormats = new Format[newCapacity]; Format[] newFormats = new Format[newCapacity];
int beforeWrap = capacity - relativeReadIndex; int beforeWrap = capacity - relativeStartIndex;
System.arraycopy(offsets, relativeReadIndex, newOffsets, 0, beforeWrap); System.arraycopy(offsets, relativeStartIndex, newOffsets, 0, beforeWrap);
System.arraycopy(timesUs, relativeReadIndex, newTimesUs, 0, beforeWrap); System.arraycopy(timesUs, relativeStartIndex, newTimesUs, 0, beforeWrap);
System.arraycopy(flags, relativeReadIndex, newFlags, 0, beforeWrap); System.arraycopy(flags, relativeStartIndex, newFlags, 0, beforeWrap);
System.arraycopy(sizes, relativeReadIndex, newSizes, 0, beforeWrap); System.arraycopy(sizes, relativeStartIndex, newSizes, 0, beforeWrap);
System.arraycopy(cryptoDatas, relativeReadIndex, newCryptoDatas, 0, beforeWrap); System.arraycopy(cryptoDatas, relativeStartIndex, newCryptoDatas, 0, beforeWrap);
System.arraycopy(formats, relativeReadIndex, newFormats, 0, beforeWrap); System.arraycopy(formats, relativeStartIndex, newFormats, 0, beforeWrap);
System.arraycopy(sourceIds, relativeReadIndex, newSourceIds, 0, beforeWrap); System.arraycopy(sourceIds, relativeStartIndex, newSourceIds, 0, beforeWrap);
int afterWrap = relativeReadIndex; int afterWrap = relativeStartIndex;
System.arraycopy(offsets, 0, newOffsets, beforeWrap, afterWrap); System.arraycopy(offsets, 0, newOffsets, beforeWrap, afterWrap);
System.arraycopy(timesUs, 0, newTimesUs, beforeWrap, afterWrap); System.arraycopy(timesUs, 0, newTimesUs, beforeWrap, afterWrap);
System.arraycopy(flags, 0, newFlags, beforeWrap, afterWrap); System.arraycopy(flags, 0, newFlags, beforeWrap, afterWrap);
...@@ -386,15 +386,15 @@ import com.google.android.exoplayer2.util.Util; ...@@ -386,15 +386,15 @@ import com.google.android.exoplayer2.util.Util;
cryptoDatas = newCryptoDatas; cryptoDatas = newCryptoDatas;
formats = newFormats; formats = newFormats;
sourceIds = newSourceIds; sourceIds = newSourceIds;
relativeReadIndex = 0; relativeStartIndex = 0;
relativeWriteIndex = capacity; relativeEndIndex = capacity;
queueSize = capacity; length = capacity;
capacity = newCapacity; capacity = newCapacity;
} else { } else {
relativeWriteIndex++; relativeEndIndex++;
if (relativeWriteIndex == capacity) { if (relativeEndIndex == capacity) {
// Wrap around. // Wrap around.
relativeWriteIndex = 0; relativeEndIndex = 0;
} }
} }
} }
...@@ -414,12 +414,12 @@ import com.google.android.exoplayer2.util.Util; ...@@ -414,12 +414,12 @@ import com.google.android.exoplayer2.util.Util;
if (largestDequeuedTimestampUs >= timeUs) { if (largestDequeuedTimestampUs >= timeUs) {
return false; return false;
} }
int retainCount = queueSize; int retainCount = length;
while (retainCount > 0 while (retainCount > 0
&& timesUs[(relativeReadIndex + retainCount - 1) % capacity] >= timeUs) { && timesUs[(relativeStartIndex + retainCount - 1) % capacity] >= timeUs) {
retainCount--; retainCount--;
} }
discardUpstreamSamples(absoluteReadIndex + retainCount); discardUpstreamSamples(absoluteStartIndex + retainCount);
return true; return true;
} }
......
...@@ -323,7 +323,7 @@ import java.io.IOException; ...@@ -323,7 +323,7 @@ import java.io.IOException;
// SampleStream methods. // SampleStream methods.
/* package */ boolean isReady(int track) { /* package */ boolean isReady(int track) {
return loadingFinished || (!isPendingReset() && !sampleQueues.valueAt(track).isEmpty()); return loadingFinished || (!isPendingReset() && sampleQueues.valueAt(track).hasNextSample());
} }
/* package */ void maybeThrowError() throws IOException { /* package */ void maybeThrowError() throws IOException {
......
...@@ -228,7 +228,7 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -228,7 +228,7 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
@Override @Override
public boolean isReady() { public boolean isReady() {
return loadingFinished || (!isPendingReset() && !primarySampleQueue.isEmpty()); return loadingFinished || (!isPendingReset() && primarySampleQueue.hasNextSample());
} }
@Override @Override
...@@ -451,7 +451,7 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -451,7 +451,7 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
@Override @Override
public boolean isReady() { public boolean isReady() {
return loadingFinished || (!isPendingReset() && !sampleQueue.isEmpty()); return loadingFinished || (!isPendingReset() && sampleQueue.hasNextSample());
} }
@Override @Override
......
...@@ -282,7 +282,7 @@ import java.util.LinkedList; ...@@ -282,7 +282,7 @@ import java.util.LinkedList;
// SampleStream implementation. // SampleStream implementation.
/* package */ boolean isReady(int group) { /* package */ boolean isReady(int group) {
return loadingFinished || (!isPendingReset() && !sampleQueues.valueAt(group).isEmpty()); return loadingFinished || (!isPendingReset() && sampleQueues.valueAt(group).hasNextSample());
} }
/* package */ void maybeThrowError() throws IOException { /* package */ void maybeThrowError() throws IOException {
......
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