Allow chunk cancelation only if nothing has been consumed from it.

The mediaChunks.size() > 1 check was supposed to ensure this, and
did roughly the right thing when there was only a single stream
(although it was unnecessarily restrictive in preventing chunk
cancelation for the first chunk, where bytesLoaded != 0 and none
of the samples had been consumed).

Now we have multiple streams the check doesn't do the right thing,
and adding a back-buffer feature will make even more incorrect.
This change switches to checking the condition we actually want
to check directly :).

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=174449398
This commit is contained in:
olly 2017-11-03 05:09:43 -07:00 committed by Oliver Woodman
parent b6b09ad40b
commit d90d04190e

View file

@ -315,7 +315,7 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
IOException error) {
long bytesLoaded = loadable.bytesLoaded();
boolean isMediaChunk = isMediaChunk(loadable);
boolean cancelable = !isMediaChunk || bytesLoaded == 0 || mediaChunks.size() > 1;
boolean cancelable = bytesLoaded == 0 || !isMediaChunk || !haveReadFromLastMediaChunk();
boolean canceled = false;
if (chunkSource.onChunkLoadError(loadable, cancelable, error)) {
canceled = true;
@ -415,6 +415,22 @@ public class ChunkSampleStream<T extends ChunkSource> implements SampleStream, S
return chunk instanceof BaseMediaChunk;
}
/**
* Returns whether samples have been read from {@code mediaChunks.getLast()}.
*/
private boolean haveReadFromLastMediaChunk() {
BaseMediaChunk lastChunk = mediaChunks.getLast();
if (primarySampleQueue.getReadIndex() > lastChunk.getFirstSampleIndex(0)) {
return true;
}
for (int i = 0; i < embeddedSampleQueues.length; i++) {
if (embeddedSampleQueues[i].getReadIndex() > lastChunk.getFirstSampleIndex(i + 1)) {
return true;
}
}
return false;
}
/* package */ boolean isPendingReset() {
return pendingResetPositionUs != C.TIME_UNSET;
}