Search in sources :

Example 1 with Downloader

use of androidx.media3.exoplayer.offline.Downloader in project media by androidx.

the class DefaultDownloaderFactory method createDownloader.

private Downloader createDownloader(DownloadRequest request, @C.ContentType int contentType) {
    @Nullable Constructor<? extends Downloader> constructor = CONSTRUCTORS.get(contentType);
    if (constructor == null) {
        throw new IllegalStateException("Module missing for content type " + contentType);
    }
    MediaItem mediaItem = new MediaItem.Builder().setUri(request.uri).setStreamKeys(request.streamKeys).setCustomCacheKey(request.customCacheKey).build();
    try {
        return constructor.newInstance(mediaItem, cacheDataSourceFactory, executor);
    } catch (Exception e) {
        throw new IllegalStateException("Failed to instantiate downloader for content type " + contentType);
    }
}
Also used : MediaItem(androidx.media3.common.MediaItem) Nullable(androidx.annotation.Nullable)

Example 2 with Downloader

use of androidx.media3.exoplayer.offline.Downloader in project media by androidx.

the class HlsDownloaderTest method createWithDefaultDownloaderFactory.

@Test
public void createWithDefaultDownloaderFactory() {
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(Mockito.mock(Cache.class)).setUpstreamDataSourceFactory(DummyDataSource.FACTORY);
    DownloaderFactory factory = new DefaultDownloaderFactory(cacheDataSourceFactory, /* executor= */
    Runnable::run);
    Downloader downloader = factory.createDownloader(new DownloadRequest.Builder(/* id= */
    "id", Uri.parse("https://www.test.com/download")).setMimeType(MimeTypes.APPLICATION_M3U8).setStreamKeys(Collections.singletonList(new StreamKey(/* groupIndex= */
    0, /* trackIndex= */
    0))).build());
    assertThat(downloader).isInstanceOf(HlsDownloader.class);
}
Also used : CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) DefaultDownloaderFactory(androidx.media3.exoplayer.offline.DefaultDownloaderFactory) DownloaderFactory(androidx.media3.exoplayer.offline.DownloaderFactory) DefaultDownloaderFactory(androidx.media3.exoplayer.offline.DefaultDownloaderFactory) Downloader(androidx.media3.exoplayer.offline.Downloader) DownloaderFactory(androidx.media3.exoplayer.offline.DownloaderFactory) DefaultDownloaderFactory(androidx.media3.exoplayer.offline.DefaultDownloaderFactory) StreamKey(androidx.media3.common.StreamKey) Test(org.junit.Test)

Example 3 with Downloader

use of androidx.media3.exoplayer.offline.Downloader in project media by androidx.

the class DownloadManagerTest method downloads_withSameIdsAndDifferentStreamKeys_areMerged.

@Test
public void downloads_withSameIdsAndDifferentStreamKeys_areMerged() throws Throwable {
    StreamKey streamKey1 = new StreamKey(/* groupIndex= */
    0, /* trackIndex= */
    0);
    postDownloadRequest(ID1, streamKey1);
    FakeDownloader downloader0 = getDownloaderAt(0);
    downloader0.assertId(ID1);
    downloader0.assertDownloadStarted();
    StreamKey streamKey2 = new StreamKey(/* groupIndex= */
    1, /* trackIndex= */
    1);
    postDownloadRequest(ID1, streamKey2);
    // The request for streamKey2 will cause the downloader for streamKey1 to be canceled and
    // replaced with a new downloader for both keys.
    downloader0.assertCanceled();
    FakeDownloader downloader1 = getDownloaderAt(1);
    downloader1.assertId(ID1);
    downloader1.assertStreamKeys(streamKey1, streamKey2);
    downloader1.assertDownloadStarted();
    downloader1.finish();
    assertCompleted(ID1);
    downloadManagerListener.blockUntilIdleAndThrowAnyFailure();
    assertDownloaderCount(2);
    assertDownloadIndexSize(1);
    assertCurrentDownloadCount(0);
}
Also used : StreamKey(androidx.media3.common.StreamKey) Test(org.junit.Test)

Example 4 with Downloader

use of androidx.media3.exoplayer.offline.Downloader in project media by androidx.

the class ProgressiveDownloaderTest method download_afterReadFailure_succeeds.

