From d90d04190e3643435fd4b4a3515cbd9fbe784cc1 Mon Sep 17 00:00:00 2001 From: olly Date: Fri, 3 Nov 2017 05:09:43 -0700 Subject: [PATCH] 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 --- .../source/chunk/ChunkSampleStream.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java index 9d5a405c2f..b64dec59bf 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/chunk/ChunkSampleStream.java @@ -315,7 +315,7 @@ public class ChunkSampleStream 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 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; }