Skip to content

Commit 1114064

Browse files
committed
make merged segment size configurable
1 parent e2ece2f commit 1114064

File tree

4 files changed

+138
-11
lines changed

4 files changed

+138
-11
lines changed

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

+27-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ public int compareTo(Segment other) {
7676
}
7777

7878
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;
79+
public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND;
80+
private static long maxMergedSegmentStartTimeDiffUs = DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US;
8081

8182
private final DataSpec manifestDataSpec;
8283
private final Parser<M> manifestParser;
@@ -107,7 +108,31 @@ public int compareTo(Segment other) {
107108
* @param executor An {@link Executor} used to make requests for the media being downloaded.
108109
* Providing an {@link Executor} that uses multiple threads will speed up the download by
109110
* allowing parts of it to be executed in parallel.
111+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two segments,
112+
* up to which the segments (of the same URI) should be merged into a single download segment,
113+
* in milliseconds.
110114
*/
115+
public SegmentDownloader(
116+
MediaItem mediaItem,
117+
Parser<M> manifestParser,
118+
CacheDataSource.Factory cacheDataSourceFactory,
119+
Executor executor,
120+
long maxMergedSegmentStartTimeDiffMs
121+
) {
122+
checkNotNull(mediaItem.localConfiguration);
123+
this.manifestDataSpec = getCompressibleDataSpec(mediaItem.localConfiguration.uri);
124+
this.manifestParser = manifestParser;
125+
this.streamKeys = new ArrayList<>(mediaItem.localConfiguration.streamKeys);
126+
this.cacheDataSourceFactory = cacheDataSourceFactory;
127+
this.executor = executor;
128+
cache = Assertions.checkNotNull(cacheDataSourceFactory.getCache());
129+
cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
130+
priorityTaskManager = cacheDataSourceFactory.getUpstreamPriorityTaskManager();
131+
activeRunnables = new ArrayList<>();
132+
maxMergedSegmentStartTimeDiffUs = Util.msToUs(maxMergedSegmentStartTimeDiffMs);
133+
}
134+
135+
@Deprecated
111136
public SegmentDownloader(
112137
MediaItem mediaItem,
113138
Parser<M> manifestParser,
@@ -425,7 +450,7 @@ private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFac
425450
@Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
426451
@Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
427452
if (lastSegment == null
428-
|| segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US
453+
|| segment.startTimeUs > lastSegment.startTimeUs + maxMergedSegmentStartTimeDiffUs
429454
|| !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
430455
lastIndexByCacheKey.put(cacheKey, nextOutIndex);
431456
segments.set(nextOutIndex, segment);

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

+35-3
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,28 @@ 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(mediaItem, new DashManifestParser(), cacheDataSourceFactory, executor, DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US);
104+
}
105+
106+
/**
107+
* @deprecated Use {@link DashDownloader#DashDownloader(MediaItem, Parser,
108+
* CacheDataSource.Factory, Executor, long)} instead.
109+
*/
110+
@Deprecated
111+
public DashDownloader(
112+
MediaItem mediaItem,
113+
Parser<DashManifest> manifestParser,
114+
CacheDataSource.Factory cacheDataSourceFactory,
115+
Executor executor
116+
) {
117+
super(
118+
mediaItem,
119+
manifestParser,
120+
cacheDataSourceFactory,
121+
executor,
122+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US
123+
);
124+
baseUrlExclusionList = new BaseUrlExclusionList();
104125
}
105126

106127
/**
@@ -113,13 +134,24 @@ public DashDownloader(
113134
* @param executor An {@link Executor} used to make requests for the media being downloaded.
114135
* Providing an {@link Executor} that uses multiple threads will speed up the download by
115136
* allowing parts of it to be executed in parallel.
137+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two segments,
138+
* up to which the segments (of the same URI) should be merged into a single download segment,
139+
* in milliseconds.
116140
*/
117141
public DashDownloader(
118142
MediaItem mediaItem,
119143
Parser<DashManifest> manifestParser,
120144
CacheDataSource.Factory cacheDataSourceFactory,
121-
Executor executor) {
122-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
145+
Executor executor,
146+
long maxMergedSegmentStartTimeDiffMs
147+
) {
148+
super(
149+
mediaItem,
150+
manifestParser,
151+
cacheDataSourceFactory,
152+
executor,
153+
maxMergedSegmentStartTimeDiffMs
154+
);
123155
baseUrlExclusionList = new BaseUrlExclusionList();
124156
}
125157

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

+40-3
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,33 @@ 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_US
98+
);
99+
}
100+
101+
/**
102+
* @deprecated Use {@link HlsDownloader#HlsDownloader(MediaItem, Parser,
103+
* CacheDataSource.Factory, Executor, long)} instead.
104+
*/
105+
@Deprecated
106+
public HlsDownloader(
107+
MediaItem mediaItem,
108+
Parser<HlsPlaylist> manifestParser,
109+
CacheDataSource.Factory cacheDataSourceFactory,
110+
Executor executor
111+
) {
112+
super(
113+
mediaItem,
114+
manifestParser,
115+
cacheDataSourceFactory,
116+
executor,
117+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US
118+
);
93119
}
94120

