Commit 1060c767 by olly Committed by Toni

Simplify FlacExtractor (step toward enabling nullness checking)

- Inline some unnecessarily split out helper methods
- Clear ExtractorInput from FlacDecoderJni data after usage
- Clean up exception handling for StreamInfo decode failures

PiperOrigin-RevId: 256524955
parent 0145edb6
...@@ -52,7 +52,7 @@ public final class FlacBinarySearchSeekerTest { ...@@ -52,7 +52,7 @@ public final class FlacBinarySearchSeekerTest {
FlacBinarySearchSeeker seeker = FlacBinarySearchSeeker seeker =
new FlacBinarySearchSeeker( new FlacBinarySearchSeeker(
decoderJni.decodeMetadata(), /* firstFramePosition= */ 0, data.length, decoderJni); decoderJni.decodeStreamInfo(), /* firstFramePosition= */ 0, data.length, decoderJni);
SeekMap seekMap = seeker.getSeekMap(); SeekMap seekMap = seeker.getSeekMap();
assertThat(seekMap).isNotNull(); assertThat(seekMap).isNotNull();
...@@ -70,7 +70,7 @@ public final class FlacBinarySearchSeekerTest { ...@@ -70,7 +70,7 @@ public final class FlacBinarySearchSeekerTest {
decoderJni.setData(input); decoderJni.setData(input);
FlacBinarySearchSeeker seeker = FlacBinarySearchSeeker seeker =
new FlacBinarySearchSeeker( new FlacBinarySearchSeeker(
decoderJni.decodeMetadata(), /* firstFramePosition= */ 0, data.length, decoderJni); decoderJni.decodeStreamInfo(), /* firstFramePosition= */ 0, data.length, decoderJni);
seeker.setSeekTargetUs(/* timeUs= */ 1000); seeker.setSeekTargetUs(/* timeUs= */ 1000);
assertThat(seeker.isSeeking()).isTrue(); assertThat(seeker.isSeeking()).isTrue();
......
...@@ -28,7 +28,7 @@ import org.junit.runner.RunWith; ...@@ -28,7 +28,7 @@ import org.junit.runner.RunWith;
public class FlacExtractorTest { public class FlacExtractorTest {
@Before @Before
public void setUp() throws Exception { public void setUp() {
if (!FlacLibrary.isAvailable()) { if (!FlacLibrary.isAvailable()) {
fail("Flac library not available."); fail("Flac library not available.");
} }
......
...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.ext.flac; ...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.ext.flac;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.decoder.DecoderInputBuffer; import com.google.android.exoplayer2.decoder.DecoderInputBuffer;
import com.google.android.exoplayer2.decoder.SimpleDecoder; import com.google.android.exoplayer2.decoder.SimpleDecoder;
import com.google.android.exoplayer2.decoder.SimpleOutputBuffer; import com.google.android.exoplayer2.decoder.SimpleOutputBuffer;
...@@ -59,14 +60,13 @@ import java.util.List; ...@@ -59,14 +60,13 @@ import java.util.List;
decoderJni.setData(ByteBuffer.wrap(initializationData.get(0))); decoderJni.setData(ByteBuffer.wrap(initializationData.get(0)));
FlacStreamInfo streamInfo; FlacStreamInfo streamInfo;
try { try {
streamInfo = decoderJni.decodeMetadata(); streamInfo = decoderJni.decodeStreamInfo();
} catch (ParserException e) {
throw new FlacDecoderException("Failed to decode StreamInfo", e);
} catch (IOException | InterruptedException e) { } catch (IOException | InterruptedException e) {
// Never happens. // Never happens.
throw new IllegalStateException(e); throw new IllegalStateException(e);
} }
if (streamInfo == null) {
throw new FlacDecoderException("Metadata decoding failed");
}
int initialInputBufferSize = int initialInputBufferSize =
maxInputBufferSize != Format.NO_VALUE ? maxInputBufferSize : streamInfo.maxFrameSize; maxInputBufferSize != Format.NO_VALUE ? maxInputBufferSize : streamInfo.maxFrameSize;
......
...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.ext.flac; ...@@ -17,6 +17,7 @@ package com.google.android.exoplayer2.ext.flac;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.extractor.ExtractorInput; import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.util.FlacStreamInfo; import com.google.android.exoplayer2.util.FlacStreamInfo;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
...@@ -59,38 +60,47 @@ import java.nio.ByteBuffer; ...@@ -59,38 +60,47 @@ import java.nio.ByteBuffer;
} }
/** /**
* Sets data to be parsed by libflac. * Sets the data to be parsed.
* *
* @param byteBufferData Source {@link ByteBuffer}. * @param byteBufferData Source {@link ByteBuffer}.
*/ */
public void setData(ByteBuffer byteBufferData) { public void setData(ByteBuffer byteBufferData) {
this.byteBufferData = byteBufferData; this.byteBufferData = byteBufferData;
this.extractorInput = null; this.extractorInput = null;
this.tempBuffer = null;
} }
/** /**
* Sets data to be parsed by libflac. * Sets the data to be parsed.
* *
* @param extractorInput Source {@link ExtractorInput}. * @param extractorInput Source {@link ExtractorInput}.
*/ */
public void setData(ExtractorInput extractorInput) { public void setData(ExtractorInput extractorInput) {
this.byteBufferData = null; this.byteBufferData = null;
this.extractorInput = extractorInput; this.extractorInput = extractorInput;
endOfExtractorInput = false;
if (tempBuffer == null) { if (tempBuffer == null) {
this.tempBuffer = new byte[TEMP_BUFFER_SIZE]; tempBuffer = new byte[TEMP_BUFFER_SIZE];
} }
endOfExtractorInput = false;
} }
/**
* Returns whether the end of the data to be parsed has been reached, or true if no data was set.
*/
public boolean isEndOfData() { public boolean isEndOfData() {
if (byteBufferData != null) { if (byteBufferData != null) {
return byteBufferData.remaining() == 0; return byteBufferData.remaining() == 0;
} else if (extractorInput != null) { } else if (extractorInput != null) {
return endOfExtractorInput; return endOfExtractorInput;
} } else {
return true; return true;
} }
}
/** Clears the data to be parsed. */
public void clearData() {
byteBufferData = null;
extractorInput = null;
}
/** /**
* Reads up to {@code length} bytes from the data source. * Reads up to {@code length} bytes from the data source.
...@@ -98,12 +108,11 @@ import java.nio.ByteBuffer; ...@@ -98,12 +108,11 @@ import java.nio.ByteBuffer;
* <p>This method blocks until at least one byte of data can be read, the end of the input is * <p>This method blocks until at least one byte of data can be read, the end of the input is
* detected or an exception is thrown. * detected or an exception is thrown.
* *
* <p>This method is called from the native code.
*
* @param target A target {@link ByteBuffer} into which data should be written. * @param target A target {@link ByteBuffer} into which data should be written.
* @return Returns the number of bytes read, or -1 on failure. If all of the data has already been * @return Returns the number of bytes read, or -1 on failure. If all of the data has already been
* read from the source, then 0 is returned. * read from the source, then 0 is returned.
*/ */
@SuppressWarnings("unused") // Called from native code.
public int read(ByteBuffer target) throws IOException, InterruptedException { public int read(ByteBuffer target) throws IOException, InterruptedException {
int byteCount = target.remaining(); int byteCount = target.remaining();
if (byteBufferData != null) { if (byteBufferData != null) {
...@@ -134,8 +143,12 @@ import java.nio.ByteBuffer; ...@@ -134,8 +143,12 @@ import java.nio.ByteBuffer;
} }
/** Decodes and consumes the StreamInfo section from the FLAC stream. */ /** Decodes and consumes the StreamInfo section from the FLAC stream. */
public FlacStreamInfo decodeMetadata() throws IOException, InterruptedException { public FlacStreamInfo decodeStreamInfo() throws IOException, InterruptedException {
return flacDecodeMetadata(nativeDecoderContext); FlacStreamInfo streamInfo = flacDecodeMetadata(nativeDecoderContext);
if (streamInfo == null) {
throw new ParserException("Failed to decode StreamInfo");
}
return streamInfo;
} }
/** /**
......
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