Skip to content

Commit ecd91d8

Browse files
committed
Merge pull request #248 from lemondoglol:update-segment-size
PiperOrigin-RevId: 507784608
2 parents 0c0c972 + b65baf7 commit ecd91d8

5 files changed

Lines changed: 135 additions & 14 deletions

File tree

RELEASENOTES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
for seeking.
2121
* Use theme when loading drawables on API 21+
2222
([#220](https://github.com/androidx/media/issues/220)).
23+
* Make the maximum difference of the start time of two segments to be
24+
merged configurable in `SegmentDownloader` and subclasses
25+
([#248](https://github.com/androidx/media/pull/248)).
2326
* Add `ConcatenatingMediaSource2` that allows combining multiple media
2427
items into a single window
2528
([#247](https://github.com/androidx/media/issues/247)).

libraries/exoplayer/src/main/java/androidx/media3/exoplayer/offline/SegmentDownloader.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ public int compareTo(Segment other) {
7575
}
7676
}
7777

78+
public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20 * C.MILLIS_PER_SECOND;
79+
7880
private static final int BUFFER_SIZE_BYTES = 128 * 1024;
79-
private static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND;
8081

8182
private final DataSpec manifestDataSpec;
8283
private final Parser<M> manifestParser;
@@ -86,6 +87,7 @@ public int compareTo(Segment other) {
8687
private final CacheKeyFactory cacheKeyFactory;
8788
@Nullable private final PriorityTaskManager priorityTaskManager;
8889
private final Executor executor;
90+
private final long maxMergedSegmentStartTimeDiffUs;
8991

9092
/**
9193
* The currently active runnables.
@@ -99,6 +101,24 @@ public int compareTo(Segment other) {
99101

100102
private volatile boolean isCanceled;
101103

104+
/**
105+
* @deprecated Use {@link SegmentDownloader#SegmentDownloader(MediaItem, Parser,
106+
* CacheDataSource.Factory, Executor, long)} instead.
107+
*/
108+
@Deprecated
109+
public SegmentDownloader(
110+
MediaItem mediaItem,
111+
Parser<M> manifestParser,
112+
CacheDataSource.Factory cacheDataSourceFactory,
113+
Executor executor) {
114+
this(
115+
mediaItem,
116+
manifestParser,
117+
cacheDataSourceFactory,
118+
executor,
119+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
120+
}
121+
102122
/**
103123
* @param mediaItem The {@link MediaItem} to be downloaded.
104124
* @param manifestParser A parser for manifests belonging to the media to be downloaded.
@@ -107,12 +127,16 @@ public int compareTo(Segment other) {
107127
* @param executor An {@link Executor} used to make requests for the media being downloaded.
108128
* Providing an {@link Executor} that uses multiple threads will speed up the download by
109129
* allowing parts of it to be executed in parallel.
130+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
131+
* segments, up to which the segments (of the same URI) should be merged into a single
132+
* download segment, in milliseconds.
110133
*/
111134
public SegmentDownloader(
112135
MediaItem mediaItem,
113136
Parser<M> manifestParser,
114137
CacheDataSource.Factory cacheDataSourceFactory,
115-
Executor executor) {
138+
Executor executor,
139+
long maxMergedSegmentStartTimeDiffMs) {
116140
checkNotNull(mediaItem.localConfiguration);
117141
this.manifestDataSpec = getCompressibleDataSpec(mediaItem.localConfiguration.uri);
118142
this.manifestParser = manifestParser;
@@ -123,6 +147,7 @@ public SegmentDownloader(
123147
cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
124148
priorityTaskManager = cacheDataSourceFactory.getUpstreamPriorityTaskManager();
125149
activeRunnables = new ArrayList<>();
150+
maxMergedSegmentStartTimeDiffUs = Util.msToUs(maxMergedSegmentStartTimeDiffMs);
126151
}
127152

128153
@Override
@@ -145,7 +170,7 @@ public final void download(@Nullable ProgressListener progressListener)
145170
// Sort the segments so that we download media in the right order from the start of the
146171
// content, and merge segments where possible to minimize the number of server round trips.
147172
Collections.sort(segments);
148-
mergeSegments(segments, cacheKeyFactory);
173+
mergeSegments(segments, cacheKeyFactory, maxMergedSegmentStartTimeDiffUs);
149174

150175
// Scan the segments, removing any that are fully downloaded.
151176
int totalSegments = segments.size();
@@ -416,7 +441,8 @@ private void removeActiveRunnable(int index) {
416441
}
417442
}
418443

419-
private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFactory) {
444+
private static void mergeSegments(
445+
List<Segment> segments, CacheKeyFactory keyFactory, long maxMergedSegmentStartTimeDiffUs) {
420446
HashMap<String, Integer> lastIndexByCacheKey = new HashMap<>();
421447
int nextOutIndex = 0;
422448
for (int i = 0; i < segments.size(); i++) {
@@ -425,7 +451,7 @@ private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFac
425451
@Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
426452
@Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
427453
if (lastSegment == null
428-
|| segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US
454+
|| segment.startTimeUs > lastSegment.startTimeUs + maxMergedSegmentStartTimeDiffUs
429455
|| !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
430456
lastIndexByCacheKey.put(cacheKey, nextOutIndex);
431457
segments.set(nextOutIndex, segment);

libraries/exoplayer_dash/src/main/java/androidx/media3/exoplayer/dash/offline/DashDownloader.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,30 @@ public DashDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSour
100100
*/
101101
public DashDownloader(
102102
MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) {
103-
this(mediaItem, new DashManifestParser(), cacheDataSourceFactory, executor);
103+
this(
104+
mediaItem,
105+
new DashManifestParser(),
106+
cacheDataSourceFactory,
107+
executor,
108+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
109+
}
110+
111+
/**
112+
* @deprecated Use {@link DashDownloader#DashDownloader(MediaItem, Parser,
113+
* CacheDataSource.Factory, Executor, long)} instead.
114+
*/
115+
@Deprecated
116+
public DashDownloader(
117+
MediaItem mediaItem,
118+
Parser<DashManifest> manifestParser,
119+
CacheDataSource.Factory cacheDataSourceFactory,
120+
Executor executor) {
121+
this(
122+
mediaItem,
123+
manifestParser,
124+
cacheDataSourceFactory,
125+
executor,
126+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
104127
}
105128

106129
/**
@@ -113,13 +136,22 @@ public DashDownloader(
113136
* @param executor An {@link Executor} used to make requests for the media being downloaded.
114137
* Providing an {@link Executor} that uses multiple threads will speed up the download by
115138
* allowing parts of it to be executed in parallel.
139+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
140+
* segments, up to which the segments (of the same URI) should be merged into a single
141+
* download segment, in milliseconds.
116142
*/
117143
public DashDownloader(
118144
MediaItem mediaItem,
119145
Parser<DashManifest> manifestParser,
120146
CacheDataSource.Factory cacheDataSourceFactory,
121-
Executor executor) {
122-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
147+
Executor executor,
148+
long maxMergedSegmentStartTimeDiffMs) {
149+
super(
150+
mediaItem,
151+
manifestParser,
152+
cacheDataSourceFactory,
153+
executor,
154+
maxMergedSegmentStartTimeDiffMs);
123155
baseUrlExclusionList = new BaseUrlExclusionList();
124156
}
125157

libraries/exoplayer_hls/src/main/java/androidx/media3/exoplayer/hls/offline/HlsDownloader.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,30 @@ public HlsDownloader(MediaItem mediaItem, CacheDataSource.Factory cacheDataSourc
8989
*/
9090
public HlsDownloader(
9191
MediaItem mediaItem, CacheDataSource.Factory cacheDataSourceFactory, Executor executor) {
92-
this(mediaItem, new HlsPlaylistParser(), cacheDataSourceFactory, executor);
92+
this(
93+
mediaItem,
94+
new HlsPlaylistParser(),
95+
cacheDataSourceFactory,
96+
executor,
97+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
98+
}
99+
100+
/**
101+
* @deprecated Use {@link HlsDownloader#HlsDownloader(MediaItem, Parser, CacheDataSource.Factory,
102+
* Executor, long)} instead.
103+
*/
104+
@Deprecated
105+
public HlsDownloader(
106+
MediaItem mediaItem,
107+
Parser<HlsPlaylist> manifestParser,
108+
CacheDataSource.Factory cacheDataSourceFactory,
109+
Executor executor) {
110+
this(
111+
mediaItem,
112+
manifestParser,
113+
cacheDataSourceFactory,
114+
executor,
115+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
93116
}
94117

95118
/**
@@ -102,13 +125,22 @@ public HlsDownloader(
102125
* @param executor An {@link Executor} used to make requests for the media being downloaded.
103126
* Providing an {@link Executor} that uses multiple threads will speed up the download by
104127
* allowing parts of it to be executed in parallel.
128+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
129+
* segments, up to which the segments (of the same URI) should be merged into a single
130+
* download segment, in milliseconds.
105131
*/
106132
public HlsDownloader(
107133
MediaItem mediaItem,
108134
Parser<HlsPlaylist> manifestParser,
109135
CacheDataSource.Factory cacheDataSourceFactory,
110-
Executor executor) {
111-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
136+
Executor executor,
137+
long maxMergedSegmentStartTimeDiffMs) {
138+
super(
139+
mediaItem,
140+
manifestParser,
141+
cacheDataSourceFactory,
142+
executor,
143+
maxMergedSegmentStartTimeDiffMs);
112144
}
113145

114146
@Override

libraries/exoplayer_smoothstreaming/src/main/java/androidx/media3/exoplayer/smoothstreaming/offline/SsDownloader.java

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,26 @@ public SsDownloader(
9393
.build(),
9494
new SsManifestParser(),
9595
cacheDataSourceFactory,
96-
executor);
96+
executor,
97+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
98+
}
99+
100+
/**
101+
* @deprecated Use {@link SsDownloader#SsDownloader(MediaItem, Parser, CacheDataSource.Factory,
102+
* Executor, long)} instead.
103+
*/
104+
@Deprecated
105+
public SsDownloader(
106+
MediaItem mediaItem,
107+
Parser<SsManifest> manifestParser,
108+
CacheDataSource.Factory cacheDataSourceFactory,
109+
Executor executor) {
110+
this(
111+
mediaItem,
112+
manifestParser,
113+
cacheDataSourceFactory,
114+
executor,
115+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
97116
}
98117

99118
/**
@@ -106,13 +125,22 @@ public SsDownloader(
106125
* @param executor An {@link Executor} used to make requests for the media being downloaded.
107126
* Providing an {@link Executor} that uses multiple threads will speed up the download by
108127
* allowing parts of it to be executed in parallel.
128+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
129+
* segments, up to which the segments (of the same URI) should be merged into a single
130+
* download segment, in milliseconds.
109131
*/
110132
public SsDownloader(
111133
MediaItem mediaItem,
112134
Parser<SsManifest> manifestParser,
113135
CacheDataSource.Factory cacheDataSourceFactory,
114-
Executor executor) {
115-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
136+
Executor executor,
137+
long maxMergedSegmentStartTimeDiffMs) {
138+
super(
139+
mediaItem,
140+
manifestParser,
141+
cacheDataSourceFactory,
142+
executor,
143+
maxMergedSegmentStartTimeDiffMs);
116144
}
117145

118146
@Override

0 commit comments

Comments
 (0)