Search in sources :

Example 46 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.

the class DefaultSsChunkSource method newMediaChunk.

// Private methods.
private static MediaChunk newMediaChunk(Format format, DataSource dataSource, Uri uri, int chunkIndex, long chunkStartTimeUs, long chunkEndTimeUs, long chunkSeekTimeUs, @C.SelectionReason int trackSelectionReason, @Nullable Object trackSelectionData, ChunkExtractor chunkExtractor) {
    DataSpec dataSpec = new DataSpec(uri);
    // In SmoothStreaming each chunk contains sample timestamps relative to the start of the chunk.
    // To convert them the absolute timestamps, we need to set sampleOffsetUs to chunkStartTimeUs.
    long sampleOffsetUs = chunkStartTimeUs;
    return new ContainerMediaChunk(dataSource, dataSpec, format, trackSelectionReason, trackSelectionData, chunkStartTimeUs, chunkEndTimeUs, chunkSeekTimeUs, /* clippedEndTimeUs= */
    C.TIME_UNSET, chunkIndex, /* chunkCount= */
    1, sampleOffsetUs, chunkExtractor);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ContainerMediaChunk(com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)

Example 47 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.

the class DataSourceContractTest method dataSpecWithEndPositionOutOfRange_readsToEnd.

@Test
public void dataSpecWithEndPositionOutOfRange_readsToEnd() 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 - 1).setLength(2).build();
        try {
            long length = dataSource.open(dataSpec);
            byte[] data = DataSourceUtil.readExactly(dataSource, /* length= */
            1);
            // TODO: Decide what the allowed behavior should be for the next read, and assert it.
            // 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(2);
            byte[] expectedData = Arrays.copyOfRange(resource.getExpectedBytes(), resourceLength - 1, resourceLength);
            assertThat(data).isEqualTo(expectedData);
        } 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)

Example 48 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.

the class DataSourceContractTest method dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException.

@Test
public void dataSpecWithPositionOutOfRange_throwsPositionOutOfRangeException() 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 + 1).build();
        try {
            IOException exception = assertThrows(IOException.class, () -> dataSource.open(dataSpec));
            assertThat(DataSourceException.isCausedByPositionOutOfRange(exception)).isTrue();
        } finally {
            dataSource.close();
        }
        additionalFailureInfo.setInfo(null);
    }
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) IOException(java.io.IOException) DataSource(com.google.android.exoplayer2.upstream.DataSource) Test(org.junit.Test)

Example 49 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.

the class DataSourceContractTest method getUri_returnsNonNullValueOnlyWhileOpen.

@Test
public void getUri_returnsNonNullValueOnlyWhileOpen() 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);
        DataSource dataSource = createDataSource();
        try {
            assertThat(dataSource.getUri()).isNull();
            dataSource.open(new DataSpec(resource.getUri()));
            assertThat(dataSource.getUri()).isNotNull();
        } finally {
            dataSource.close();
        }
        assertThat(dataSource.getUri()).isNull();
        additionalFailureInfo.setInfo(null);
    }
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DataSource(com.google.android.exoplayer2.upstream.DataSource) Test(org.junit.Test)

Example 50 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project ExoPlayer by google.

the class DataSourceContractTest method getResponseHeaders_noNullKeysOrValues.

@Test
public void getResponseHeaders_noNullKeysOrValues() 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);
        DataSource dataSource = createDataSource();
        try {
            dataSource.open(new DataSpec(resource.getUri()));
            Map<String, List<String>> responseHeaders = dataSource.getResponseHeaders();
            assertThat(responseHeaders).doesNotContainKey(null);
            assertThat(responseHeaders.values()).doesNotContain(null);
            for (List<String> value : responseHeaders.values()) {
                assertThat(value).doesNotContain(null);
            }
        } finally {
            dataSource.close();
        }
        additionalFailureInfo.setInfo(null);
    }
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) 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