Search in sources :

Example 6 with CacheDataSource

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

the class CacheDataSourceTest method propagatesHttpHeadersUpstream.

@Test
public void propagatesHttpHeadersUpstream() throws Exception {
    CacheDataSource cacheDataSource = createCacheDataSource(/* setReadException= */
    false, /* unknownLength= */
    false);
    DataSpec dataSpec = buildDataSpec(/* position= */
    2, /* length= */
    5);
    cacheDataSource.open(dataSpec);
    DataSpec[] upstreamDataSpecs = upstreamDataSource.getAndClearOpenedDataSpecs();
    assertThat(upstreamDataSpecs).hasLength(1);
    assertThat(upstreamDataSpecs[0].httpRequestHeaders).isEqualTo(this.httpRequestHeaders);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 7 with CacheDataSource

use of com.google.android.exoplayer2.upstream.cache.CacheDataSource 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 8 with CacheDataSource

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

the class CacheDataSourceTest method ignoreCacheForUnsetLengthRequests.

@Test
public void ignoreCacheForUnsetLengthRequests() throws Exception {
    FakeDataSource upstream = new FakeDataSource();
    upstream.getDataSet().setData(testDataUri, TEST_DATA);
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, CacheDataSource.FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS);
    cacheDataSource.open(unboundedDataSpec);
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
    assertThat(cache.getKeys()).isEmpty();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) Test(org.junit.Test)

Example 9 with CacheDataSource

use of com.google.android.exoplayer2.upstream.cache.CacheDataSource 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 10 with CacheDataSource

use of com.google.android.exoplayer2.upstream.cache.CacheDataSource 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)

Aggregations

FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)17 Test (org.junit.Test)15 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)13 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)7 FileDataSource (com.google.android.exoplayer2.upstream.FileDataSource)7 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)5 IOException (java.io.IOException)5 Uri (android.net.Uri)3 File (java.io.File)3 FakeData (com.google.android.exoplayer2.testutil.FakeDataSet.FakeData)2 DataSource (com.google.android.exoplayer2.upstream.DataSource)2 CacheDataSink (com.google.android.exoplayer2.upstream.cache.CacheDataSink)2 PriorityTooLowException (com.google.android.exoplayer2.util.PriorityTaskManager.PriorityTooLowException)2 ExecutionException (java.util.concurrent.ExecutionException)2 Nullable (androidx.annotation.Nullable)1 FailOnCloseDataSink (com.google.android.exoplayer2.testutil.FailOnCloseDataSink)1 DataSink (com.google.android.exoplayer2.upstream.DataSink)1 DefaultDataSource (com.google.android.exoplayer2.upstream.DefaultDataSource)1 DummyDataSource (com.google.android.exoplayer2.upstream.DummyDataSource)1 LeastRecentlyUsedCacheEvictor (com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor)1