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
This commit is contained in:
aquilescanta 2018-11-02 05:28:24 -07:00 committed by Oliver Woodman
parent b0555315cb
commit 32927bb62c
3 changed files with 24 additions and 4 deletions

View File

@ -27,8 +27,11 @@
* Fix handling of streams with appended data * Fix handling of streams with appended data
([#4954](https://github.com/google/ExoPlayer/issues/4954)). ([#4954](https://github.com/google/ExoPlayer/issues/4954)).
* DASH: Parse ProgramInformation element if present in the manifest. * DASH: Parse ProgramInformation element if present in the manifest.
* HLS: Add constructor to `DefaultHlsExtractorFactory` for adding TS payload * HLS:
reader factory flags * 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)). ([#4861](https://github.com/google/ExoPlayer/issues/4861)).
* SubRip: Add support for alignment tags, and remove tags from the displayed * SubRip: Add support for alignment tags, and remove tags from the displayed
captions ([#4306](https://github.com/google/ExoPlayer/issues/4306)). captions ([#4306](https://github.com/google/ExoPlayer/issues/4306)).

View File

@ -130,16 +130,16 @@ public final class DefaultExtractorInput implements ExtractorInput {
public boolean advancePeekPosition(int length, boolean allowEndOfInput) public boolean advancePeekPosition(int length, boolean allowEndOfInput)
throws IOException, InterruptedException { throws IOException, InterruptedException {
ensureSpaceForPeek(length); ensureSpaceForPeek(length);
int bytesPeeked = Math.min(peekBufferLength - peekBufferPosition, length); int bytesPeeked = peekBufferLength - peekBufferPosition;
while (bytesPeeked < length) { while (bytesPeeked < length) {
bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked, bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked,
allowEndOfInput); allowEndOfInput);
if (bytesPeeked == C.RESULT_END_OF_INPUT) { if (bytesPeeked == C.RESULT_END_OF_INPUT) {
return false; return false;
} }
peekBufferLength = peekBufferPosition + bytesPeeked;
} }
peekBufferPosition += length; peekBufferPosition += length;
peekBufferLength = Math.max(peekBufferLength, peekBufferPosition);
return true; return true;
} }

View File

@ -338,6 +338,23 @@ public class DefaultExtractorInputTest {
} }
} }
@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 @Test
public void testResetPeekPosition() throws Exception { public void testResetPeekPosition() throws Exception {
DefaultExtractorInput input = createDefaultExtractorInput(); DefaultExtractorInput input = createDefaultExtractorInput();