Search in sources :

Example 81 with DataSpec

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();
}
Also used : InitializationChunk(com.google.android.exoplayer2.source.chunk.InitializationChunk) DataSpec(com.google.android.exoplayer2.upstream.DataSpec)

Example 82 with DataSpec

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

the class DashUtil method loadManifest.

/**
   * Loads a DASH manifest.
   *
   * @param dataSource The {@link HttpDataSource} from which the manifest should be read.
   * @param manifestUriString The URI of the manifest to be read.
   * @return An instance of {@link DashManifest}.
   * @throws IOException If an error occurs reading data from the stream.
   * @see DashManifestParser
   */
public static DashManifest loadManifest(DataSource dataSource, String manifestUriString) throws IOException {
    DataSourceInputStream inputStream = new DataSourceInputStream(dataSource, new DataSpec(Uri.parse(manifestUriString), DataSpec.FLAG_ALLOW_CACHING_UNKNOWN_LENGTH));
    try {
        inputStream.open();
        DashManifestParser parser = new DashManifestParser();
        return parser.parse(dataSource.getUri(), inputStream);
    } finally {
        inputStream.close();
    }
}
Also used : DashManifestParser(com.google.android.exoplayer2.source.dash.manifest.DashManifestParser) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) DataSourceInputStream(com.google.android.exoplayer2.upstream.DataSourceInputStream)

Example 83 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, String cacheKey, int chunkIndex, long chunkStartTimeUs, long chunkEndTimeUs, int trackSelectionReason, Object trackSelectionData, ChunkExtractorWrapper extractorWrapper) {
    DataSpec dataSpec = new DataSpec(uri, 0, C.LENGTH_UNSET, cacheKey);
    // 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, chunkIndex, 1, sampleOffsetUs, extractorWrapper);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ContainerMediaChunk(com.google.android.exoplayer2.source.chunk.ContainerMediaChunk)

Example 84 with DataSpec

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

the class ImaUtil method getAdsRequestForAdTagDataSpec.

/**
 * Returns an {@link AdsRequest} based on the specified ad tag {@link DataSpec}.
 */
public static AdsRequest getAdsRequestForAdTagDataSpec(ImaFactory imaFactory, DataSpec adTagDataSpec) throws IOException {
    AdsRequest request = imaFactory.createAdsRequest();
    if (DataSchemeDataSource.SCHEME_DATA.equals(adTagDataSpec.uri.getScheme())) {
        DataSchemeDataSource dataSchemeDataSource = new DataSchemeDataSource();
        try {
            dataSchemeDataSource.open(adTagDataSpec);
            request.setAdsResponse(Util.fromUtf8Bytes(DataSourceUtil.readToEnd(dataSchemeDataSource)));
        } finally {
            dataSchemeDataSource.close();
        }
    } else {
        request.setAdTagUrl(adTagDataSpec.uri.toString());
    }
    return request;
}
Also used : AdsRequest(com.google.ads.interactivemedia.v3.api.AdsRequest) DataSchemeDataSource(com.google.android.exoplayer2.upstream.DataSchemeDataSource)

Example 85 with DataSpec

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

the class CronetDataSourceTest method overreadByteBuffer.

@Test
public void overreadByteBuffer() throws HttpDataSourceException {
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 0, 16);
    testResponseHeader.put("Content-Length", Long.toString(16L));
    mockResponseStartSuccess();
    mockReadSuccess(0, 16);
    dataSourceUnderTest.open(testDataSpec);
    ByteBuffer returnedBuffer = ByteBuffer.allocateDirect(8);
    int bytesRead = dataSourceUnderTest.read(returnedBuffer);
    assertThat(bytesRead).isEqualTo(8);
    returnedBuffer.flip();
    assertThat(copyByteBufferToArray(returnedBuffer)).isEqualTo(buildTestDataArray(0, 8));
    // The current buffer is kept if not completely consumed by DataSource reader.
    returnedBuffer = ByteBuffer.allocateDirect(6);
    bytesRead += dataSourceUnderTest.read(returnedBuffer);
    assertThat(bytesRead).isEqualTo(14);
    returnedBuffer.flip();
    assertThat(copyByteBufferToArray(returnedBuffer)).isEqualTo(buildTestDataArray(8, 6));
    // 2 bytes left at this point.
    returnedBuffer = ByteBuffer.allocateDirect(8);
    bytesRead += dataSourceUnderTest.read(returnedBuffer);
    assertThat(bytesRead).isEqualTo(16);
    returnedBuffer.flip();
    assertThat(copyByteBufferToArray(returnedBuffer)).isEqualTo(buildTestDataArray(14, 2));
    // Called on each.
    verify(mockUrlRequest, times(3)).read(any(ByteBuffer.class));
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 8);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 6);
    verify(mockTransferListener, times(1)).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, 2);
    // Now we already returned the 16 bytes initially asked.
    // Try to read again even though all requested 16 bytes are already returned.
    // Return C.RESULT_END_OF_INPUT
    returnedBuffer = ByteBuffer.allocateDirect(16);
    int bytesOverRead = dataSourceUnderTest.read(returnedBuffer);
    assertThat(bytesOverRead).isEqualTo(C.RESULT_END_OF_INPUT);
    assertThat(returnedBuffer.position()).isEqualTo(0);
    // C.RESULT_END_OF_INPUT should not be reported though the TransferListener.
    verify(mockTransferListener, never()).onBytesTransferred(dataSourceUnderTest, testDataSpec, /* isNetwork= */
    true, C.RESULT_END_OF_INPUT);
    // Number of calls to cronet should not have increased.
    verify(mockUrlRequest, times(3)).read(any(ByteBuffer.class));
    // Check for connection not automatically closed.
    verify(mockUrlRequest, never()).cancel();
    assertThat(bytesRead).isEqualTo(16);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) ByteBuffer(java.nio.ByteBuffer) 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