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);
}
}
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);
}
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);
}
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);
}
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);
}
Aggregations