Search in sources :

Example 1 with CacheDataSource

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

the class CacheDataSourceTest method testUnsatisfiableRange.

public void testUnsatisfiableRange() throws Exception {
    // Bounded request but the content length is unknown. This forces all data to be cached but not
    // the length
    assertCacheAndRead(false, true);
    // Now do an unbounded request. This will read all of the data from cache and then try to read
    // more from upstream which will cause to a 416 so CDS will store the length.
    CacheDataSource cacheDataSource = createCacheDataSource(true, true);
    assertReadDataContentLength(cacheDataSource, true, true);
    // If the user try to access off range then it should throw an IOException
    try {
        cacheDataSource = createCacheDataSource(false, false);
        cacheDataSource.open(new DataSpec(Uri.EMPTY, TEST_DATA.length, 5, KEY_1));
        fail();
    } catch (IOException e) {
    // success
    }
}
Also used : DataSpec(com.google.android.exoplayer2.upstream.DataSpec) IOException(java.io.IOException)

Example 2 with CacheDataSource

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

the class CacheDataSourceTest method createCacheDataSource.

private CacheDataSource createCacheDataSource(boolean setReadException, boolean simulateUnknownLength, @CacheDataSource.Flags int flags, CacheDataSink cacheWriteDataSink) {
    FakeDataSource.Builder builder = new FakeDataSource.Builder();
    if (setReadException) {
        builder.appendReadError(new IOException("Shouldn't read from upstream"));
    }
    FakeDataSource upstream = builder.setSimulateUnknownLength(simulateUnknownLength).appendReadData(TEST_DATA).build();
    return new CacheDataSource(simpleCache, upstream, new FileDataSource(), cacheWriteDataSink, flags, null);
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) IOException(java.io.IOException)

Example 3 with CacheDataSource

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

the class SlideVideoControls method createDataSource.

@Override
public DataSource createDataSource() {
    LeastRecentlyUsedCacheEvictor evictor = new LeastRecentlyUsedCacheEvictor(maxCacheSize);
    SimpleCache simpleCache = new SimpleCache(new File(context.getCacheDir(), "media"), evictor);
    return new CacheDataSource(simpleCache, defaultDatasourceFactory.createDataSource(), new FileDataSource(), new CacheDataSink(simpleCache, maxFileSize), CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR, null);
}
Also used : CacheDataSink(com.google.android.exoplayer2.upstream.cache.CacheDataSink) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) LeastRecentlyUsedCacheEvictor(com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) File(java.io.File)

Example 4 with CacheDataSource

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

the class SegmentDownloader method remove.

@Override
public final void remove() {
    CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload();
    try {
        M manifest = getManifest(dataSource, manifestDataSpec, /* removing= */
        true);
        List<Segment> segments = getSegments(dataSource, manifest, /* removing= */
        true);
        for (int i = 0; i < segments.size(); i++) {
            cache.removeResource(cacheKeyFactory.buildCacheKey(segments.get(i).dataSpec));
        }
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (Exception e) {
    // Ignore exceptions when removing.
    } finally {
        // Always attempt to remove the manifest.
        cache.removeResource(cacheKeyFactory.buildCacheKey(manifestDataSpec));
    }
}
Also used : CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) PriorityTooLowException(com.google.android.exoplayer2.util.PriorityTaskManager.PriorityTooLowException)

Example 5 with CacheDataSource

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

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 : AesCipherDataSink(com.google.android.exoplayer2.upstream.crypto.AesCipherDataSink) DataSink(com.google.android.exoplayer2.upstream.DataSink) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) AesCipherDataSource(com.google.android.exoplayer2.upstream.crypto.AesCipherDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) AesCipherDataSink(com.google.android.exoplayer2.upstream.crypto.AesCipherDataSink) FileDataSource(com.google.android.exoplayer2.upstream.FileDataSource) AesCipherDataSource(com.google.android.exoplayer2.upstream.crypto.AesCipherDataSource) File(java.io.File)

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