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;