Search in sources :

Example 76 with DataSpec

use of com.google.android.exoplayer2.upstream.DataSpec in project prebid-mobile-android by prebid.

the class XandrInstreamVideoGamActivity method initializePlayer.

private void initializePlayer() {
    SimpleExoPlayer.Builder playerBuilder = new SimpleExoPlayer.Builder(this);
    player = playerBuilder.build();
    playerView.setPlayer(player);
    adsLoader.setPlayer(player);
    Uri uri = Uri.parse(getString(R.string.content_url));
    MediaItem mediaItem = MediaItem.fromUri(uri);
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, getString(R.string.app_name));
    ProgressiveMediaSource.Factory mediaSourceFactory = new ProgressiveMediaSource.Factory(dataSourceFactory);
    MediaSource mediaSource = mediaSourceFactory.createMediaSource(mediaItem);
    DataSpec dataSpec = new DataSpec(adsUri);
    AdsMediaSource adsMediaSource = new AdsMediaSource(mediaSource, dataSpec, "ad", mediaSourceFactory, adsLoader, playerView);
    player.setMediaSource(adsMediaSource);
    player.setPlayWhenReady(true);
    player.prepare();
}
Also used : SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) Uri(android.net.Uri) DataSource(com.google.android.exoplayer2.upstream.DataSource) ProgressiveMediaSource(com.google.android.exoplayer2.source.ProgressiveMediaSource) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) MediaItem(com.google.android.exoplayer2.MediaItem) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) AdsMediaSource(com.google.android.exoplayer2.source.ads.AdsMediaSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec)

Example 77 with DataSpec

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

the class CronetDataSourceTest method testRangeRequestWith206Response.

@Test
public void testRangeRequestWith206Response() throws HttpDataSourceException {
    mockResponseStartSuccess();
    mockReadSuccess(1000, 5000);
    // Server supports range requests.
    testUrlResponseInfo = createUrlResponseInfo(206);
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 1000, 5000, null);
    dataSourceUnderTest.open(testDataSpec);
    byte[] returnedBuffer = new byte[16];
    int bytesRead = dataSourceUnderTest.read(returnedBuffer, 0, 16);
    assertEquals(16, bytesRead);
    assertArrayEquals(buildTestDataArray(1000, 16), returnedBuffer);
    verify(mockTransferListener).onBytesTransferred(dataSourceUnderTest, 16);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 78 with DataSpec

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

the class CronetDataSourceTest method testRequestOpenGzippedCompressedReturnsDataSpecLength.

@Test
public void testRequestOpenGzippedCompressedReturnsDataSpecLength() throws HttpDataSourceException {
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 0, 5000, null);
    testResponseHeader.put("Content-Encoding", "gzip");
    testResponseHeader.put("Content-Length", Long.toString(50L));
    mockResponseStartSuccess();
    assertEquals(5000, /* contentLength */
    dataSourceUnderTest.open(testDataSpec));
    verify(mockTransferListener).onTransferStart(dataSourceUnderTest, testDataSpec);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 79 with DataSpec

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

the class CronetDataSourceTest method testRangeRequestWith200Response.

@Test
public void testRangeRequestWith200Response() throws HttpDataSourceException {
    mockResponseStartSuccess();
    mockReadSuccess(0, 7000);
    // Server does not support range requests.
    testUrlResponseInfo = createUrlResponseInfo(200);
    testDataSpec = new DataSpec(Uri.parse(TEST_URL), 1000, 5000, null);
    dataSourceUnderTest.open(testDataSpec);
    byte[] returnedBuffer = new byte[16];
    int bytesRead = dataSourceUnderTest.read(returnedBuffer, 0, 16);
    assertEquals(16, bytesRead);
    assertArrayEquals(buildTestDataArray(1000, 16), returnedBuffer);
    verify(mockTransferListener).onBytesTransferred(dataSourceUnderTest, 16);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 80 with DataSpec

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;
}
Also used : InterruptedIOException(java.io.InterruptedIOException) DataSourceException(com.google.android.exoplayer2.upstream.DataSourceException) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) IOException(java.io.IOException) InterruptedIOException(java.io.InterruptedIOException) Uri(android.net.Uri)

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