Search in sources :

Example 1 with FileDataSource

use of androidx.media3.datasource.FileDataSource in project media by androidx.

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(androidx.media3.test.utils.FakeDataSource) FileDataSource(androidx.media3.datasource.FileDataSource) FakeData(androidx.media3.test.utils.FakeDataSet.FakeData) Test(org.junit.Test)

Example 2 with FileDataSource

use of androidx.media3.datasource.FileDataSource in project media by androidx.

the class CacheDataSourceTest2 method buildCacheDataSource.

private static CacheDataSource buildCacheDataSource(Context context, DataSource upstreamSource, boolean useAesEncryption) throws CacheException {
    File cacheDir = context.getExternalCacheDir();
    Cache cache = new SimpleCache(new File(cacheDir, EXO_CACHE_DIR), new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    emptyCache(cache);
    // Source and cipher
    final String secretKey = "testKey:12345678";
    DataSource file = new FileDataSource();
    DataSource cacheReadDataSource = useAesEncryption ? new AesCipherDataSource(Util.getUtf8Bytes(secretKey), file) : file;
    // Sink and cipher
    CacheDataSink cacheSink = new CacheDataSink(cache, EXO_CACHE_MAX_FILESIZE);
    byte[] scratch = new byte[3897];
    DataSink cacheWriteDataSink = useAesEncryption ? new AesCipherDataSink(Util.getUtf8Bytes(secretKey), cacheSink, scratch) : cacheSink;
    return new CacheDataSource(cache, upstreamSource, cacheReadDataSource, cacheWriteDataSink, CacheDataSource.FLAG_BLOCK_ON_CACHE, // eventListener
    null);
}
Also used : DataSink(androidx.media3.datasource.DataSink) AesCipherDataSink(androidx.media3.datasource.AesCipherDataSink) FileDataSource(androidx.media3.datasource.FileDataSource) DataSource(androidx.media3.datasource.DataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) AesCipherDataSource(androidx.media3.datasource.AesCipherDataSource) AesCipherDataSink(androidx.media3.datasource.AesCipherDataSink) FileDataSource(androidx.media3.datasource.FileDataSource) AesCipherDataSource(androidx.media3.datasource.AesCipherDataSource) File(java.io.File)

Example 3 with FileDataSource

use of androidx.media3.datasource.FileDataSource in project media by androidx.

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(androidx.media3.test.utils.FakeDataSource) FileDataSource(androidx.media3.datasource.FileDataSource) DataSpec(androidx.media3.datasource.DataSpec) Test(org.junit.Test)

Example 4 with FileDataSource

use of androidx.media3.datasource.FileDataSource in project media by androidx.

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(androidx.media3.test.utils.FakeDataSource) FakeDataSet(androidx.media3.test.utils.FakeDataSet) FileDataSource(androidx.media3.datasource.FileDataSource) DataSpec(androidx.media3.datasource.DataSpec) FailOnCloseDataSink(androidx.media3.test.utils.FailOnCloseDataSink) Test(org.junit.Test)

Aggregations

FileDataSource (androidx.media3.datasource.FileDataSource)4 FakeDataSource (androidx.media3.test.utils.FakeDataSource)4 Test (org.junit.Test)3 DataSpec (androidx.media3.datasource.DataSpec)2 AesCipherDataSink (androidx.media3.datasource.AesCipherDataSink)1 AesCipherDataSource (androidx.media3.datasource.AesCipherDataSource)1 DataSink (androidx.media3.datasource.DataSink)1 DataSource (androidx.media3.datasource.DataSource)1 FailOnCloseDataSink (androidx.media3.test.utils.FailOnCloseDataSink)1 FakeDataSet (androidx.media3.test.utils.FakeDataSet)1 FakeData (androidx.media3.test.utils.FakeDataSet.FakeData)1 File (java.io.File)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1