use of com.google.android.exoplayer2.offline.Downloader in project ExoPlayer by google.
the class DashDownloaderTest 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_MPD).setStreamKeys(Collections.singletonList(new StreamKey(/* groupIndex= */
0, /* trackIndex= */
0))).build());
assertThat(downloader).isInstanceOf(DashDownloader.class);
}
use of com.google.android.exoplayer2.offline.Downloader in project ExoPlayer by google.
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 com.google.android.exoplayer2.offline.Downloader in project Slide by ccrama.
the class GifUtils method cacheSaveGif.
/**
* Temporarily cache or permanently save a GIF
*
* @param uri URL of the GIF
* @param a
* @param subreddit Subreddit for saving in sub-specific folders
* @param save Whether to permanently save the GIF of just temporarily cache it
*/
public static void cacheSaveGif(Uri uri, Activity a, String subreddit, String submissionTitle, boolean save) {
if (save) {
try {
Toast.makeText(a, a.getString(R.string.mediaview_notif_title), Toast.LENGTH_SHORT).show();
} catch (Exception ignored) {
}
}
if (Reddit.appRestart.getString("imagelocation", "").isEmpty()) {
showFirstDialog(a);
} else if (!new File(Reddit.appRestart.getString("imagelocation", "")).exists()) {
showErrorDialog(a);
} else {
new AsyncTask<Void, Integer, Boolean>() {
File outFile;
NotificationManager notifMgr = ContextCompat.getSystemService(a, NotificationManager.class);
@Override
protected void onPreExecute() {
super.onPreExecute();
if (save) {
Notification notif = new NotificationCompat.Builder(a, Reddit.CHANNEL_IMG).setContentTitle(a.getString(R.string.mediaview_saving, uri.toString().replace("/DASHPlaylist.mpd", ""))).setSmallIcon(R.drawable.ic_download).setProgress(0, 0, true).setOngoing(true).build();
notifMgr.notify(1, notif);
}
}
@Override
protected Boolean doInBackground(Void... voids) {
String folderPath = Reddit.appRestart.getString("imagelocation", "");
String subFolderPath = "";
if (SettingValues.imageSubfolders && !subreddit.isEmpty()) {
subFolderPath = File.separator + subreddit;
}
String extension = ".mp4";
outFile = FileUtil.getValidFile(folderPath, subFolderPath, submissionTitle, "", extension);
OutputStream out = null;
InputStream in = null;
try {
DataSource.Factory downloader = new OkHttpDataSource.Factory(Reddit.client).setUserAgent(a.getString(R.string.app_name));
DataSource.Factory cacheDataSourceFactory = new CacheDataSource.Factory().setCache(Reddit.videoCache).setUpstreamDataSourceFactory(downloader);
if (uri.getLastPathSegment().endsWith("DASHPlaylist.mpd")) {
InputStream dashManifestStream = new DataSourceInputStream(cacheDataSourceFactory.createDataSource(), new DataSpec(uri));
DashManifest dashManifest = new DashManifestParser().parse(uri, dashManifestStream);
dashManifestStream.close();
Uri audioUri = null;
Uri videoUri = null;
for (int i = 0; i < dashManifest.getPeriodCount(); i++) {
for (AdaptationSet as : dashManifest.getPeriod(i).adaptationSets) {
boolean isAudio = false;
int bitrate = 0;
String hqUri = null;
for (Representation r : as.representations) {
if (r.format.bitrate > bitrate) {
bitrate = r.format.bitrate;
hqUri = r.baseUrl;
}
if (MimeTypes.isAudio(r.format.sampleMimeType)) {
isAudio = true;
}
}
if (isAudio) {
audioUri = Uri.parse(hqUri);
} else {
videoUri = Uri.parse(hqUri);
}
}
}
if (audioUri != null) {
LogUtil.v("Downloading DASH audio from: " + audioUri);
DataSourceInputStream audioInputStream = new DataSourceInputStream(cacheDataSourceFactory.createDataSource(), new DataSpec(audioUri));
if (save) {
FileUtils.copyInputStreamToFile(audioInputStream, new File(a.getCacheDir().getAbsolutePath(), "audio.mp4"));
} else {
IOUtils.copy(audioInputStream, NullOutputStream.NULL_OUTPUT_STREAM);
}
audioInputStream.close();
}
if (videoUri != null) {
LogUtil.v("Downloading DASH video from: " + videoUri);
DataSourceInputStream videoInputStream = new DataSourceInputStream(cacheDataSourceFactory.createDataSource(), new DataSpec(videoUri));
if (save) {
FileUtils.copyInputStreamToFile(videoInputStream, new File(a.getCacheDir().getAbsolutePath(), "video.mp4"));
} else {
IOUtils.copy(videoInputStream, NullOutputStream.NULL_OUTPUT_STREAM);
}
videoInputStream.close();
}
if (!save) {
return true;
} else if (audioUri != null && videoUri != null) {
if (mux(new File(a.getCacheDir().getAbsolutePath(), "video.mp4").getAbsolutePath(), new File(a.getCacheDir().getAbsolutePath(), "audio.mp4").getAbsolutePath(), new File(a.getCacheDir().getAbsolutePath(), "muxed.mp4").getAbsolutePath())) {
in = new FileInputStream(new File(a.getCacheDir().getAbsolutePath(), "muxed.mp4"));
} else {
throw new IOException("Muxing failed!");
}
} else {
in = new FileInputStream(new File(a.getCacheDir().getAbsolutePath(), "video.mp4"));
}
} else {
in = new DataSourceInputStream(cacheDataSourceFactory.createDataSource(), new DataSpec(uri));
}
out = save ? new FileOutputStream(outFile) : NullOutputStream.NULL_OUTPUT_STREAM;
IOUtils.copy(in, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
LogUtil.e("Error saving GIF called with: " + "from = [" + uri + "], in = [" + in + "]");
return false;
} finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
LogUtil.e("Error closing GIF called with: " + "from = [" + uri + "], out = [" + out + "]");
return false;
}
}
return true;
}
@Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
if (save) {
notifMgr.cancel(1);
if (success) {
doNotifGif(outFile, a);
} else {
showErrorDialog(a);
}
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
use of com.google.android.exoplayer2.offline.Downloader 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.offline.Downloader 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);
}
Aggregations