From 2e1024f0d144616ec5e665147ea63f0aa2c8ae83 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 27 Apr 2020 14:06:54 +0100 Subject: [PATCH] Add CacheDataSourceFactory getters A previous change added these getters to CacheDataSource, but it can also be useful to retrieve these components directly from the factory. This is useful for tasks where we're going to need to build multiple CacheDataSource instances (e.g., to make requests in parallel), and also need to operate directly on the same components. It's a bit more natural to retrieve them from the factory than from an arbitrary CacheDataSource in this case, since it can avoid unnatural code where you create a CacheDataSource instance earlier than you would otherwise just to use its getters, and/or create one just to use its getters and then throw it away. PiperOrigin-RevId: 308606020 --- .../exoplayer2/offline/SegmentDownloader.java | 18 ++++++++----- .../upstream/cache/CacheDataSource.java | 26 +++++++++++++++++-- 2 files changed, 35 insertions(+), 9 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 f7b2fc7ffd..3bf792988e 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 @@ -105,12 +105,17 @@ 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(); + @Nullable + PriorityTaskManager priorityTaskManager = + cacheDataSourceFactory.getUpstreamPriorityTaskManager(); if (priorityTaskManager != null) { priorityTaskManager.add(C.PRIORITY_DOWNLOAD); } try { + Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache()); + CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory(); + CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading(); + // Get the manifest and all of the segments. M manifest = getManifest(dataSource, manifestDataSpec); if (!streamKeys.isEmpty()) { @@ -118,7 +123,7 @@ public abstract class SegmentDownloader> impleme } List segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false); Collections.sort(segments); - mergeSegments(segments, dataSource.getCacheKeyFactory()); + mergeSegments(segments, cacheKeyFactory); // Scan the segments, removing any that are fully downloaded. int totalSegments = segments.size(); @@ -128,8 +133,7 @@ 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, dataSource.getCache(), dataSource.getCacheKeyFactory()); + CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory); long segmentLength = segmentLengthAndBytesDownloaded.first; long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second; bytesDownloaded += segmentBytesDownloaded; @@ -185,9 +189,9 @@ public abstract class SegmentDownloader> impleme @Override public final void remove() throws InterruptedException { + Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache()); + CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory(); CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload(); - Cache cache = dataSource.getCache(); - CacheKeyFactory cacheKeyFactory = dataSource.getCacheKeyFactory(); try { M manifest = getManifest(dataSource, manifestDataSpec); List segments = getSegments(dataSource, manifest, true); 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 c316d75501..3f9010a609 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 @@ -80,6 +80,15 @@ public final class CacheDataSource implements DataSource { return this; } + /** + * Returns the cache that will be used, or {@code null} if {@link #setCache} has yet to be + * called. + */ + @Nullable + public Cache getCache() { + return cache; + } + /** * Sets the {@link DataSource.Factory} for {@link DataSource DataSources} for reading from the * cache. @@ -124,6 +133,11 @@ public final class CacheDataSource implements DataSource { return this; } + /** Returns the {@link CacheKeyFactory} that will be used. */ + public CacheKeyFactory getCacheKeyFactory() { + return cacheKeyFactory; + } + /** * Sets the {@link DataSource.Factory} for upstream {@link DataSource DataSources}, which are * used to read data in the case of a cache miss. @@ -165,6 +179,15 @@ public final class CacheDataSource implements DataSource { return this; } + /** + * Returns the {@link PriorityTaskManager} that will bs used when requesting data from upstream, + * or {@code null} if there is none. + */ + @Nullable + public PriorityTaskManager getUpstreamPriorityTaskManager() { + return upstreamPriorityTaskManager; + } + /** * Sets the priority to use when requesting data from upstream. The priority is only used if a * {@link PriorityTaskManager} is set by calling {@link #setUpstreamPriorityTaskManager}. @@ -492,6 +515,7 @@ public final class CacheDataSource implements DataSource { this.ignoreCacheForUnsetLengthRequests = (flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0; this.upstreamPriority = upstreamPriority; + this.upstreamPriorityTaskManager = upstreamPriorityTaskManager; if (upstreamDataSource != null) { if (upstreamPriorityTaskManager != null) { upstreamDataSource = @@ -499,14 +523,12 @@ public final class CacheDataSource implements DataSource { upstreamDataSource, upstreamPriorityTaskManager, upstreamPriority); } this.upstreamDataSource = upstreamDataSource; - this.upstreamPriorityTaskManager = upstreamPriorityTaskManager; this.cacheWriteDataSource = cacheWriteDataSink != null ? new TeeDataSource(upstreamDataSource, cacheWriteDataSink) : null; } else { this.upstreamDataSource = DummyDataSource.INSTANCE; - this.upstreamPriorityTaskManager = null; this.cacheWriteDataSource = null; } this.eventListener = eventListener;