From 84faa1ae3f1d9ca1c8f89e0d448c3b4f4b9c7d6f Mon Sep 17 00:00:00 2001 From: olly Date: Thu, 23 Apr 2020 14:58:54 +0100 Subject: [PATCH] SegmentDownloader cleanup This is just some trivial cleanup to get things into a better state to implement parallel segment downloads. Issue: #5978 PiperOrigin-RevId: 308041996 --- .../exoplayer2/offline/SegmentDownloader.java | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java index eba62686a6..cd4e67863b 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java @@ -21,6 +21,7 @@ import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSpec; +import com.google.android.exoplayer2.upstream.cache.Cache; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory; import com.google.android.exoplayer2.upstream.cache.CacheUtil; @@ -67,9 +68,9 @@ public abstract class SegmentDownloader> impleme private static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND; private final DataSpec manifestDataSpec; - private final CacheDataSource dataSource; - private final CacheDataSource removingDataSource; private final ArrayList streamKeys; + private final CacheDataSource.Factory cacheDataSourceFactory; + @Nullable private final ExecutorService executorService; private final AtomicBoolean isCanceled; /** @@ -91,8 +92,8 @@ public abstract class SegmentDownloader> impleme @Nullable ExecutorService executorService) { this.manifestDataSpec = getCompressibleDataSpec(manifestUri); this.streamKeys = new ArrayList<>(streamKeys); - this.dataSource = cacheDataSourceFactory.createDataSourceForDownloading(); - this.removingDataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload(); + this.cacheDataSourceFactory = cacheDataSourceFactory; + this.executorService = executorService; isCanceled = new AtomicBoolean(); } @@ -106,6 +107,7 @@ public abstract class SegmentDownloader> impleme @Override public final void download(@Nullable ProgressListener progressListener) throws IOException, InterruptedException { + CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading(); @Nullable PriorityTaskManager priorityTaskManager = dataSource.getUpstreamPriorityTaskManager(); if (priorityTaskManager != null) { priorityTaskManager.add(C.PRIORITY_DOWNLOAD); @@ -148,16 +150,16 @@ public abstract class SegmentDownloader> impleme } // Download the segments. - @Nullable ProgressNotifier progressNotifier = null; - if (progressListener != null) { - progressNotifier = - new ProgressNotifier( - progressListener, - contentLength, - totalSegments, - bytesDownloaded, - segmentsDownloaded); - } + @Nullable + ProgressNotifier progressNotifier = + progressListener != null + ? new ProgressNotifier( + progressListener, + contentLength, + totalSegments, + bytesDownloaded, + segmentsDownloaded) + : null; byte[] temporaryBuffer = new byte[BUFFER_SIZE_BYTES]; for (int i = 0; i < segments.size(); i++) { CacheUtil.cache( @@ -185,17 +187,20 @@ public abstract class SegmentDownloader> impleme @Override public final void remove() throws InterruptedException { + CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload(); + Cache cache = dataSource.getCache(); + CacheKeyFactory cacheKeyFactory = dataSource.getCacheKeyFactory(); try { - M manifest = getManifest(removingDataSource, manifestDataSpec); - List segments = getSegments(removingDataSource, manifest, true); + M manifest = getManifest(dataSource, manifestDataSpec); + List segments = getSegments(dataSource, manifest, true); for (int i = 0; i < segments.size(); i++) { - removeDataSpec(segments.get(i).dataSpec); + CacheUtil.remove(segments.get(i).dataSpec, cache, cacheKeyFactory); } } catch (IOException e) { // Ignore exceptions when removing. } finally { // Always attempt to remove the manifest. - removeDataSpec(manifestDataSpec); + CacheUtil.remove(manifestDataSpec, cache, cacheKeyFactory); } } @@ -228,10 +233,6 @@ public abstract class SegmentDownloader> impleme DataSource dataSource, M manifest, boolean allowIncompleteList) throws InterruptedException, IOException; - private void removeDataSpec(DataSpec dataSpec) { - CacheUtil.remove(dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory()); - } - protected static DataSpec getCompressibleDataSpec(Uri uri) { return new DataSpec.Builder().setUri(uri).setFlags(DataSpec.FLAG_ALLOW_GZIP).build(); }