Search in sources :

Example 16 with CacheDataSource

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

the class CacheDataSourceContractTest method createDataSource.

@Override
protected DataSource createDataSource() throws IOException {
    File tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    SimpleCache cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    upstreamDataSource = new FakeDataSource(fakeDataSet);
    return new CacheDataSource(cache, upstreamDataSource);
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) File(java.io.File)

Example 17 with CacheDataSource

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

the class CacheDataSourceTest2 method testReads.

private void testReads(boolean useEncryption) throws IOException {
    FakeDataSource upstreamSource = buildFakeUpstreamSource();
    CacheDataSource source = buildCacheDataSource(ApplicationProvider.getApplicationContext(), upstreamSource, useEncryption);
    // First read, should arrive from upstream.
    testRead(END_ON_BOUNDARY, source);
    assertSingleOpen(upstreamSource, 0, OFFSET_ON_BOUNDARY);
    // Second read, should arrive from upstream.
    testRead(START_OFF_BOUNDARY, source);
    assertSingleOpen(upstreamSource, OFFSET_OFF_BOUNDARY, DATA.length);
    // Second read, should arrive part from cache and part from upstream.
    testRead(END_OFF_BOUNDARY, source);
    assertSingleOpen(upstreamSource, OFFSET_ON_BOUNDARY, OFFSET_OFF_BOUNDARY);
    // Third read, should arrive from cache.
    testRead(FULL, source);
    assertNoOpen(upstreamSource);
    // Various reads, should all arrive from cache.
    testRead(FULL, source);
    assertNoOpen(upstreamSource);
    testRead(START_ON_BOUNDARY, source);
    assertNoOpen(upstreamSource);
    testRead(END_ON_BOUNDARY, source);
    assertNoOpen(upstreamSource);
    testRead(START_OFF_BOUNDARY, source);
    assertNoOpen(upstreamSource);
    testRead(END_OFF_BOUNDARY, source);
    assertNoOpen(upstreamSource);
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource)

Example 18 with CacheDataSource

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

the class CacheDataSourceTest method switchToCacheSourceWithReadOnlyCacheDataSource.

@Test
public void switchToCacheSourceWithReadOnlyCacheDataSource() throws Exception {
    // Create a fake data source with a 1 MB default data.
    FakeDataSource upstream = new FakeDataSource();
    FakeData fakeData = upstream.getDataSet().newDefaultData().appendReadData(1024 * 1024 - 1);
    // Insert an action just before the end of the data to fail the test if reading from upstream
    // reaches end of the data.
    fakeData.appendReadAction(() -> fail("Read from upstream shouldn't reach to the end of the data.")).appendReadData(1);
    // 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);
    byte[] buffer = new byte[1024];
    cacheDataSource.read(buffer, 0, buffer.length);
    // Cache the data. Although we use another FakeDataSource instance, it shouldn't matter.
    FakeDataSource upstream2 = new FakeDataSource(new FakeDataSource().getDataSet().newDefaultData().appendReadData(1024 * 1024).endData());
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, upstream2), unboundedDataSpec, /* temporaryBuffer= */
    null, /* progressListener= */
    null);
    cacheWriter.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) FakeData(com.google.android.exoplayer2.testutil.FakeDataSet.FakeData) Test(org.junit.Test)

Example 19 with CacheDataSource

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

the class CacheDataSourceTest method boundedRead_doesNotSetContentLength.

@Test
public void boundedRead_doesNotSetContentLength() throws Exception {
    DataSpec dataSpec = buildDataSpec(0, TEST_DATA.length);
    // Read up to the end of the data, but since the DataSpec is bounded, the read doesn't see the
    // EOS, and so the content length remains unknown.
    CacheDataSource cacheDataSource = createCacheDataSource(false, true);
    assertReadData(cacheDataSource, dataSpec, true);
    assertThat(ContentMetadata.getContentLength(cache.getContentMetadata(defaultCacheKey))).isEqualTo(C.LENGTH_UNSET);
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) Test(org.junit.Test)

Example 20 with CacheDataSource

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

the class CacheWriterTest method cacheLengthExceedsActualDataLength.

@Test
public void cacheLengthExceedsActualDataLength() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().setRandomData("test_data", 100);
    FakeDataSource dataSource = new FakeDataSource(fakeDataSet);
    Uri testUri = Uri.parse("test_data");
    DataSpec dataSpec = new DataSpec(testUri, /* position= */
    0, /* length= */
    1000);
    CachingCounters counters = new CachingCounters();
    CacheWriter cacheWriter = new CacheWriter(new CacheDataSource(cache, dataSource), dataSpec, /* temporaryBuffer= */
    null, counters);
    cacheWriter.cache();
    counters.assertValues(0, 100, 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