95121
/**
@@ -102,13 +128,24 @@ public HlsDownloader(
102128
* @param executor An {@link Executor} used to make requests for the media being downloaded.
103129
* Providing an {@link Executor} that uses multiple threads will speed up the download by
104130
* allowing parts of it to be executed in parallel.
131+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two segments,
132+
* up to which the segments (of the same URI) should be merged into a single download segment,
133+
* in milliseconds.
105134
*/
106135
public HlsDownloader(
107136
MediaItem mediaItem,
108137
Parser<HlsPlaylist> manifestParser,
109138
CacheDataSource.Factory cacheDataSourceFactory,
110-
Executor executor) {
111-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
139+
Executor executor,
140+
long maxMergedSegmentStartTimeDiffMs
141+
) {
142+
super(
143+
mediaItem,
144+
manifestParser,
145+
cacheDataSourceFactory,
146+
executor,
147+
maxMergedSegmentStartTimeDiffMs
148+
);
112149
}
113150

114151
@Override

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

+36-3
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,29 @@ public SsDownloader(
9393
.build(),
9494
new SsManifestParser(),
9595
cacheDataSourceFactory,
96-
executor);
96+
executor,
97+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US
98+
);
99+
}
100+
101+
/**
102+
* @deprecated Use {@link SsDownloader#SsDownloader(MediaItem, Parser,
103+
* CacheDataSource.Factory, Executor, long)} instead.
104+
*/
105+
@Deprecated
106+
public SsDownloader(
107+
MediaItem mediaItem,
108+
Parser<SsManifest> manifestParser,
109+
CacheDataSource.Factory cacheDataSourceFactory,
110+
Executor executor
111+
) {
112+
super(
113+
mediaItem,
114+
manifestParser,
115+
cacheDataSourceFactory,
116+
executor,
117+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_US
118+
);
97119
}
98120

99121
/**
@@ -106,13 +128,24 @@ public SsDownloader(
106128
* @param executor An {@link Executor} used to make requests for the media being downloaded.
107129
* Providing an {@link Executor} that uses multiple threads will speed up the download by
108130
* allowing parts of it to be executed in parallel.
131+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two segments,
132+
* up to which the segments (of the same URI) should be merged into a single download segment,
133+
* in milliseconds.
109134
*/
110135
public SsDownloader(
111136
MediaItem mediaItem,
112137
Parser<SsManifest> manifestParser,
113138
CacheDataSource.Factory cacheDataSourceFactory,
114-
Executor executor) {
115-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
139+
Executor executor,
140+
long maxMergedSegmentStartTimeDiffMs
141+
) {
142+
super(
143+
mediaItem,
144+
manifestParser,
145+
cacheDataSourceFactory,
146+
executor,
147+
maxMergedSegmentStartTimeDiffMs
148+
);
116149
}
117150

118151
@Override

0 commit comments

Comments
 (0)