Search in sources :

Example 16 with Cache

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

the class DashDownloadTest method download.

// Download tests
@Test
public void download() throws Exception {
    DashDownloader dashDownloader = downloadContent();
    dashDownloader.download(/* progressListener= */
    null);
    testRunner.setStreamName("test_h264_fixed_download").setDataSourceFactory(offlineDataSourceFactory).run();
    dashDownloader.remove();
    assertWithMessage("There should be no cache key left").that(cache.getKeys()).isEmpty();
    assertWithMessage("There should be no content left").that(cache.getCacheSpace()).isEqualTo(0);
}
Also used : DashDownloader(com.google.android.exoplayer2.source.dash.offline.DashDownloader) Test(org.junit.Test)

Example 17 with Cache

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

the class DashDownloadTest method downloadContent.

private DashDownloader downloadContent() throws Exception {
    DashManifest dashManifest = DashUtil.loadManifest(httpDataSourceFactory.createDataSource(), MANIFEST_URI);
    ArrayList<StreamKey> keys = new ArrayList<>();
    for (int pIndex = 0; pIndex < dashManifest.getPeriodCount(); pIndex++) {
        List<AdaptationSet> adaptationSets = dashManifest.getPeriod(pIndex).adaptationSets;
        for (int aIndex = 0; aIndex < adaptationSets.size(); aIndex++) {
            AdaptationSet adaptationSet = adaptationSets.get(aIndex);
            List<Representation> representations = adaptationSet.representations;
            for (int rIndex = 0; rIndex < representations.size(); rIndex++) {
                String id = representations.get(rIndex).format.id;
                if (DashTestData.AAC_AUDIO_REPRESENTATION_ID.equals(id) || DashTestData.H264_CDD_FIXED.equals(id)) {
                    keys.add(new StreamKey(pIndex, aIndex, rIndex));
                }
            }
        }
    }
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(httpDataSourceFactory);
    return new DashDownloader(new MediaItem.Builder().setUri(MANIFEST_URI).setStreamKeys(keys).build(), cacheDataSourceFactory);
}
Also used : DashDownloader(com.google.android.exoplayer2.source.dash.offline.DashDownloader) DashManifest(com.google.android.exoplayer2.source.dash.manifest.DashManifest) ArrayList(java.util.ArrayList) Representation(com.google.android.exoplayer2.source.dash.manifest.Representation) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) AdaptationSet(com.google.android.exoplayer2.source.dash.manifest.AdaptationSet) StreamKey(com.google.android.exoplayer2.offline.StreamKey)

Example 18 with Cache

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

the class DownloadServiceDashTest method setUp.

@Before
public void setUp() throws IOException {
    testThread = new DummyMainThread();
    context = ApplicationProvider.getApplicationContext();
    tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
    cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    Runnable pauseAction = () -> {
        if (pauseDownloadCondition != null) {
            try {
                pauseDownloadCondition.block();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
        }
    };
    fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).newData("audio_init_data").appendReadAction(pauseAction).appendReadData(TestUtil.buildTestData(10)).endData().setRandomData("audio_segment_1", 4).setRandomData("audio_segment_2", 5).setRandomData("audio_segment_3", 6).setRandomData("text_segment_1", 1).setRandomData("text_segment_2", 2).setRandomData("text_segment_3", 3);
    final DataSource.Factory fakeDataSourceFactory = new FakeDataSource.Factory().setFakeDataSet(fakeDataSet);
    fakeStreamKey1 = new StreamKey(0, 0, 0);
    fakeStreamKey2 = new StreamKey(0, 1, 0);
    testThread.runTestOnMainThread(() -> {
        DefaultDownloadIndex downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
        DefaultDownloaderFactory downloaderFactory = new DefaultDownloaderFactory(new CacheDataSource.Factory().setCache(cache).setUpstreamDataSourceFactory(fakeDataSourceFactory), /* executor= */
        Runnable::run);
        final DownloadManager dashDownloadManager = new DownloadManager(ApplicationProvider.getApplicationContext(), downloadIndex, downloaderFactory);
        downloadManagerListener = new TestDownloadManagerListener(dashDownloadManager);
        dashDownloadManager.resumeDownloads();
        dashDownloadService = new DownloadService(DownloadService.FOREGROUND_NOTIFICATION_ID_NONE) {

            @Override
            protected DownloadManager getDownloadManager() {
                return dashDownloadManager;
            }

            @Override
            @Nullable
            protected Scheduler getScheduler() {
                return null;
            }

            @Override
            protected Notification getForegroundNotification(List<Download> downloads, @Requirements.RequirementFlags int notMetRequirements) {
                throw new UnsupportedOperationException();
            }
        };
        dashDownloadService.onCreate();
    });
}
Also used : Scheduler(com.google.android.exoplayer2.scheduler.Scheduler) DownloadManager(com.google.android.exoplayer2.offline.DownloadManager) DownloadService(com.google.android.exoplayer2.offline.DownloadService) Notification(android.app.Notification) SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DefaultDownloaderFactory(com.google.android.exoplayer2.offline.DefaultDownloaderFactory) Download(com.google.android.exoplayer2.offline.Download) NoOpCacheEvictor(com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) CacheDataSource(com.google.android.exoplayer2.upstream.cache.CacheDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) FakeDataSource(com.google.android.exoplayer2.testutil.FakeDataSource) DefaultDownloadIndex(com.google.android.exoplayer2.offline.DefaultDownloadIndex) DummyMainThread(com.google.android.exoplayer2.testutil.DummyMainThread) StreamKey(com.google.android.exoplayer2.offline.StreamKey) Nullable(androidx.annotation.Nullable) TestDownloadManagerListener(com.google.android.exoplayer2.robolectric.TestDownloadManagerListener) Before(org.junit.Before)

Example 19 with Cache

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

the class DashDownloaderTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    tempFolder = Util.createTempDirectory(ApplicationProvider.getApplicationContext(), "ExoPlayerTest");
    cache = new SimpleCache(tempFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
    progressListener = new ProgressListener();
}
Also used : SimpleCache(com.google.android.exoplayer2.upstream.cache.SimpleCache) NoOpCacheEvictor(com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor) Before(org.junit.Before)

Example 20 with Cache

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

the class DashDownloaderTest method representationWithoutIndex.

@Test
public void representationWithoutIndex() throws Exception {
    FakeDataSet fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD_NO_INDEX).setRandomData("test_segment_1", 4);
    DashDownloader dashDownloader = getDashDownloader(fakeDataSet, new StreamKey(0, 0, 0));
    try {
        dashDownloader.download(progressListener);
        fail();
    } catch (DownloadException e) {
    // Expected.
    }
    dashDownloader.remove();
    assertCacheEmpty(cache);
}
Also used : DownloadException(com.google.android.exoplayer2.offline.DownloadException) FakeDataSet(com.google.android.exoplayer2.testutil.FakeDataSet) StreamKey(com.google.android.exoplayer2.offline.StreamKey) Test(org.junit.Test)

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 SimpleCache (com.google.android.exoplayer2.upstream.cache.SimpleCache)10 StreamKey (com.google.android.exoplayer2.offline.StreamKey)9 File (java.io.File)9 DataSource (com.google.android.exoplayer2.upstream.DataSource)8 CacheDataSource (com.google.android.exoplayer2.upstream.cache.CacheDataSource)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 LeastRecentlyUsedCacheEvictor (com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor)3 Notification (android.app.Notification)2 NotificationManager (android.app.NotificationManager)2 Context (android.content.Context)2