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
This commit is contained in:
parent
86fb3dfede
commit
2e1024f0d1
@ -105,12 +105,17 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
@Override
|
@Override
|
||||||
public final void download(@Nullable ProgressListener progressListener)
|
public final void download(@Nullable ProgressListener progressListener)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException {
|
||||||
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading();
|
@Nullable
|
||||||
@Nullable PriorityTaskManager priorityTaskManager = dataSource.getUpstreamPriorityTaskManager();
|
PriorityTaskManager priorityTaskManager =
|
||||||
|
cacheDataSourceFactory.getUpstreamPriorityTaskManager();
|
||||||
if (priorityTaskManager != null) {
|
if (priorityTaskManager != null) {
|
||||||
priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
|
priorityTaskManager.add(C.PRIORITY_DOWNLOAD);
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
|
Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache());
|
||||||
|
CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
|
||||||
|
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForDownloading();
|
||||||
|
|
||||||
// Get the manifest and all of the segments.
|
// Get the manifest and all of the segments.
|
||||||
M manifest = getManifest(dataSource, manifestDataSpec);
|
M manifest = getManifest(dataSource, manifestDataSpec);
|
||||||
if (!streamKeys.isEmpty()) {
|
if (!streamKeys.isEmpty()) {
|
||||||
@ -118,7 +123,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
}
|
}
|
||||||
List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);
|
List<Segment> segments = getSegments(dataSource, manifest, /* allowIncompleteList= */ false);
|
||||||
Collections.sort(segments);
|
Collections.sort(segments);
|
||||||
mergeSegments(segments, dataSource.getCacheKeyFactory());
|
mergeSegments(segments, cacheKeyFactory);
|
||||||
|
|
||||||
// Scan the segments, removing any that are fully downloaded.
|
// Scan the segments, removing any that are fully downloaded.
|
||||||
int totalSegments = segments.size();
|
int totalSegments = segments.size();
|
||||||
@ -128,8 +133,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
for (int i = segments.size() - 1; i >= 0; i--) {
|
for (int i = segments.size() - 1; i >= 0; i--) {
|
||||||
Segment segment = segments.get(i);
|
Segment segment = segments.get(i);
|
||||||
Pair<Long, Long> segmentLengthAndBytesDownloaded =
|
Pair<Long, Long> segmentLengthAndBytesDownloaded =
|
||||||
CacheUtil.getCached(
|
CacheUtil.getCached(segment.dataSpec, cache, cacheKeyFactory);
|
||||||
segment.dataSpec, dataSource.getCache(), dataSource.getCacheKeyFactory());
|
|
||||||
long segmentLength = segmentLengthAndBytesDownloaded.first;
|
long segmentLength = segmentLengthAndBytesDownloaded.first;
|
||||||
long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
|
long segmentBytesDownloaded = segmentLengthAndBytesDownloaded.second;
|
||||||
bytesDownloaded += segmentBytesDownloaded;
|
bytesDownloaded += segmentBytesDownloaded;
|
||||||
@ -185,9 +189,9 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public final void remove() throws InterruptedException {
|
public final void remove() throws InterruptedException {
|
||||||
|
Cache cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache());
|
||||||
|
CacheKeyFactory cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
|
||||||
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload();
|
CacheDataSource dataSource = cacheDataSourceFactory.createDataSourceForRemovingDownload();
|
||||||
Cache cache = dataSource.getCache();
|
|
||||||
CacheKeyFactory cacheKeyFactory = dataSource.getCacheKeyFactory();
|
|
||||||
try {
|
try {
|
||||||
M manifest = getManifest(dataSource, manifestDataSpec);
|
M manifest = getManifest(dataSource, manifestDataSpec);
|
||||||
List<Segment> segments = getSegments(dataSource, manifest, true);
|
List<Segment> segments = getSegments(dataSource, manifest, true);
|
||||||
|
@ -80,6 +80,15 @@ public final class CacheDataSource implements DataSource {
|
|||||||
return this;
|
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
|
* Sets the {@link DataSource.Factory} for {@link DataSource DataSources} for reading from the
|
||||||
* cache.
|
* cache.
|
||||||
@ -124,6 +133,11 @@ public final class CacheDataSource implements DataSource {
|
|||||||
return this;
|
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
|
* Sets the {@link DataSource.Factory} for upstream {@link DataSource DataSources}, which are
|
||||||
* used to read data in the case of a cache miss.
|
* used to read data in the case of a cache miss.
|
||||||
@ -165,6 +179,15 @@ public final class CacheDataSource implements DataSource {
|
|||||||
return this;
|
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
|
* 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}.
|
* {@link PriorityTaskManager} is set by calling {@link #setUpstreamPriorityTaskManager}.
|
||||||
@ -492,6 +515,7 @@ public final class CacheDataSource implements DataSource {
|
|||||||
this.ignoreCacheForUnsetLengthRequests =
|
this.ignoreCacheForUnsetLengthRequests =
|
||||||
(flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0;
|
(flags & FLAG_IGNORE_CACHE_FOR_UNSET_LENGTH_REQUESTS) != 0;
|
||||||
this.upstreamPriority = upstreamPriority;
|
this.upstreamPriority = upstreamPriority;
|
||||||
|
this.upstreamPriorityTaskManager = upstreamPriorityTaskManager;
|
||||||
if (upstreamDataSource != null) {
|
if (upstreamDataSource != null) {
|
||||||
if (upstreamPriorityTaskManager != null) {
|
if (upstreamPriorityTaskManager != null) {
|
||||||
upstreamDataSource =
|
upstreamDataSource =
|
||||||
@ -499,14 +523,12 @@ public final class CacheDataSource implements DataSource {
|
|||||||
upstreamDataSource, upstreamPriorityTaskManager, upstreamPriority);
|
upstreamDataSource, upstreamPriorityTaskManager, upstreamPriority);
|
||||||
}
|
}
|
||||||
this.upstreamDataSource = upstreamDataSource;
|
this.upstreamDataSource = upstreamDataSource;
|
||||||
this.upstreamPriorityTaskManager = upstreamPriorityTaskManager;
|
|
||||||
this.cacheWriteDataSource =
|
this.cacheWriteDataSource =
|
||||||
cacheWriteDataSink != null
|
cacheWriteDataSink != null
|
||||||
? new TeeDataSource(upstreamDataSource, cacheWriteDataSink)
|
? new TeeDataSource(upstreamDataSource, cacheWriteDataSink)
|
||||||
: null;
|
: null;
|
||||||
} else {
|
} else {
|
||||||
this.upstreamDataSource = DummyDataSource.INSTANCE;
|
this.upstreamDataSource = DummyDataSource.INSTANCE;
|
||||||
this.upstreamPriorityTaskManager = null;
|
|
||||||
this.cacheWriteDataSource = null;
|
this.cacheWriteDataSource = null;
|
||||||
}
|
}
|
||||||
this.eventListener = eventListener;
|
this.eventListener = eventListener;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user