use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class CacheDataSource method openNextSource.
/**
* Opens the next source. If the cache contains data spanning the current read position then
* {@link #cacheReadDataSource} is opened to read from it. Else {@link #upstreamDataSource} is
* opened to read from the upstream source and write into the cache.
* @param initial Whether it is the initial open call.
*/
private boolean openNextSource(boolean initial) throws IOException {
DataSpec dataSpec;
CacheSpan span;
if (currentRequestIgnoresCache) {
span = null;
} else if (blockOnCache) {
try {
span = cache.startReadWrite(key, readPosition);
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
} else {
span = cache.startReadWriteNonBlocking(key, readPosition);
}
if (span == null) {
// The data is locked in the cache, or we're ignoring the cache. Bypass the cache and read
// from upstream.
currentDataSource = upstreamDataSource;
dataSpec = new DataSpec(uri, readPosition, bytesRemaining, key, flags);
} else if (span.isCached) {
// Data is cached, read from cache.
Uri fileUri = Uri.fromFile(span.file);
long filePosition = readPosition - span.position;
long length = span.length - filePosition;
if (bytesRemaining != C.LENGTH_UNSET) {
length = Math.min(length, bytesRemaining);
}
dataSpec = new DataSpec(fileUri, readPosition, filePosition, length, key, flags);
currentDataSource = cacheReadDataSource;
} else {
// Data is not cached, and data is not locked, read from upstream with cache backing.
long length;
if (span.isOpenEnded()) {
length = bytesRemaining;
} else {
length = span.length;
if (bytesRemaining != C.LENGTH_UNSET) {
length = Math.min(length, bytesRemaining);
}
}
dataSpec = new DataSpec(uri, readPosition, length, key, flags);
if (cacheWriteDataSource != null) {
currentDataSource = cacheWriteDataSource;
lockedSpan = span;
} else {
currentDataSource = upstreamDataSource;
cache.releaseHoleSpan(span);
}
}
currentRequestUnbounded = dataSpec.length == C.LENGTH_UNSET;
boolean successful = false;
long currentBytesRemaining = 0;
try {
currentBytesRemaining = currentDataSource.open(dataSpec);
successful = true;
} catch (IOException e) {
// end of the stream.
if (!initial && currentRequestUnbounded) {
Throwable cause = e;
while (cause != null) {
if (cause instanceof DataSourceException) {
int reason = ((DataSourceException) cause).reason;
if (reason == DataSourceException.POSITION_OUT_OF_RANGE) {
e = null;
break;
}
}
cause = cause.getCause();
}
}
if (e != null) {
throw e;
}
}
// bytesRemaining == C.LENGTH_UNSET) and got a resolved length from open() request
if (currentRequestUnbounded && currentBytesRemaining != C.LENGTH_UNSET) {
bytesRemaining = currentBytesRemaining;
setContentLength(dataSpec.position + bytesRemaining);
}
return successful;
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class DefaultExtractorInputTest method buildFailingDataSource.
private static FakeDataSource buildFailingDataSource() throws Exception {
FakeDataSource.Builder builder = new FakeDataSource.Builder();
builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 0, 6));
builder.appendReadError(new IOException());
builder.appendReadData(Arrays.copyOfRange(TEST_DATA, 6, 9));
FakeDataSource testDataSource = builder.build();
testDataSource.open(new DataSpec(Uri.parse(TEST_URI)));
return testDataSource;
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class ContainerMediaChunk method load.
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public final void load() throws IOException, InterruptedException {
DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded);
try {
// Create and open the input.
ExtractorInput input = new DefaultExtractorInput(dataSource, loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
if (bytesLoaded == 0) {
// Configure the output and set it as the target for the extractor wrapper.
BaseMediaChunkOutput output = getOutput();
output.setSampleOffsetUs(sampleOffsetUs);
extractorWrapper.init(output);
}
// Load and decode the sample data.
try {
Extractor extractor = extractorWrapper.extractor;
int result = Extractor.RESULT_CONTINUE;
while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
result = extractor.read(input, null);
}
Assertions.checkState(result != Extractor.RESULT_SEEK);
} finally {
bytesLoaded = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
}
} finally {
Util.closeQuietly(dataSource);
}
loadCompleted = true;
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class InitializationChunk method load.
@SuppressWarnings("NonAtomicVolatileUpdate")
@Override
public void load() throws IOException, InterruptedException {
DataSpec loadDataSpec = Util.getRemainderDataSpec(dataSpec, bytesLoaded);
try {
// Create and open the input.
ExtractorInput input = new DefaultExtractorInput(dataSource, loadDataSpec.absoluteStreamPosition, dataSource.open(loadDataSpec));
if (bytesLoaded == 0) {
extractorWrapper.init(null);
}
// Load and decode the initialization data.
try {
Extractor extractor = extractorWrapper.extractor;
int result = Extractor.RESULT_CONTINUE;
while (result == Extractor.RESULT_CONTINUE && !loadCanceled) {
result = extractor.read(input, null);
}
Assertions.checkState(result != Extractor.RESULT_SEEK);
} finally {
bytesLoaded = (int) (input.getPosition() - dataSpec.absoluteStreamPosition);
}
} finally {
Util.closeQuietly(dataSource);
}
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class DashUtil method loadInitializationData.
private static void loadInitializationData(DataSource dataSource, Representation representation, ChunkExtractorWrapper extractorWrapper, RangedUri requestUri) throws IOException, InterruptedException {
DataSpec dataSpec = new DataSpec(requestUri.resolveUri(representation.baseUrl), requestUri.start, requestUri.length, representation.getCacheKey());
InitializationChunk initializationChunk = new InitializationChunk(dataSource, dataSpec, representation.format, C.SELECTION_REASON_UNKNOWN, null, /* trackSelectionData */
extractorWrapper);
initializationChunk.load();
}
Aggregations