From dcdc9aa1116fdaa8bcc351bfbdc5bf7ebda3b9a3 Mon Sep 17 00:00:00 2001 From: lemondoglol Date: Mon, 30 Jan 2023 10:57:45 -0500 Subject: [PATCH] make merged segment size configurable --- .../exoplayer/offline/SegmentDownloader.java | 25 +++++++++++++- .../dash/offline/DashDownloader.java | 23 +++++++++++-- .../exoplayer/hls/offline/HlsDownloader.java | 34 +++++++++++++++++-- .../smoothstreaming/offline/SsDownloader.java | 30 ++++++++++++++-- 4 files changed, 105 insertions(+), 7 deletions(-) diff --git a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java index d4b8ece7262..f42d3896f1a 100644 --- a/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java +++ b/libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java @@ -76,7 +76,8 @@ public int compareTo(Segment other) { } private static final int BUFFER_SIZE_BYTES = 128 * 1024; - private static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND; + public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND; + private static long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US; private final DataSpec manifestDataSpec; private final Parser manifestParser; @@ -107,7 +108,29 @@ public int compareTo(Segment other) { * @param executor An {@link Executor} used to make requests for the media being downloaded. * Providing an {@link Executor} that uses multiple threads will speed up the download by * allowing parts of it to be executed in parallel. + * @param maxMergedSegmentStartTimeDiffUs max merged size for each segment duration */ + public SegmentDownloader( + MediaItem mediaItem, + Parser manifestParser, + CacheDataSource.Factory cacheDataSourceFactory, + Executor executor, + long maxMergedSegmentStartTimeDiffUs + ) { + checkNotNull(mediaItem.localConfiguration); + this.manifestDataSpec = getCompressibleDataSpec(mediaItem.localConfiguration.uri); + this.manifestParser = manifestParser; + this.streamKeys = new ArrayList<>(mediaItem.localConfiguration.streamKeys); + this.cacheDataSourceFactory = cacheDataSourceFactory; + this.executor = executor; + cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache()); + cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory(); + priorityTaskManager = cacheDataSourceFactory.getUpstreamPriorityTaskManager(); + activeRunnables = new ArrayList<>(); + MAX_MERGED_SEGMENT_START_TIME_DIFF_US = maxMergedSegmentStartTimeDiffUs * C.MICROS_PER_SECOND; + } + + @Deprecated public SegmentDownloader( MediaItem mediaItem, Parser manifestParser, diff --git a/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/offline/DashDownloader.java b/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/offline/DashDownloader.java index e3c41727288..9af4c32e751 100644 --- a/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/offline/DashDownloader.java +++ b/libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/offline/DashDownloader.java @@ -100,7 +100,7 @@ public DashDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSour */ public DashDownloader( MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) { - this(mediaItem, new DashManifestParser(), cacheDataSourceFactory, executor); + this(mediaItem, new DashManifestParser(), cacheDataSourceFactory, executor, DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US); } /** @@ -113,13 +113,32 @@ public DashDownloader( * @param executor An {@link Executor} used to make requests for the media being downloaded. * Providing an {@link Executor} that uses multiple threads will speed up the download by * allowing parts of it to be executed in parallel. + * @param maxMergedSegmentStartTimeDiffUs max merged size for each segment duration */ + public DashDownloader( + MediaItem mediaItem, + Parser manifestParser, + CacheDataSource.Factory cacheDataSourceFactory, + Executor executor, + long maxMergedSegmentStartTimeDiffUs + ) { + super(mediaItem, manifestParser, cacheDataSourceFactory, executor, maxMergedSegmentStartTimeDiffUs); + baseUrlExclusionList = new BaseUrlExclusionList(); + } + + @Deprecated public DashDownloader( MediaItem mediaItem, Parser manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) { - super(mediaItem, manifestParser, cacheDataSourceFactory, executor); + super( + mediaItem, + manifestParser, + cacheDataSourceFactory, + executor, + DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US + ); baseUrlExclusionList = new BaseUrlExclusionList(); } diff --git a/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/offline/HlsDownloader.java b/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/offline/HlsDownloader.java index 777694e9f63..8165bd06e7a 100644 --- a/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/offline/HlsDownloader.java +++ b/libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/offline/HlsDownloader.java @@ -89,7 +89,13 @@ public HlsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourc */ public HlsDownloader( MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) { - this(mediaItem, new HlsPlaylistParser(), cacheDataSourceFactory, executor); + this( + mediaItem, + new HlsPlaylistParser(), + cacheDataSourceFactory, + executor, + DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US + ); } /** @@ -102,13 +108,37 @@ public HlsDownloader( * @param executor An {@link Executor} used to make requests for the media being downloaded. * Providing an {@link Executor} that uses multiple threads will speed up the download by * allowing parts of it to be executed in parallel. + * @param maxMergedSegmentStartTimeDiffUs max merged size for each segment duration */ + public HlsDownloader( + MediaItem mediaItem, + Parser manifestParser, + CacheDataSource.Factory cacheDataSourceFactory, + Executor executor, + long maxMergedSegmentStartTimeDiffUs + ) { + super( + mediaItem, + manifestParser, + cacheDataSourceFactory, + executor, + maxMergedSegmentStartTimeDiffUs + ); + } + + @Deprecated public HlsDownloader( MediaItem mediaItem, Parser manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) { - super(mediaItem, manifestParser, cacheDataSourceFactory, executor); + super( + mediaItem, + manifestParser, + cacheDataSourceFactory, + executor, + DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US + ); } @Override diff --git a/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader.java b/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader.java index 4ca632271bb..d563d84ac0d 100644 --- a/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader.java +++ b/libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader.java @@ -93,7 +93,9 @@ public SsDownloader( .build(), new SsManifestParser(), cacheDataSourceFactory, - executor); + executor, + DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US + ); } /** @@ -106,13 +108,37 @@ public SsDownloader( * @param executor An {@link Executor} used to make requests for the media being downloaded. * Providing an {@link Executor} that uses multiple threads will speed up the download by * allowing parts of it to be executed in parallel. + * @param maxMergedSegmentStartTimeDiffUs max merged size for each segment duration */ + public SsDownloader( + MediaItem mediaItem, + Parser manifestParser, + CacheDataSource.Factory cacheDataSourceFactory, + Executor executor, + long maxMergedSegmentStartTimeDiffUs + ) { + super( + mediaItem, + manifestParser, + cacheDataSourceFactory, + executor, + maxMergedSegmentStartTimeDiffUs + ); + } + + @Deprecated public SsDownloader( MediaItem mediaItem, Parser manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) { - super(mediaItem, manifestParser, cacheDataSourceFactory, executor); + super( + mediaItem, + manifestParser, + cacheDataSourceFactory, + executor, + DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US + ); } @Override