Enable download parallelisation in demo app

- Deprecate constructors that don't take an executor, to direct
  developers toward the new ones. Callers can trivially pass
  Runnable::run to one of the new ones if they want old behaviour.
- Add comment explaining warning suppression added in the CL that
  added parallelised download support.

Issue: #5978
PiperOrigin-RevId: 318803296
This commit is contained in:
olly 2020-06-29 14:36:57 +01:00 committed by Oliver Woodman
parent 2eab6802c9
commit 314bc65d62
5 changed files with 46 additions and 6 deletions

View file

@ -145,7 +145,10 @@
([#7078](https://github.com/google/ExoPlayer/issues/7078)).
* Remove generics from DRM components.
* Downloads and caching:
* Merge downloads in `SegmentDownloader` to improve overall download speed
* Support passing an `Executor` to `DefaultDownloaderFactory` on which
data downloads are performed.
* Parallelize and merge downloads in `SegmentDownloader` to improve
overall download speed
([#5978](https://github.com/google/ExoPlayer/issues/5978)).
* Support multiple non-overlapping write locks for the same key in
`SimpleCache`.

View file

@ -36,6 +36,7 @@ import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.Util;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.Executors;
/**
* Placeholder application to facilitate overriding Application methods for debugging and testing.
@ -129,7 +130,11 @@ public class DemoApplication extends MultiDexApplication {
DOWNLOAD_TRACKER_ACTION_FILE, downloadIndex, /* addNewDownloadsAsCompleted= */ true);
downloadManager =
new DownloadManager(
this, getDatabaseProvider(), getDownloadCache(), buildHttpDataSourceFactory());
this,
getDatabaseProvider(),
getDownloadCache(),
buildHttpDataSourceFactory(),
Executors.newFixedThreadPool(/* nThreads= */ 6));
downloadTracker =
new DownloadTracker(/* context= */ this, buildDataSourceFactory(), downloadManager);
}

View file

@ -79,7 +79,9 @@ public class DefaultDownloaderFactory implements DownloaderFactory {
*
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which
* downloads will be written.
* @deprecated Use {@link #DefaultDownloaderFactory(CacheDataSource.Factory, Executor)}.
*/
@Deprecated
public DefaultDownloaderFactory(CacheDataSource.Factory cacheDataSourceFactory) {
this(cacheDataSourceFactory, Runnable::run);
}
@ -89,9 +91,10 @@ public class DefaultDownloaderFactory implements DownloaderFactory {
*
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which
* downloads will be written.
* @param executor An {@link Executor} used to make requests for media being downloaded. Providing
* an {@link Executor} that uses multiple threads will speed up download tasks that can be
* split into smaller parts for parallel execution.
* @param executor An {@link Executor} used to download data. Passing {@code Runnable::run} will
* cause each download task to download data on its own thread. Passing an {@link Executor}
* that uses multiple threads will speed up download tasks that can be split into smaller
* parts for parallel execution.
*/
public DefaultDownloaderFactory(
CacheDataSource.Factory cacheDataSourceFactory, Executor executor) {

View file

@ -52,6 +52,7 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.Executor;
/**
* Manages downloads.
@ -197,16 +198,42 @@ public final class DownloadManager {
* an {@link CacheEvictor} that will not evict downloaded content, for example {@link
* NoOpCacheEvictor}.
* @param upstreamFactory A {@link Factory} for creating {@link DataSource}s for downloading data.
* @deprecated Use {@link #DownloadManager(Context, DatabaseProvider, Cache, Factory, Executor)}.
*/
@Deprecated
public DownloadManager(
Context context, DatabaseProvider databaseProvider, Cache cache, Factory upstreamFactory) {
this(context, databaseProvider, cache, upstreamFactory, Runnable::run);
}
/**
* Constructs a {@link DownloadManager}.
*
* @param context Any context.
* @param databaseProvider Provides the SQLite database in which downloads are persisted.
* @param cache A cache to be used to store downloaded data. The cache should be configured with
* an {@link CacheEvictor} that will not evict downloaded content, for example {@link
* NoOpCacheEvictor}.
* @param upstreamFactory A {@link Factory} for creating {@link DataSource}s for downloading data.
* @param executor An {@link Executor} used to download data. Passing {@code Runnable::run} will
* cause each download task to download data on its own thread. Passing an {@link Executor}
* that uses multiple threads will speed up download tasks that can be split into smaller
* parts for parallel execution.
*/
public DownloadManager(
Context context,
DatabaseProvider databaseProvider,
Cache cache,
Factory upstreamFactory,
Executor executor) {
this(
context,
new DefaultDownloadIndex(databaseProvider),
new DefaultDownloaderFactory(
new CacheDataSource.Factory()
.setCache(cache)
.setUpstreamDataSourceFactory(upstreamFactory)));
.setUpstreamDataSourceFactory(upstreamFactory),
executor));
}
/**

View file

@ -159,6 +159,8 @@ public abstract class RunnableFutureTask<R, E extends Exception> implements Runn
// Do nothing.
}
// The return value is guaranteed to be non-null if and only if R is a non-null type, but there's
// no way to assert this. Suppress the warning instead.
@SuppressWarnings("return.type.incompatible")
@UnknownNull
private R getResult() throws ExecutionException {