@Test
public void download_afterReadFailure_succeeds() throws Exception {
    Uri uri = Uri.parse("test:///test.mp4");
    // Fake data has a built in failure after 10 bytes.
    FakeDataSet data = new FakeDataSet();
    data.newData(uri).appendReadData(10).appendReadError(new IOException()).appendReadData(20);
    DataSource.Factory upstreamDataSource = new FakeDataSource.Factory().setFakeDataSet(data);
    MediaItem mediaItem = MediaItem.fromUri(uri);
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(downloadCache).setUpstreamDataSourceFactory(upstreamDataSource);
    ProgressiveDownloader downloader = new ProgressiveDownloader(mediaItem, cacheDataSourceFactory);
    TestProgressListener progressListener = new TestProgressListener();
    // Failure expected after 10 bytes.
    assertThrows(IOException.class, () -> downloader.download(progressListener));
    assertThat(progressListener.bytesDownloaded).isEqualTo(10);
    // Retry should succeed.
    downloader.download(progressListener);
    assertThat(progressListener.bytesDownloaded).isEqualTo(30);
}
Also used : FakeDataSet(androidx.media3.test.utils.FakeDataSet) IOException(java.io.IOException) Uri(android.net.Uri) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) DataSource(androidx.media3.datasource.DataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) MediaItem(androidx.media3.common.MediaItem) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) Test(org.junit.Test)

Example 5 with Downloader

use of androidx.media3.exoplayer.offline.Downloader in project media by androidx.

the class ProgressiveDownloaderTest method download_afterWriteFailureOnClose_succeeds.

@Test
public void download_afterWriteFailureOnClose_succeeds() throws Exception {
    Uri uri = Uri.parse("test:///test.mp4");
    FakeDataSet data = new FakeDataSet();
    data.newData(uri).appendReadData(1024);
    DataSource.Factory upstreamDataSource = new FakeDataSource.Factory().setFakeDataSet(data);
    AtomicBoolean failOnClose = new AtomicBoolean(/* initialValue= */
    true);
    FailOnCloseDataSink.Factory dataSinkFactory = new FailOnCloseDataSink.Factory(downloadCache, failOnClose);
    MediaItem mediaItem = MediaItem.fromUri(uri);
    CacheDataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(downloadCache).setCacheWriteDataSinkFactory(dataSinkFactory).setUpstreamDataSourceFactory(upstreamDataSource);
    ProgressiveDownloader downloader = new ProgressiveDownloader(mediaItem, cacheDataSourceFactory);
    TestProgressListener progressListener = new TestProgressListener();
    // Failure expected after 1024 bytes.
    assertThrows(IOException.class, () -> downloader.download(progressListener));
    assertThat(progressListener.bytesDownloaded).isEqualTo(1024);
    failOnClose.set(false);
    // Retry should succeed.
    downloader.download(progressListener);
    assertThat(progressListener.bytesDownloaded).isEqualTo(1024);
}
Also used : FakeDataSet(androidx.media3.test.utils.FakeDataSet) Uri(android.net.Uri) FailOnCloseDataSink(androidx.media3.test.utils.FailOnCloseDataSink) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) DataSource(androidx.media3.datasource.DataSource) FakeDataSource(androidx.media3.test.utils.FakeDataSource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FakeDataSource(androidx.media3.test.utils.FakeDataSource) MediaItem(androidx.media3.common.MediaItem) CacheDataSource(androidx.media3.datasource.cache.CacheDataSource) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 CacheDataSource (androidx.media3.datasource.cache.CacheDataSource)5 StreamKey (androidx.media3.common.StreamKey)4 MediaItem (androidx.media3.common.MediaItem)3 DefaultDownloaderFactory (androidx.media3.exoplayer.offline.DefaultDownloaderFactory)3 Downloader (androidx.media3.exoplayer.offline.Downloader)3 DownloaderFactory (androidx.media3.exoplayer.offline.DownloaderFactory)3 FakeDataSet (androidx.media3.test.utils.FakeDataSet)3 Uri (android.net.Uri)2 DataSource (androidx.media3.datasource.DataSource)2 FakeDataSource (androidx.media3.test.utils.FakeDataSource)2 Nullable (androidx.annotation.Nullable)1 FailOnCloseDataSink (androidx.media3.test.utils.FailOnCloseDataSink)1 IOException (java.io.IOException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1