From 7ea83d7167d0b021fb280fca2b5dfa4a5616b5c1 Mon Sep 17 00:00:00 2001 From: olly Date: Fri, 17 Apr 2020 17:22:39 +0100 Subject: [PATCH] Better tie injection of Cache/CacheDataSource/CacheKeyFactory PiperOrigin-RevId: 307056661 --- .../offline/DownloaderConstructorHelper.java | 15 --------------- .../exoplayer2/offline/ProgressiveDownloader.java | 10 +--------- .../exoplayer2/offline/SegmentDownloader.java | 14 ++++---------- .../upstream/cache/CacheDataSource.java | 10 ++++++++++ .../exoplayer2/upstream/cache/CacheUtil.java | 10 +++------- .../exoplayer2/upstream/cache/CacheUtilTest.java | 4 ---- 6 files changed, 18 insertions(+), 45 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java index 0d53b3cde0..7ba4315a80 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/DownloaderConstructorHelper.java @@ -28,14 +28,11 @@ import com.google.android.exoplayer2.upstream.cache.CacheDataSinkFactory; import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory; -import com.google.android.exoplayer2.upstream.cache.CacheUtil; import com.google.android.exoplayer2.util.PriorityTaskManager; /** A helper class that holds necessary parameters for {@link Downloader} construction. */ public final class DownloaderConstructorHelper { - private final Cache cache; - @Nullable private final CacheKeyFactory cacheKeyFactory; @Nullable private final PriorityTaskManager priorityTaskManager; private final CacheDataSourceFactory onlineCacheDataSourceFactory; private final CacheDataSourceFactory offlineCacheDataSourceFactory; @@ -133,19 +130,7 @@ public final class DownloaderConstructorHelper { CacheDataSource.FLAG_BLOCK_ON_CACHE, /* eventListener= */ null, cacheKeyFactory); - this.cache = cache; this.priorityTaskManager = priorityTaskManager; - this.cacheKeyFactory = cacheKeyFactory; - } - - /** Returns the {@link Cache} instance. */ - public Cache getCache() { - return cache; - } - - /** Returns the {@link CacheKeyFactory}. */ - public CacheKeyFactory getCacheKeyFactory() { - return cacheKeyFactory != null ? cacheKeyFactory : CacheUtil.DEFAULT_CACHE_KEY_FACTORY; } /** Returns a {@link PriorityTaskManager} instance. */ diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java index 055410c431..912e6c2657 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/ProgressiveDownloader.java @@ -19,9 +19,7 @@ import android.net.Uri; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; 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; import com.google.android.exoplayer2.util.PriorityTaskManager; import java.io.IOException; @@ -41,9 +39,7 @@ public final class ProgressiveDownloader implements Downloader { private static final int BUFFER_SIZE_BYTES = 128 * 1024; private final DataSpec dataSpec; - private final Cache cache; private final CacheDataSource dataSource; - private final CacheKeyFactory cacheKeyFactory; private final PriorityTaskManager priorityTaskManager; private final AtomicBoolean isCanceled; @@ -61,9 +57,7 @@ public final class ProgressiveDownloader implements Downloader { .setKey(customCacheKey) .setFlags(DataSpec.FLAG_ALLOW_CACHE_FRAGMENTATION) .build(); - this.cache = constructorHelper.getCache(); this.dataSource = constructorHelper.createCacheDataSource(); - this.cacheKeyFactory = constructorHelper.getCacheKeyFactory(); this.priorityTaskManager = constructorHelper.getPriorityTaskManager(); isCanceled = new AtomicBoolean(); } @@ -75,8 +69,6 @@ public final class ProgressiveDownloader implements Downloader { try { CacheUtil.cache( dataSpec, - cache, - cacheKeyFactory, dataSource, new byte[BUFFER_SIZE_BYTES], priorityTaskManager, @@ -96,7 +88,7 @@ public final class ProgressiveDownloader implements Downloader { @Override public void remove() { - CacheUtil.remove(dataSpec, cache, cacheKeyFactory); + CacheUtil.remove(dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory()); } private static final class ProgressForwarder implements CacheUtil.ProgressListener { 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 299998ea88..cbf87ce49c 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,7 +21,6 @@ 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,10 +66,8 @@ 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 Cache cache; private final CacheDataSource dataSource; private final CacheDataSource offlineDataSource; - private final CacheKeyFactory cacheKeyFactory; private final PriorityTaskManager priorityTaskManager; private final ArrayList streamKeys; private final AtomicBoolean isCanceled; @@ -85,10 +82,8 @@ public abstract class SegmentDownloader> impleme Uri manifestUri, List streamKeys, DownloaderConstructorHelper constructorHelper) { this.manifestDataSpec = getCompressibleDataSpec(manifestUri); this.streamKeys = new ArrayList<>(streamKeys); - this.cache = constructorHelper.getCache(); this.dataSource = constructorHelper.createCacheDataSource(); this.offlineDataSource = constructorHelper.createOfflineCacheDataSource(); - this.cacheKeyFactory = constructorHelper.getCacheKeyFactory(); this.priorityTaskManager = constructorHelper.getPriorityTaskManager(); isCanceled = new AtomicBoolean(); } @@ -112,7 +107,7 @@ public abstract class SegmentDownloader> impleme } List segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false); Collections.sort(segments); - mergeSegments(segments, cacheKeyFactory); + mergeSegments(segments, dataSource.getCacheKeyFactory()); // Scan the segments, removing any that are fully downloaded. int totalSegments = segments.size(); @@ -122,7 +117,8 @@ public abstract class SegmentDownloader> impleme for (int i = segments.size() - 1; i >= 0; i--) { Segment segment = segments.get(i); Pair segmentLengthAndBytesDownloaded = - CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory); + CacheUtil.getCached( + segment.dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory()); long segmentLength = segmentLengthAndBytesDownloaded.first; long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second; bytesDownloaded += segmentBytesDownloaded; @@ -155,8 +151,6 @@ public abstract class SegmentDownloader> impleme for (int i = 0; i < segments.size(); i++) { CacheUtil.cache( segments.get(i).dataSpec, - cache, - cacheKeyFactory, dataSource, buffer, priorityTaskManager, @@ -224,7 +218,7 @@ public abstract class SegmentDownloader> impleme throws InterruptedException, IOException; private void removeDataSpec(DataSpec dataSpec) { - CacheUtil.remove(dataSpec, cache, cacheKeyFactory); + CacheUtil.remove(dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory()); } protected static DataSpec getCompressibleDataSpec(Uri uri) { diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java index 5142f2428d..398b1b4a3a 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheDataSource.java @@ -244,6 +244,16 @@ public final class CacheDataSource implements DataSource { this.eventListener = eventListener; } + /** Returns the {@link Cache} used by this instance. */ + public Cache getCache() { + return cache; + } + + /** Returns the {@link CacheKeyFactory} used by this instance. */ + public CacheKeyFactory getCacheKeyFactory() { + return cacheKeyFactory; + } + @Override public void addTransferListener(TransferListener transferListener) { cacheReadDataSource.addTransferListener(transferListener); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java index 55347e6143..3fac80d1ae 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/cache/CacheUtil.java @@ -125,8 +125,6 @@ public final class CacheUtil { throws IOException, InterruptedException { cache( dataSpec, - cache, - /* cacheKeyFactory= */ null, new CacheDataSource(cache, upstream), new byte[DEFAULT_BUFFER_SIZE_BYTES], /* priorityTaskManager= */ null, @@ -149,9 +147,7 @@ public final class CacheUtil { *

This method may be slow and shouldn't normally be called on the main thread. * * @param dataSpec Defines the data to be cached. - * @param cache A {@link Cache} to store the data. - * @param cacheKeyFactory An optional factory for cache keys. - * @param dataSource A {@link CacheDataSource} that works on the {@code cache}. + * @param dataSource A {@link CacheDataSource} to be used for caching the data. * @param buffer The buffer to be used while caching. * @param priorityTaskManager If not null it's used to check whether it is allowed to proceed with * caching. @@ -166,8 +162,6 @@ public final class CacheUtil { @WorkerThread public static void cache( DataSpec dataSpec, - Cache cache, - @Nullable CacheKeyFactory cacheKeyFactory, CacheDataSource dataSource, byte[] buffer, @Nullable PriorityTaskManager priorityTaskManager, @@ -179,6 +173,8 @@ public final class CacheUtil { Assertions.checkNotNull(dataSource); Assertions.checkNotNull(buffer); + Cache cache = dataSource.getCache(); + CacheKeyFactory cacheKeyFactory = dataSource.getCacheKeyFactory(); String key = buildCacheKey(dataSpec, cacheKeyFactory); long bytesLeft; @Nullable ProgressNotifier progressNotifier = null; diff --git a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java index 79d18f25a6..9cb27bd6d6 100644 --- a/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java +++ b/library/core/src/test/java/com/google/android/exoplayer2/upstream/cache/CacheUtilTest.java @@ -301,8 +301,6 @@ public final class CacheUtilTest { try { CacheUtil.cache( dataSpec, - cache, - /* cacheKeyFactory= */ null, new CacheDataSource(cache, dataSource), new byte[CacheUtil.DEFAULT_BUFFER_SIZE_BYTES], /* priorityTaskManager= */ null, @@ -353,8 +351,6 @@ public final class CacheUtilTest { .build(); CacheUtil.cache( dataSpec, - cache, - /* cacheKeyFactory= */ null, // Set fragmentSize to 10 to make sure there are multiple spans. new CacheDataSource( cache,