Search in sources :

Example 1 with CacheDataSource

use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.

the class CacheAsserts method assertDataCached.

/**
 * Asserts that the cache contains the given data for {@code dataSpec}.
 *
 * @throws IOException If an error occurred reading from the Cache.
 */
public static void assertDataCached(Cache cache, DataSpec dataSpec, byte[] expected) throws IOException {
    DataSource dataSource = new CacheDataSource(cache, DummyDataSource.INSTANCE, 0);
    byte[] bytes;
    try {
        dataSource.open(dataSpec);
        bytes = DataSourceUtil.readToEnd(dataSource);
    } catch (IOException e) {
        throw new IOException("Opening/reading cache failed: " + dataSpec, e);
    } finally {
        dataSource.close();
    }
    assertWithMessage("Cached data doesn't match expected for '" + dataSpec.uri + "',").that(bytes).isEqualTo(expected);
}
Also used : CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) IOException(java.io.IOException) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) DummyDataSource(androidx.media3.datasource.DummyDataSource) DataSource(androidx.media3.datasource.DataSource)

Example 2 with CacheDataSource

use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.

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

Example 3 with CacheDataSource

use of androidx.media3.datasource.cache.CacheDataSource 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 4 with CacheDataSource

use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.

the class CacheDataSourceTest method unknownLengthContentReadInOneConnectionAndLengthIsResolved.

@Test
public void unknownLengthContentReadInOneConnectionAndLengthIsResolved() throws Exception {
    FakeDataSource upstream = new FakeDataSource();
    upstream.getDataSet().newData(testDataUri).appendReadData(TEST_DATA).setSimulateUnknownLength(true);
    CacheDataSource cacheDataSource = new CacheDataSource(cache, upstream, 0);
    cacheDataSource.open(unboundedDataSpec);
    DataSourceUtil.readToEnd(cacheDataSource);
    cacheDataSource.close();
    assertThat(upstream.getAndClearOpenedDataSpecs()).hasLength(1);
    assertThat(ContentMetadata.getContentLength(cache.getContentMetadata(defaultCacheKey))).isEqualTo(TEST_DATA.length);
}
Also used : FakeDataSource(androidx.media3.test.utils.FakeDataSource) Test(org.junit.Test)

Example 5 with CacheDataSource

use of androidx.media3.datasource.cache.CacheDataSource in project media by androidx.

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

Aggregations

FakeDataSource (androidx.media3.test.utils.FakeDataSource)16 Test (org.junit.Test)15 DataSpec (androidx.media3.datasource.DataSpec)12 FakeDataSet (androidx.media3.test.utils.FakeDataSet)7 FileDataSource (androidx.media3.datasource.FileDataSource)4 Uri (android.net.Uri)3 CacheDataSource (androidx.media3.datasource.cache.CacheDataSource)3 IOException (java.io.IOException)3 PriorityTooLowException (androidx.media3.common.PriorityTaskManager.PriorityTooLowException)2 DataSource (androidx.media3.datasource.DataSource)2 FakeData (androidx.media3.test.utils.FakeDataSet.FakeData)2 File (java.io.File)2 ExecutionException (java.util.concurrent.ExecutionException)2 Nullable (androidx.annotation.Nullable)1 AesCipherDataSink (androidx.media3.datasource.AesCipherDataSink)1 AesCipherDataSource (androidx.media3.datasource.AesCipherDataSource)1 DataSink (androidx.media3.datasource.DataSink)1 DummyDataSource (androidx.media3.datasource.DummyDataSource)1 FailOnCloseDataSink (androidx.media3.test.utils.FailOnCloseDataSink)1 ArrayDeque (java.util.ArrayDeque)1