Commit 40039ed0 by eguven Committed by Oliver Woodman

Fix and move Util.getRemainderDataSpec() to DataSpec.subrange()

Made the method copy all of the fields of DataSpec in to the new instance. Also converted
it to an instance method of DataSpec for ease of usage, discovery and maintenance.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=159670314
parent 44f6dbb0
...@@ -93,7 +93,7 @@ public class ContainerMediaChunk extends BaseMediaChunk { ...@@ -93,7 +93,7 @@ public class ContainerMediaChunk extends BaseMediaChunk {
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")
@Override @Override
public final void load() throws IOException, InterruptedException { public final void load() throws IOException, InterruptedException {
DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded); DataSpec loadDataSpec = dataSpec.subrange(bytesLoaded);
try { try {
// Create and open the input. // Create and open the input.
ExtractorInput input = new DefaultExtractorInput(dataSource, ExtractorInput input = new DefaultExtractorInput(dataSource,
......
...@@ -72,7 +72,7 @@ public final class InitializationChunk extends Chunk { ...@@ -72,7 +72,7 @@ public final class InitializationChunk extends Chunk {
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded); DataSpec loadDataSpec = dataSpec.subrange(bytesLoaded);
try { try {
// Create and open the input. // Create and open the input.
ExtractorInput input = new DefaultExtractorInput(dataSource, ExtractorInput input = new DefaultExtractorInput(dataSource,
......
...@@ -85,7 +85,7 @@ public final class SingleSampleMediaChunk extends BaseMediaChunk { ...@@ -85,7 +85,7 @@ public final class SingleSampleMediaChunk extends BaseMediaChunk {
@SuppressWarnings("NonAtomicVolatileUpdate") @SuppressWarnings("NonAtomicVolatileUpdate")
@Override @Override
public void load() throws IOException, InterruptedException { public void load() throws IOException, InterruptedException {
DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded); DataSpec loadDataSpec = dataSpec.subrange(bytesLoaded);
try { try {
// Create and open the input. // Create and open the input.
long length = dataSource.open(loadDataSpec); long length = dataSource.open(loadDataSpec);
......
...@@ -68,7 +68,7 @@ public final class DataSpec { ...@@ -68,7 +68,7 @@ public final class DataSpec {
* The position of the data when read from {@link #uri}. * The position of the data when read from {@link #uri}.
* <p> * <p>
* Always equal to {@link #absoluteStreamPosition} unless the {@link #uri} defines the location * Always equal to {@link #absoluteStreamPosition} unless the {@link #uri} defines the location
* of a subset of the underyling data. * of a subset of the underlying data.
*/ */
public final long position; public final long position;
/** /**
...@@ -187,4 +187,31 @@ public final class DataSpec { ...@@ -187,4 +187,31 @@ public final class DataSpec {
+ ", " + position + ", " + length + ", " + key + ", " + flags + "]"; + ", " + position + ", " + length + ", " + key + ", " + flags + "]";
} }
/**
* Returns a {@link DataSpec} that represents a subrange of the data defined by this DataSpec. The
* subrange includes data from the offset up to the end of this DataSpec.
*
* @param offset The offset of the subrange.
* @return A {@link DataSpec} that represents a subrange of the data defined by this DataSpec.
*/
public DataSpec subrange(long offset) {
return subrange(offset, length == C.LENGTH_UNSET ? C.LENGTH_UNSET : length - offset);
}
/**
* Returns a {@link DataSpec} that represents a subrange of the data defined by this DataSpec.
*
* @param offset The offset of the subrange.
* @param length The length of the subrange.
* @return A {@link DataSpec} that represents a subrange of the data defined by this DataSpec.
*/
public DataSpec subrange(long offset, long length) {
if (offset == 0 && this.length == length) {
return this;
} else {
return new DataSpec(uri, postBody, absoluteStreamPosition + offset, position + offset, length,
key, flags);
}
}
} }
...@@ -34,7 +34,6 @@ import com.google.android.exoplayer2.C; ...@@ -34,7 +34,6 @@ import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.ExoPlayerLibraryInfo; import com.google.android.exoplayer2.ExoPlayerLibraryInfo;
import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.Closeable; import java.io.Closeable;
import java.io.File; import java.io.File;
...@@ -681,25 +680,6 @@ public final class Util { ...@@ -681,25 +680,6 @@ public final class Util {
} }
/** /**
* Given a {@link DataSpec} and a number of bytes already loaded, returns a {@link DataSpec}
* that represents the remainder of the data.
*
* @param dataSpec The original {@link DataSpec}.
* @param bytesLoaded The number of bytes already loaded.
* @return A {@link DataSpec} that represents the remainder of the data.
*/
public static DataSpec getRemainderDataSpec(DataSpec dataSpec, int bytesLoaded) {
if (bytesLoaded == 0) {
return dataSpec;
} else {
long remainingLength = dataSpec.length == C.LENGTH_UNSET ? C.LENGTH_UNSET
: dataSpec.length - bytesLoaded;
return new DataSpec(dataSpec.uri, dataSpec.position + bytesLoaded, remainingLength,
dataSpec.key, dataSpec.flags);
}
}
/**
* Returns the integer equal to the big-endian concatenation of the characters in {@code string} * Returns the integer equal to the big-endian concatenation of the characters in {@code string}
* as bytes. The string must be no more than four characters long. * as bytes. The string must be no more than four characters long.
* *
......
...@@ -210,7 +210,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -210,7 +210,7 @@ import java.util.concurrent.atomic.AtomicInteger;
// According to spec, for packed audio, initDataSpec is expected to be null. // According to spec, for packed audio, initDataSpec is expected to be null.
return; return;
} }
DataSpec initSegmentDataSpec = Util.getRemainderDataSpec(initDataSpec, initSegmentBytesLoaded); DataSpec initSegmentDataSpec = initDataSpec.subrange(initSegmentBytesLoaded);
try { try {
ExtractorInput input = new DefaultExtractorInput(initDataSource, ExtractorInput input = new DefaultExtractorInput(initDataSource,
initSegmentDataSpec.absoluteStreamPosition, initDataSource.open(initSegmentDataSpec)); initSegmentDataSpec.absoluteStreamPosition, initDataSource.open(initSegmentDataSpec));
...@@ -239,7 +239,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -239,7 +239,7 @@ import java.util.concurrent.atomic.AtomicInteger;
loadDataSpec = dataSpec; loadDataSpec = dataSpec;
skipLoadedBytes = bytesLoaded != 0; skipLoadedBytes = bytesLoaded != 0;
} else { } else {
loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded); loadDataSpec = dataSpec.subrange(bytesLoaded);
skipLoadedBytes = false; skipLoadedBytes = false;
} }
if (!isMasterTimestampSource) { if (!isMasterTimestampSource) {
...@@ -396,7 +396,7 @@ import java.util.concurrent.atomic.AtomicInteger; ...@@ -396,7 +396,7 @@ import java.util.concurrent.atomic.AtomicInteger;
} else if (lastPathSegment.endsWith(MP3_FILE_EXTENSION)) { } else if (lastPathSegment.endsWith(MP3_FILE_EXTENSION)) {
extractor = new Mp3Extractor(0, startTimeUs); extractor = new Mp3Extractor(0, startTimeUs);
} else { } else {
throw new IllegalArgumentException("Unkown extension for audio file: " + lastPathSegment); throw new IllegalArgumentException("Unknown extension for audio file: " + lastPathSegment);
} }
extractor.init(extractorOutput); extractor.init(extractorOutput);
return extractor; return extractor;
......
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