Commit 6607f49b by tonihei Committed by Oliver Woodman

Fix reporting of format changes in ChunkSampleStream.

Until recently, changing primary track formats were reported when the
corresponding media chunk was discarded which always happened immediately
after the sample has been read.

Now, media chunks may be discarded later on or in batches, leaving the
current reporting mechanism broken because changes may never be reported.

This fix separates the discarding from the reporting such that format changes
can be reported when the media chunk is first read from, while the discarding
operation only discards without reporting format changes.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=176519071
parent 15a1f9a5
...@@ -258,8 +258,12 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -258,8 +258,12 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
if (isPendingReset()) { if (isPendingReset()) {
return C.RESULT_NOTHING_READ; return C.RESULT_NOTHING_READ;
} }
return primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished, int result = primarySampleQueue.read(formatHolder, buffer, formatRequired, loadingFinished,
lastSeekPositionUs); lastSeekPositionUs);
if (result == C.RESULT_BUFFER_READ) {
maybeNotifyPrimaryTrackFormatChanged(primarySampleQueue.getReadIndex(), 1);
}
return result;
} }
@Override @Override
...@@ -274,6 +278,9 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -274,6 +278,9 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
skipCount = 0; skipCount = 0;
} }
} }
if (skipCount > 0) {
maybeNotifyPrimaryTrackFormatChanged(primarySampleQueue.getReadIndex(), skipCount);
}
return skipCount; return skipCount;
} }
...@@ -434,23 +441,48 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S ...@@ -434,23 +441,48 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
return pendingResetPositionUs != C.TIME_UNSET; return pendingResetPositionUs != C.TIME_UNSET;
} }
private void discardDownstreamMediaChunks(int primaryStreamReadIndex) { private void discardDownstreamMediaChunks(int discardToPrimaryStreamIndex) {
if (!mediaChunks.isEmpty()) { if (!mediaChunks.isEmpty()) {
while (mediaChunks.size() > 1 while (mediaChunks.size() > 1
&& mediaChunks.get(1).getFirstSampleIndex(0) <= primaryStreamReadIndex) { && mediaChunks.get(1).getFirstSampleIndex(0) <= discardToPrimaryStreamIndex) {
mediaChunks.removeFirst(); mediaChunks.removeFirst();
} }
BaseMediaChunk currentChunk = mediaChunks.getFirst(); }
Format trackFormat = currentChunk.trackFormat; }
if (!trackFormat.equals(primaryDownstreamTrackFormat)) {
eventDispatcher.downstreamFormatChanged(primaryTrackType, trackFormat, private void maybeNotifyPrimaryTrackFormatChanged(int toPrimaryStreamReadIndex, int readCount) {
currentChunk.trackSelectionReason, currentChunk.trackSelectionData, if (!mediaChunks.isEmpty()) {
currentChunk.startTimeUs); int fromPrimaryStreamReadIndex = toPrimaryStreamReadIndex - readCount;
int fromChunkIndex = 0;
while (fromChunkIndex < mediaChunks.size() - 1
&& mediaChunks.get(fromChunkIndex + 1).getFirstSampleIndex(0)
<= fromPrimaryStreamReadIndex) {
fromChunkIndex++;
}
int toChunkIndex = fromChunkIndex + 1;
if (readCount > 1) {
while (toChunkIndex < mediaChunks.size()
&& mediaChunks.get(toChunkIndex).getFirstSampleIndex(0) < toPrimaryStreamReadIndex) {
toChunkIndex++;
}
}
for (int i = fromChunkIndex; i < toChunkIndex; i++) {
maybeNotifyPrimaryTrackFormatChanged(i);
} }
primaryDownstreamTrackFormat = trackFormat;
} }
} }
private void maybeNotifyPrimaryTrackFormatChanged(int mediaChunkReadIndex) {
BaseMediaChunk currentChunk = mediaChunks.get(mediaChunkReadIndex);
Format trackFormat = currentChunk.trackFormat;
if (!trackFormat.equals(primaryDownstreamTrackFormat)) {
eventDispatcher.downstreamFormatChanged(primaryTrackType, trackFormat,
currentChunk.trackSelectionReason, currentChunk.trackSelectionData,
currentChunk.startTimeUs);
}
primaryDownstreamTrackFormat = trackFormat;
}
/** /**
* Discard upstream media chunks until the queue length is equal to the length specified. * Discard upstream media chunks until the queue length is equal to the length specified.
* *
......
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