use of com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project react-native-track-player by react-native-kit.
the class ExoPlayback method load.
@Override
public void load(Track track, Promise callback) {
loadCallback = callback;
Uri url = track.url;
String userAgent = Util.getUserAgent(context, "react-native-track-player");
DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
DataSource.Factory factory = new DefaultDataSourceFactory(context, null, httpDataSourceFactory);
MediaSource source;
if (cacheMaxSize > 0 && !track.urlLocal) {
File cacheDir = new File(context.getCacheDir(), "TrackPlayer");
Cache cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(cacheMaxSize));
factory = new CacheDataSourceFactory(cache, factory, 0, cacheMaxSize);
}
if (track.type == TrackType.DASH) {
source = new DashMediaSource(url, factory, new DefaultDashChunkSource.Factory(factory), null, null);
} else if (track.type == TrackType.HLS) {
source = new HlsMediaSource(url, factory, null, null);
} else if (track.type == TrackType.SMOOTH_STREAMING) {
source = new SsMediaSource(url, factory, new DefaultSsChunkSource.Factory(factory), null, null);
} else {
source = new ExtractorMediaSource(url, factory, new DefaultExtractorsFactory(), null, null);
}
player.prepare(source);
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project ExoPlayer by google.
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);
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project ExoPlayer by google.
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 com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project ExoPlayer by google.
the class SsDownloaderTest method createWithDefaultDownloaderFactory.
@Test
public void createWithDefaultDownloaderFactory() throws Exception {
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_SS).setStreamKeys(Collections.singletonList(new StreamKey(/* groupIndex= */
0, /* trackIndex= */
0))).build());
assertThat(downloader).isInstanceOf(SsDownloader.class);
}
use of com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory in project ExoPlayer by google.
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);
}
}
Aggregations