Search in sources :

Example 91 with DataSpec

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.
    }
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) EOFException(java.io.EOFException) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 92 with DataSpec

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;
    }
}
Also used : DataSourceException(com.google.android.exoplayer2.upstream.DataSourceException) FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) IOException(java.io.IOException) Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 93 with DataSpec

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);
    }
}
Also used : Format(com.google.android.exoplayer2.Format) MediaChunkIterator(com.google.android.exoplayer2.source.chunk.MediaChunkIterator) SingleSampleMediaChunk(com.google.android.exoplayer2.source.chunk.SingleSampleMediaChunk) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Segment(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData.Segment)

Example 94 with DataSpec

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);
}
Also used : TransferListener(com.google.android.exoplayer2.upstream.TransferListener) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Nullable(androidx.annotation.Nullable) DataSource(com.google.android.exoplayer2.upstream.DataSource) Test(org.junit.Test)

Example 95 with DataSpec

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);
    }
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DataSource(com.google.android.exoplayer2.upstream.DataSource) Test(org.junit.Test)

Aggregations

DataSpec (com.google.android.exoplayer2.upstream.DataSpec)118 Test (org.junit.Test)79 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)28 DataSource (com.google.android.exoplayer2.upstream.DataSource)22 Uri (android.net.Uri)17 Nullable (androidx.annotation.Nullable)17 IOException (java.io.IOException)17 DefaultExtractorInput (com.google.android.exoplayer2.extractor.DefaultExtractorInput)9 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)9 HlsMediaPlaylist (com.google.android.exoplayer2.source.hls.playlist.HlsMediaPlaylist)8 ArrayList (java.util.ArrayList)7 ExtractorInput (com.google.android.exoplayer2.extractor.ExtractorInput)6 InterruptedIOException (java.io.InterruptedIOException)5 List (java.util.List)5 ContainerMediaChunk (com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)4 Representation (com.google.android.exoplayer2.source.dash.manifest.Representation)4 DataSourceException (com.google.android.exoplayer2.upstream.DataSourceException)4 DataSourceInputStream (com.google.android.exoplayer2.upstream.DataSourceInputStream)4 HttpDataSource (com.google.android.exoplayer2.upstream.HttpDataSource)4 ByteBuffer (java.nio.ByteBuffer)4