use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class DefaultExtractorInputTest method skipFullyLarge.
@Test
public void skipFullyLarge() throws Exception {
// Tests skipping an amount of data that's larger than any internal scratch space.
int largeSkipSize = 1024 * 1024;
FakeDataSource testDataSource = new FakeDataSource();
testDataSource.getDataSet().newDefaultData().appendReadData(new byte[largeSkipSize]);
testDataSource.open(new DataSpec(Uri.parse(TEST_URI)));
DefaultExtractorInput input = new DefaultExtractorInput(testDataSource, 0, C.LENGTH_UNSET);
input.skipFully(largeSkipSize);
assertThat(input.getPosition()).isEqualTo(largeSkipSize);
// Check that we fail with EOFException we skip again.
try {
input.skipFully(1);
fail();
} catch (EOFException e) {
// Expected.
}
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class FakeDataSource method open.
@Override
public final long open(DataSpec dataSpec) throws IOException {
Assertions.checkState(!openCalled);
openCalled = true;
// DataSpec requires a matching close call even if open fails.
uri = dataSpec.uri;
openedDataSpecs.add(dataSpec);
transferInitializing(dataSpec);
FakeData fakeData = fakeDataSet.getData(dataSpec.uri.toString());
if (fakeData == null) {
throw new IOException("Data not found: " + dataSpec.uri);
}
this.fakeData = fakeData;
long totalLength = 0;
for (Segment segment : fakeData.getSegments()) {
totalLength += segment.length;
}
if (totalLength == 0) {
throw new IOException("Data is empty: " + dataSpec.uri);
}
if (dataSpec.position > totalLength) {
throw new DataSourceException(PlaybackException.ERROR_CODE_IO_READ_POSITION_OUT_OF_RANGE);
}
// Scan through the segments, configuring them for the current read.
boolean findingCurrentSegmentIndex = true;
currentSegmentIndex = 0;
int scannedLength = 0;
for (Segment segment : fakeData.getSegments()) {
segment.bytesRead = (int) min(max(0, dataSpec.position - scannedLength), segment.length);
scannedLength += segment.length;
findingCurrentSegmentIndex &= segment.isErrorSegment() ? segment.exceptionCleared : (!segment.isActionSegment() && segment.bytesRead == segment.length);
if (findingCurrentSegmentIndex) {
currentSegmentIndex++;
}
}
sourceOpened = true;
transferStarted(dataSpec);
// Configure bytesRemaining, and return.
if (dataSpec.length == C.LENGTH_UNSET) {
bytesRemaining = totalLength - dataSpec.position;
return fakeData.isSimulatingUnknownLength() ? C.LENGTH_UNSET : bytesRemaining;
} else {
bytesRemaining = dataSpec.length;
return bytesRemaining;
}
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class FakeChunkSource method getNextChunk.
@Override
public void getNextChunk(long playbackPositionUs, long loadPositionUs, List<? extends MediaChunk> queue, ChunkHolder out) {
long bufferedDurationUs = loadPositionUs - playbackPositionUs;
int chunkIndex = queue.isEmpty() ? dataSet.getChunkIndexByPosition(playbackPositionUs) : (int) queue.get(queue.size() - 1).getNextChunkIndex();
MediaChunkIterator[] chunkIterators = new MediaChunkIterator[trackSelection.length()];
for (int i = 0; i < chunkIterators.length; i++) {
int trackGroupIndex = trackSelection.getIndexInTrackGroup(i);
chunkIterators[i] = new FakeAdaptiveDataSet.Iterator(dataSet, trackGroupIndex, chunkIndex);
}
trackSelection.updateSelectedTrack(playbackPositionUs, bufferedDurationUs, C.TIME_UNSET, queue, chunkIterators);
if (chunkIndex >= dataSet.getChunkCount()) {
out.endOfStream = true;
} else {
Format selectedFormat = trackSelection.getSelectedFormat();
long startTimeUs = dataSet.getStartTime(chunkIndex);
long endTimeUs = startTimeUs + dataSet.getChunkDuration(chunkIndex);
int trackGroupIndex = trackSelection.getIndexInTrackGroup(trackSelection.getSelectedIndex());
String uri = dataSet.getUri(trackGroupIndex);
Segment fakeDataChunk = Assertions.checkStateNotNull(dataSet.getData(uri)).getSegments().get(chunkIndex);
DataSpec dataSpec = new DataSpec(Uri.parse(uri), fakeDataChunk.byteOffset, fakeDataChunk.length);
int trackType = MimeTypes.getTrackType(selectedFormat.sampleMimeType);
out.chunk = new SingleSampleMediaChunk(dataSource, dataSpec, selectedFormat, trackSelection.getSelectionReason(), trackSelection.getSelectionData(), startTimeUs, endTimeUs, chunkIndex, trackType, selectedFormat);
}
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class DataSourceContractTest method resourceNotFound_transferListenerCallbacks.
@Test
public void resourceNotFound_transferListenerCallbacks() throws Exception {
DataSource dataSource = createDataSource();
TransferListener listener = mock(TransferListener.class);
dataSource.addTransferListener(listener);
@Nullable DataSource callbackSource = getTransferListenerDataSource();
if (callbackSource == null) {
callbackSource = dataSource;
}
assertThrows(IOException.class, () -> dataSource.open(new DataSpec(getNotFoundUri())));
// Verify onTransferInitializing() has been called exactly from DataSource.open().
verify(listener).onTransferInitializing(eq(callbackSource), any(), anyBoolean());
verifyNoMoreInteractions(listener);
dataSource.close();
verifyNoMoreInteractions(listener);
}
use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.
the class DataSourceContractTest method dataSpecWithPositionAtEndAndLength_readsZeroBytes.
@Test
public void dataSpecWithPositionAtEndAndLength_readsZeroBytes() throws Exception {
ImmutableList<TestResource> resources = getTestResources();
Assertions.checkArgument(!resources.isEmpty(), "Must provide at least one test resource.");
for (int i = 0; i < resources.size(); i++) {
additionalFailureInfo.setInfo(getFailureLabel(resources, i));
TestResource resource = resources.get(i);
int resourceLength = resource.getExpectedBytes().length;
DataSource dataSource = createDataSource();
DataSpec dataSpec = new DataSpec.Builder().setUri(resource.getUri()).setPosition(resourceLength).setLength(1).build();
try {
long length = dataSource.open(dataSpec);
byte[] data = unboundedReadsAreIndefinite() ? Util.EMPTY_BYTE_ARRAY : DataSourceUtil.readToEnd(dataSource);
// The DataSource.open() contract requires the returned length to equal the length in the
// DataSpec if set. This is true even though the DataSource implementation may know that
// fewer bytes will be read in this case.
assertThat(length).isEqualTo(1);
assertThat(data).isEmpty();
} finally {
dataSource.close();
}
additionalFailureInfo.setInfo(null);
}
}
Aggregations