Commit 1f202f0a by aquilescanta Committed by Oliver Woodman

Make DataSource extend DataReader and upcast uses

This means DataSource.read now may throw InterruptedException.

PiperOrigin-RevId: 300523430
parent 93e203f9
...@@ -23,6 +23,11 @@ public interface DataReader { ...@@ -23,6 +23,11 @@ public interface DataReader {
/** /**
* Reads up to {@code length} bytes of data from the input. * Reads up to {@code length} bytes of data from the input.
* *
* <p>If {@code readLength} is zero then 0 is returned. Otherwise, if no data is available because
* the end of the opened range has been reached, then {@link C#RESULT_END_OF_INPUT} is returned.
* Otherwise, the call will block until at least one byte of data has been read and the number of
* bytes read is returned.
*
* @param target A target array into which data should be written. * @param target A target array into which data should be written.
* @param offset The offset into the target array at which to write. * @param offset The offset into the target array at which to write.
* @param length The maximum number of bytes to read from the input. * @param length The maximum number of bytes to read from the input.
......
...@@ -23,10 +23,8 @@ import java.util.Collections; ...@@ -23,10 +23,8 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
/** /** Reads data from URI-identified resources. */
* A component from which streams of data can be read. public interface DataSource extends DataReader {
*/
public interface DataSource {
/** /**
* A factory for {@link DataSource} instances. * A factory for {@link DataSource} instances.
...@@ -64,24 +62,6 @@ public interface DataSource { ...@@ -64,24 +62,6 @@ public interface DataSource {
long open(DataSpec dataSpec) throws IOException; long open(DataSpec dataSpec) throws IOException;
/** /**
* Reads up to {@code readLength} bytes of data and stores them into {@code buffer}, starting at
* index {@code offset}.
*
* <p>If {@code readLength} is zero then 0 is returned. Otherwise, if no data is available because
* the end of the opened range has been reached, then {@link C#RESULT_END_OF_INPUT} is returned.
* Otherwise, the call will block until at least one byte of data has been read and the number of
* bytes read is returned.
*
* @param buffer The buffer into which the read data should be stored.
* @param offset The start offset into {@code buffer} at which data should be written.
* @param readLength The maximum number of bytes to read.
* @return The number of bytes read, or {@link C#RESULT_END_OF_INPUT} if no data is available
* because the end of the opened range has been reached.
* @throws IOException If an error occurs reading from the source.
*/
int read(byte[] buffer, int offset, int readLength) throws IOException;
/**
* When the source is open, returns the {@link Uri} from which data is being read. The returned * When the source is open, returns the {@link Uri} from which data is being read. The returned
* {@link Uri} will be identical to the one passed {@link #open(DataSpec)} in the {@link DataSpec} * {@link Uri} will be identical to the one passed {@link #open(DataSpec)} in the {@link DataSpec}
* unless redirection has occurred. If redirection has occurred, the {@link Uri} after redirection * unless redirection has occurred. If redirection has occurred, the {@link Uri} after redirection
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import android.net.Uri;
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.extractor.DefaultExtractorInput; import com.google.android.exoplayer2.extractor.DefaultExtractorInput;
...@@ -23,7 +24,7 @@ import com.google.android.exoplayer2.extractor.ExtractorInput; ...@@ -23,7 +24,7 @@ import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.extractor.ExtractorOutput; import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.PositionHolder; import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor; import com.google.android.exoplayer2.extractor.mp3.Mp3Extractor;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataReader;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.EOFException; import java.io.EOFException;
...@@ -50,9 +51,10 @@ import java.io.IOException; ...@@ -50,9 +51,10 @@ import java.io.IOException;
} }
@Override @Override
public void init(DataSource dataSource, long position, long length, ExtractorOutput output) public void init(
DataReader dataReader, Uri uri, long position, long length, ExtractorOutput output)
throws IOException { throws IOException {
extractorInput = new DefaultExtractorInput(dataSource, position, length); extractorInput = new DefaultExtractorInput(dataReader, position, length);
if (extractor != null) { if (extractor != null) {
return; return;
} }
...@@ -76,7 +78,7 @@ import java.io.IOException; ...@@ -76,7 +78,7 @@ import java.io.IOException;
"None of the available extractors (" "None of the available extractors ("
+ Util.getCommaDelimitedSimpleClassNames(extractors) + Util.getCommaDelimitedSimpleClassNames(extractors)
+ ") could read the stream.", + ") could read the stream.",
Assertions.checkNotNull(dataSource.getUri())); Assertions.checkNotNull(uri));
} }
} }
extractor.init(output); extractor.init(output);
......
...@@ -15,11 +15,12 @@ ...@@ -15,11 +15,12 @@
*/ */
package com.google.android.exoplayer2.source; package com.google.android.exoplayer2.source;
import android.net.Uri;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.extractor.Extractor; import com.google.android.exoplayer2.extractor.Extractor;
import com.google.android.exoplayer2.extractor.ExtractorOutput; import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.PositionHolder; import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataReader;
import java.io.IOException; import java.io.IOException;
/** Extracts the contents of a container file from a progressive media stream. */ /** Extracts the contents of a container file from a progressive media stream. */
...@@ -28,15 +29,16 @@ import java.io.IOException; ...@@ -28,15 +29,16 @@ import java.io.IOException;
/** /**
* Initializes the underlying infrastructure for reading from the input. * Initializes the underlying infrastructure for reading from the input.
* *
* @param dataSource The {@link DataSource} from which data should be read. * @param dataReader The {@link DataReader} from which data should be read.
* @param position The initial position of the {@code dataSource} in the stream. * @param uri The {@link Uri} from which the media is obtained.
* @param position The initial position of the {@code dataReader} in the stream.
* @param length The length of the stream, or {@link C#LENGTH_UNSET} if length is unknown. * @param length The length of the stream, or {@link C#LENGTH_UNSET} if length is unknown.
* @param output The {@link ExtractorOutput} that will be used to initialize the selected * @param output The {@link ExtractorOutput} that will be used to initialize the selected
* extractor. * extractor.
* @throws UnrecognizedInputFormatException Thrown if the input format could not be detected. * @throws UnrecognizedInputFormatException Thrown if the input format could not be detected.
* @throws IOException Thrown if the input could not be read. * @throws IOException Thrown if the input could not be read.
*/ */
void init(DataSource dataSource, long position, long length, ExtractorOutput output) void init(DataReader dataReader, Uri uri, long position, long length, ExtractorOutput output)
throws IOException; throws IOException;
/** Releases any held resources. */ /** Releases any held resources. */
......
...@@ -976,7 +976,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -976,7 +976,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
icyTrackOutput = icyTrack(); icyTrackOutput = icyTrack();
icyTrackOutput.format(ICY_FORMAT); icyTrackOutput.format(ICY_FORMAT);
} }
progressiveMediaExtractor.init(extractorDataSource, position, length, extractorOutput); progressiveMediaExtractor.init(
extractorDataSource, uri, position, length, extractorOutput);
if (icyHeaders != null) { if (icyHeaders != null) {
progressiveMediaExtractor.disableSeekingOnMp3Streams(); progressiveMediaExtractor.disableSeekingOnMp3Streams();
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package com.google.android.exoplayer2.extractor; package com.google.android.exoplayer2.extractor;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataReader;
import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.io.EOFException; import java.io.EOFException;
...@@ -24,9 +24,7 @@ import java.io.IOException; ...@@ -24,9 +24,7 @@ import java.io.IOException;
import java.io.InterruptedIOException; import java.io.InterruptedIOException;
import java.util.Arrays; import java.util.Arrays;
/** /** An {@link ExtractorInput} that wraps a {@link DataReader}. */
* An {@link ExtractorInput} that wraps a {@link DataSource}.
*/
public final class DefaultExtractorInput implements ExtractorInput { public final class DefaultExtractorInput implements ExtractorInput {
private static final int PEEK_MIN_FREE_SPACE_AFTER_RESIZE = 64 * 1024; private static final int PEEK_MIN_FREE_SPACE_AFTER_RESIZE = 64 * 1024;
...@@ -34,7 +32,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -34,7 +32,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
private static final int SCRATCH_SPACE_SIZE = 4096; private static final int SCRATCH_SPACE_SIZE = 4096;
private final byte[] scratchSpace; private final byte[] scratchSpace;
private final DataSource dataSource; private final DataReader dataReader;
private final long streamLength; private final long streamLength;
private long position; private long position;
...@@ -43,12 +41,12 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -43,12 +41,12 @@ public final class DefaultExtractorInput implements ExtractorInput {
private int peekBufferLength; private int peekBufferLength;
/** /**
* @param dataSource The wrapped {@link DataSource}. * @param dataReader The wrapped {@link DataReader}.
* @param position The initial position in the stream. * @param position The initial position in the stream.
* @param length The length of the stream, or {@link C#LENGTH_UNSET} if it is unknown. * @param length The length of the stream, or {@link C#LENGTH_UNSET} if it is unknown.
*/ */
public DefaultExtractorInput(DataSource dataSource, long position, long length) { public DefaultExtractorInput(DataReader dataReader, long position, long length) {
this.dataSource = dataSource; this.dataReader = dataReader;
this.position = position; this.position = position;
this.streamLength = length; this.streamLength = length;
peekBuffer = new byte[PEEK_MIN_FREE_SPACE_AFTER_RESIZE]; peekBuffer = new byte[PEEK_MIN_FREE_SPACE_AFTER_RESIZE];
...@@ -60,7 +58,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -60,7 +58,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
int bytesRead = readFromPeekBuffer(target, offset, length); int bytesRead = readFromPeekBuffer(target, offset, length);
if (bytesRead == 0) { if (bytesRead == 0) {
bytesRead = bytesRead =
readFromDataSource( readFromUpstream(
target, offset, length, /* bytesAlreadyRead= */ 0, /* allowEndOfInput= */ true); target, offset, length, /* bytesAlreadyRead= */ 0, /* allowEndOfInput= */ true);
} }
commitBytesRead(bytesRead); commitBytesRead(bytesRead);
...@@ -72,7 +70,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -72,7 +70,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
throws IOException { throws IOException {
int bytesRead = readFromPeekBuffer(target, offset, length); int bytesRead = readFromPeekBuffer(target, offset, length);
while (bytesRead < length && bytesRead != C.RESULT_END_OF_INPUT) { while (bytesRead < length && bytesRead != C.RESULT_END_OF_INPUT) {
bytesRead = readFromDataSource(target, offset, length, bytesRead, allowEndOfInput); bytesRead = readFromUpstream(target, offset, length, bytesRead, allowEndOfInput);
} }
commitBytesRead(bytesRead); commitBytesRead(bytesRead);
return bytesRead != C.RESULT_END_OF_INPUT; return bytesRead != C.RESULT_END_OF_INPUT;
...@@ -88,7 +86,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -88,7 +86,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
int bytesSkipped = skipFromPeekBuffer(length); int bytesSkipped = skipFromPeekBuffer(length);
if (bytesSkipped == 0) { if (bytesSkipped == 0) {
bytesSkipped = bytesSkipped =
readFromDataSource(scratchSpace, 0, Math.min(length, scratchSpace.length), 0, true); readFromUpstream(scratchSpace, 0, Math.min(length, scratchSpace.length), 0, true);
} }
commitBytesRead(bytesSkipped); commitBytesRead(bytesSkipped);
return bytesSkipped; return bytesSkipped;
...@@ -100,7 +98,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -100,7 +98,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
while (bytesSkipped < length && bytesSkipped != C.RESULT_END_OF_INPUT) { while (bytesSkipped < length && bytesSkipped != C.RESULT_END_OF_INPUT) {
int minLength = Math.min(length, bytesSkipped + scratchSpace.length); int minLength = Math.min(length, bytesSkipped + scratchSpace.length);
bytesSkipped = bytesSkipped =
readFromDataSource(scratchSpace, -bytesSkipped, minLength, bytesSkipped, allowEndOfInput); readFromUpstream(scratchSpace, -bytesSkipped, minLength, bytesSkipped, allowEndOfInput);
} }
commitBytesRead(bytesSkipped); commitBytesRead(bytesSkipped);
return bytesSkipped != C.RESULT_END_OF_INPUT; return bytesSkipped != C.RESULT_END_OF_INPUT;
...@@ -118,7 +116,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -118,7 +116,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
int bytesPeeked; int bytesPeeked;
if (peekBufferRemainingBytes == 0) { if (peekBufferRemainingBytes == 0) {
bytesPeeked = bytesPeeked =
readFromDataSource( readFromUpstream(
peekBuffer, peekBuffer,
peekBufferPosition, peekBufferPosition,
length, length,
...@@ -156,8 +154,8 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -156,8 +154,8 @@ public final class DefaultExtractorInput implements ExtractorInput {
ensureSpaceForPeek(length); ensureSpaceForPeek(length);
int bytesPeeked = peekBufferLength - peekBufferPosition; int bytesPeeked = peekBufferLength - peekBufferPosition;
while (bytesPeeked < length) { while (bytesPeeked < length) {
bytesPeeked = readFromDataSource(peekBuffer, peekBufferPosition, length, bytesPeeked, bytesPeeked =
allowEndOfInput); readFromUpstream(peekBuffer, peekBufferPosition, length, bytesPeeked, allowEndOfInput);
if (bytesPeeked == C.RESULT_END_OF_INPUT) { if (bytesPeeked == C.RESULT_END_OF_INPUT) {
return false; return false;
} }
...@@ -259,7 +257,7 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -259,7 +257,7 @@ public final class DefaultExtractorInput implements ExtractorInput {
} }
/** /**
* Starts or continues a read from the data source. * Starts or continues a read from the data reader.
* *
* @param target A target array into which data should be written. * @param target A target array into which data should be written.
* @param offset The offset into the target array at which to write. * @param offset The offset into the target array at which to write.
...@@ -275,13 +273,13 @@ public final class DefaultExtractorInput implements ExtractorInput { ...@@ -275,13 +273,13 @@ public final class DefaultExtractorInput implements ExtractorInput {
* read and {@code allowEndOfInput} is false. * read and {@code allowEndOfInput} is false.
* @throws IOException If an error occurs reading from the input. * @throws IOException If an error occurs reading from the input.
*/ */
private int readFromDataSource( private int readFromUpstream(
byte[] target, int offset, int length, int bytesAlreadyRead, boolean allowEndOfInput) byte[] target, int offset, int length, int bytesAlreadyRead, boolean allowEndOfInput)
throws IOException { throws IOException {
if (Thread.interrupted()) { if (Thread.interrupted()) {
throw new InterruptedIOException(); throw new InterruptedIOException();
} }
int bytesRead = dataSource.read(target, offset + bytesAlreadyRead, length - bytesAlreadyRead); int bytesRead = dataReader.read(target, offset + bytesAlreadyRead, length - bytesAlreadyRead);
if (bytesRead == C.RESULT_END_OF_INPUT) { if (bytesRead == C.RESULT_END_OF_INPUT) {
if (bytesAlreadyRead == 0 && allowEndOfInput) { if (bytesAlreadyRead == 0 && allowEndOfInput) {
return C.RESULT_END_OF_INPUT; return C.RESULT_END_OF_INPUT;
......
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