Search in sources :

Example 1 with Cache

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

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

the class DemoUtil method getDownloadCache.

private static synchronized Cache getDownloadCache(Context context) {
    if (downloadCache == null) {
        File downloadContentDirectory = new File(getDownloadDirectory(context), DOWNLOAD_CONTENT_DIRECTORY);
        downloadCache = new SimpleCache(downloadContentDirectory, new NoOpCacheEvictor(), getDatabaseProvider(context));
    }
    return downloadCache;
}
Also used : SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) NoOpCacheEvictor(com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor) File(java.io.File)

Example 3 with Cache

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

the class DefaultDrmSessionManagerProviderTest method create_reusesCachedInstanceWherePossible.

@Test
public void create_reusesCachedInstanceWherePossible() {
    MediaItem mediaItem1 = new MediaItem.Builder().setUri("https://example.test/content-1").setDrmConfiguration(new MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID).build()).build();
    // Same DRM info as item1, but different URL to check it doesn't prevent re-using a manager.
    MediaItem mediaItem2 = new MediaItem.Builder().setUri("https://example.test/content-2").setDrmConfiguration(new MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID).build()).build();
    // Different DRM info to 1 and 2, needs a different manager instance.
    MediaItem mediaItem3 = new MediaItem.Builder().setUri("https://example.test/content-3").setDrmConfiguration(new MediaItem.DrmConfiguration.Builder(C.WIDEVINE_UUID).setLicenseUri("https://example.test/license").build()).build();
    DefaultDrmSessionManagerProvider provider = new DefaultDrmSessionManagerProvider();
    DrmSessionManager drmSessionManager1 = provider.get(mediaItem1);
    DrmSessionManager drmSessionManager2 = provider.get(mediaItem2);
    DrmSessionManager drmSessionManager3 = provider.get(mediaItem3);
    // Get a manager for the first item again - expect it to be a different instance to last time
    // since we only cache one.
    DrmSessionManager drmSessionManager4 = provider.get(mediaItem1);
    assertThat(drmSessionManager1).isSameInstanceAs(drmSessionManager2);
    assertThat(drmSessionManager1).isNotSameInstanceAs(drmSessionManager3);
    assertThat(drmSessionManager1).isNotSameInstanceAs(drmSessionManager4);
}
Also used : MediaItem(com.google.android.exoplayer2.MediaItem) DrmSessionManager(com.google.android.exoplayer2.drm.DrmSessionManager) DefaultDrmSessionManagerProvider(com.google.android.exoplayer2.drm.DefaultDrmSessionManagerProvider) Test(org.junit.Test)

Example 4 with Cache

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

Example 5 with Cache

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

the class CacheDataSourceTest method setUp.

@Before
public void setUp() throws Exception {
    testDataUri = Uri.parse("https://www.test.com/data");
    httpRequestHeaders = new HashMap<>();
    httpRequestHeaders.put("Test-key", "Test-val");
    unboundedDataSpec = buildDataSpec(/* unbounded= */
    true, /* key= */
    null);
    boundedDataSpec = buildDataSpec(/* unbounded= */
    false, /* key= */
    null);
    unboundedDataSpecWithKey = buildDataSpec(/* unbounded= */
    true, DATASPEC_KEY);
    boundedDataSpecWithKey = buildDataSpec(/* unbounded= */
    false, DATASPEC_KEY);
    defaultCacheKey = CacheKeyFactory.DEFAULT.buildCacheKey(unboundedDataSpec);
    customCacheKey = "customKey." + defaultCacheKey;
    cacheKeyFactory = dataSpec -> customCacheKey;
    tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    upstreamDataSource = new FakeDataSource();
}
Also used : FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) Before(org.junit.Before)

Aggregations

Test (org.junit.Test)25 FakeDataSource (com.google.android.exoplayer2.testutil.FakeDataSource)19 FakeDataSet (com.google.android.exoplayer2.testutil.FakeDataSet)18 DataSpec (com.google.android.exoplayer2.upstream.DataSpec)16 StreamKey (com.google.android.exoplayer2.offline.StreamKey)9 SimpleCache (com.google.android.exoplayer2.upstream.cache.SimpleCache)9 DataSource (com.google.android.exoplayer2.upstream.DataSource)8 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)8 File (java.io.File)8 Uri (android.net.Uri)7 FileDataSource (com.google.android.exoplayer2.upstream.FileDataSource)6 NoOpCacheEvictor (com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor)6 IOException (java.io.IOException)6 Before (org.junit.Before)6 RequestSet (com.google.android.exoplayer2.testutil.CacheAsserts.RequestSet)5 Nullable (androidx.annotation.Nullable)3 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 Context (android.content.Context)2 SQLException (android.database.SQLException)2