From 32927bb62ca8d59f8f6af22ae13520e8cb145f16 Mon Sep 17 00:00:00 2001 From: aquilescanta Date: Fri, 2 Nov 2018 05:28:24 -0700 Subject: [PATCH] Update the DefaultExtractorInput's peek buffer length on each write This prevents leaving an inconsistent state after a EOF exception. Issue:#5039 ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=219785275 --- RELEASENOTES.md | 7 +++++-- .../extractor/DefaultExtractorInput.java | 4 ++-- .../extractor/DefaultExtractorInputTest.java | 17 +++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index c61b1fecb49..569e2e555e7 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -27,8 +27,11 @@ * Fix handling of streams with appended data ([#4954](https://github.com/google/ExoPlayer/issues/4954)). * DASH: Parse ProgramInformation element if present in the manifest. -* HLS: Add constructor to `DefaultHlsExtractorFactory` for adding TS payload - reader factory flags +* HLS: + * Add constructor to `DefaultHlsExtractorFactory` for adding TS payload + reader factory flags + * Fix bug in segment sniffing + ([#5039](https://github.com/google/ExoPlayer/issues/5039)). ([#4861](https://github.com/google/ExoPlayer/issues/4861)). * SubRip: Add support for alignment tags, and remove tags from the displayed captions ([#4306](https://github.com/google/ExoPlayer/issues/4306)). diff --git a/library/core/src/main/java/com/google/android/exoplayer2/extractor/DefaultExtractorInput.java b/library/core/src/main/java/com/google/android/exoplayer2/extractor/DefaultExtractorInput.java index c3f63040916..450cca42b07 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/extractor/DefaultExtractorInput.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/extractor/DefaultExtractorInput.java @@ -130,16 +130,16 @@ public void peekFully(byte[] target, int offset, int length) public boolean advancePeekPosition(int length, boolean allowEndOfInput) throws IOException, InterruptedException { ensureSpaceForPeek(length); - int bytesPeeked = Math.min(peekBufferLength - peekBufferPosition, length); + int bytesPeeked = peekBufferLength - peekBufferPosition; while (bytesPeeked < length) { bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked, allowEndOfInput); if (bytesPeeked == C.RESULT_END_OF_INPUT) { return false; } + peekBufferLength = peekBufferPosition + bytesPeeked; } peekBufferPosition += length; - peekBufferLength = Math.max(peekBufferLength, peekBufferPosition); return true; } diff --git a/library/core/src/test/java/com/google/android/exoplayer2/extractor/DefaultExtractorInputTest.java b/library/core/src/test/java/com/google/android/exoplayer2/extractor/DefaultExtractorInputTest.java index a96dfaf2f88..8b263615784 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/extractor/DefaultExtractorInputTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/extractor/DefaultExtractorInputTest.java @@ -338,6 +338,23 @@ public void testPeekFully() throws Exception { } } + @Test + public void testPeekFullyAfterEofExceptionPeeksAsExpected() throws Exception { + DefaultExtractorInput input = createDefaultExtractorInput(); + byte[] target = new byte[TEST_DATA.length + 10]; + + try { + input.peekFully(target, /* offset= */ 0, target.length); + fail(); + } catch (EOFException expected) { + // Do nothing. Expected. + } + input.peekFully(target, /* offset= */ 0, /* length= */ TEST_DATA.length); + + assertThat(input.getPeekPosition()).isEqualTo(TEST_DATA.length); + assertThat(Arrays.equals(TEST_DATA, Arrays.copyOf(target, TEST_DATA.length))).isTrue(); + } + @Test public void testResetPeekPosition() throws Exception { DefaultExtractorInput input = createDefaultExtractorInput();