Make use of MediaItem in ProgressiveDownloader

PiperOrigin-RevId: 311513746
This commit is contained in:
bachinger 2020-05-14 13:22:24 +01:00 committed by Oliver Woodman
parent 6abec07a07
commit c1aa9b917b

View File

@ -18,9 +18,11 @@ package com.google.android.exoplayer2.offline;
import android.net.Uri; import android.net.Uri;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.upstream.DataSpec; import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheUtil; import com.google.android.exoplayer2.upstream.cache.CacheUtil;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.PriorityTaskManager; import com.google.android.exoplayer2.util.PriorityTaskManager;
import java.io.IOException; import java.io.IOException;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
@ -37,22 +39,46 @@ public final class ProgressiveDownloader implements Downloader {
@Nullable private volatile Thread downloadThread; @Nullable private volatile Thread downloadThread;
/** /** @deprecated Use {@link #ProgressiveDownloader(MediaItem, CacheDataSource.Factory)} instead. */
* @param uri Uri of the data to be downloaded. @SuppressWarnings("deprecation")
* @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache @Deprecated
* indexing. May be null.
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
* download will be written.
*/
public ProgressiveDownloader( public ProgressiveDownloader(
Uri uri, @Nullable String customCacheKey, CacheDataSource.Factory cacheDataSourceFactory) { Uri uri, @Nullable String customCacheKey, CacheDataSource.Factory cacheDataSourceFactory) {
this(uri, customCacheKey, cacheDataSourceFactory, Runnable::run); this(uri, customCacheKey, cacheDataSourceFactory, Runnable::run);
} }
/** /**
* @param uri Uri of the data to be downloaded. * Creates a new instance.
* @param customCacheKey A custom key that uniquely identifies the original stream. Used for cache *
* indexing. May be null. * @param mediaItem The media item with a uri to the stream to be downloaded.
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
* download will be written.
*/
public ProgressiveDownloader(
MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory) {
this(mediaItem, cacheDataSourceFactory, Runnable::run);
}
/**
* @deprecated Use {@link #ProgressiveDownloader(MediaItem, CacheDataSource.Factory, Executor)}
* instead.
*/
@Deprecated
public ProgressiveDownloader(
Uri uri,
@Nullable String customCacheKey,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this(
new MediaItem.Builder().setUri(uri).setCustomCacheKey(customCacheKey).build(),
cacheDataSourceFactory,
executor);
}
/**
* Creates a new instance.
*
* @param mediaItem The media item with a uri to the stream to be downloaded.
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the * @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
* download will be written. * download will be written.
* @param executor An {@link Executor} used to make requests for the media being downloaded. In * @param executor An {@link Executor} used to make requests for the media being downloaded. In
@ -60,14 +86,12 @@ public final class ProgressiveDownloader implements Downloader {
* download by allowing parts of it to be executed in parallel. * download by allowing parts of it to be executed in parallel.
*/ */
public ProgressiveDownloader( public ProgressiveDownloader(
Uri uri, MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) {
@Nullable String customCacheKey, Assertions.checkNotNull(mediaItem.playbackProperties);
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
dataSpec = dataSpec =
new DataSpec.Builder() new DataSpec.Builder()
.setUri(uri) .setUri(mediaItem.playbackProperties.uri)
.setKey(customCacheKey) .setKey(mediaItem.playbackProperties.customCacheKey)
.setFlags(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION) .setFlags(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION)
.build(); .build();
dataSource = cacheDataSourceFactory.createDataSourceForDownloading(); dataSource = cacheDataSourceFactory.createDataSourceForDownloading();
@ -115,10 +139,10 @@ public final class ProgressiveDownloader implements Downloader {
private static final class ProgressForwarder implements CacheUtil.ProgressListener { private static final class ProgressForwarder implements CacheUtil.ProgressListener {
private final ProgressListener progessListener; private final ProgressListener progressListener;
public ProgressForwarder(ProgressListener progressListener) { public ProgressForwarder(ProgressListener progressListener) {
this.progessListener = progressListener; this.progressListener = progressListener;
} }
@Override @Override
@ -127,7 +151,7 @@ public final class ProgressiveDownloader implements Downloader {
contentLength == C.LENGTH_UNSET || contentLength == 0 contentLength == C.LENGTH_UNSET || contentLength == 0
? C.PERCENTAGE_UNSET ? C.PERCENTAGE_UNSET
: ((bytesCached * 100f) / contentLength); : ((bytesCached * 100f) / contentLength);
progessListener.onProgress(contentLength, bytesCached, percentDownloaded); progressListener.onProgress(contentLength, bytesCached, percentDownloaded);
} }
} }
} }