Search in sources :

Example 36 with DataSpec

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

the class CacheDataSourceTest method deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash.

@Test
public void deleteCachedWhileReadingFromUpstreamWithReadOnlyCacheDataSourceDoesNotCrash() throws Exception {
    // Create a fake data source with a 1 KB default data.
    FakeDataSource upstream = new FakeDataSource();
    int dataLength = 1024;
    upstream.getDataSet().newDefaultData().appendReadData(dataLength).endData();
    // Cache the latter half of the data.
    int halfDataLength = 512;
    DataSpec dataSpec = buildDataSpec(halfDataLength, C.LENGTH_UNSET);
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream), dataSpec, /* temporaryBuffer= */
    null, /* progressListener= */
    null);
    cacheWriter.cache();
    // Create cache read-only CacheDataSource.
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, new FileDataSource(), null, 0, null);
    // Open source and read some data from upstream as the data hasn't cached yet.
    cacheDataSource.open(unboundedDataSpec);
    DataSourceUtil.readExactly(cacheDataSource, 100);
    // Delete cached data.
    cache.removeResource(cacheDataSource.getCacheKeyFactory().buildCacheKey(unboundedDataSpec));
    assertCacheEmpty(cache);
    // Read the rest of the data.
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 37 with DataSpec

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

the class CacheWriterTest method cache_afterFailureOnClose_succeeds.

@Test
public void cache_afterFailureOnClose_succeeds() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
    FakeDataSource upstreamDataSource = new FakeDataSource(fakeDataSet);
    AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
    true);
    FailOnCloseDataSink dataSink = new FailOnCloseDataSink(cache, failOnClose);
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstreamDataSource, new FileDataSource(), dataSink, /* flags= */
    0, /* eventListener= */
    null);
    CachingCounters counters = new CachingCounters();
    CacheWriter cacheWriter = new CacheWriter(cacheDataSource, new DataSpec(Uri.parse("test_data")), /* temporaryBuffer= */
    null, counters);
    // DataSink.close failing must cause the operation to fail rather than succeed.
    assertThrows(IOException.class, cacheWriter::cache);
    // Since all of the bytes were read through the DataSource chain successfully before the sink
    // was closed, the progress listener will have seen all of the bytes being cached, even though
    // this may not really be the case.
    counters.assertValues(/* bytesAlreadyCached= */
    0, /* bytesNewlyCached= */
    100, /* contentLength= */
    100);
    failOnClose.set(false);
    // The bytes will be downloaded again, but cached successfully this time.
    cacheWriter.cache();
    counters.assertValues(/* bytesAlreadyCached= */
    0, /* bytesNewlyCached= */
    100, /* contentLength= */
    100);
    assertCachedData(cache, fakeDataSet);
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) FailOnCloseDataSink(com.google.android.exoplayer2.testutil.FailOnCloseDataSink) Test(org.junit.Test)

Example 38 with DataSpec

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

the class CacheWriterTest method cacheUnknownLengthPartialCaching.

@Test
public void cacheUnknownLengthPartialCaching() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().newData("test_data").setSimulateUnknownLength(true).appendReadData(TestUtil.buildTestData(100)).endData();
    FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
    Uri testUri = Uri.parse("test_data");
    DataSpec dataSpec = new DataSpec(testUri, /* position= */
    10, /* length= */
    20);
    CachingCounters counters = new CachingCounters();
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(0, 20, 20);
    counters.reset();
    cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), new DataSpec(testUri), /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(20, 80, 100);
    assertCachedData(cache, fakeDataSet);
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Uri(android.net.Uri) Test(org.junit.Test)

Example 39 with DataSpec

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

the class CacheDataSourceTest method deleteCachedWhileReadingFromUpstreamWithBlockingCacheDataSourceDoesNotBlock.

@Test
public void deleteCachedWhileReadingFromUpstreamWithBlockingCacheDataSourceDoesNotBlock() throws Exception {
    // Create a fake data source with a 1 KB default data.
    FakeDataSource upstream = new FakeDataSource();
    int dataLength = 1024;
    upstream.getDataSet().newDefaultData().appendReadData(dataLength).endData();
    // Cache the latter half of the data.
    int halfDataLength = 512;
    DataSpec dataSpec = buildDataSpec(/* position= */
    0, halfDataLength);
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream), dataSpec, /* temporaryBuffer= */
    null, /* progressListener= */
    null);
    cacheWriter.cache();
    // Create blocking CacheDataSource.
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, CacheDataSource.FLAG_BLOCK_ON_CACHE);
    cacheDataSource.open(unboundedDataSpec);
    // Read the first half from upstream as it hasn't cached yet.
    DataSourceUtil.readExactly(cacheDataSource, halfDataLength);
    // Delete the cached latter half.
    NavigableSet<CacheSpan> cachedSpans = cache.getCachedSpans(defaultCacheKey);
    for (CacheSpan cachedSpan : cachedSpans) {
        if (cachedSpan.position >= halfDataLength) {
            cache.removeSpan(cachedSpan);
        }
    }
    // Read the rest of the data.
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 40 with DataSpec

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

the class PsExtractorSeekTest method readInputLength.

// Internal methods
private long readInputLength() throws IOException {
    DataSpec dataSpec = new DataSpec(Uri.parse("asset:///" + PS_FILE_PATH));
    long totalInputLength = dataSource.open(dataSpec);
    DataSourceUtil.closeQuietly(dataSource);
    return totalInputLength;
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec)

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