Skip to content

Commit 3dcdfe5

Browse files
microkatztonihei
authored andcommitted
Merge pull request #248 from lemondoglol:update-segment-size
PiperOrigin-RevId: 507784608 (cherry picked from commit 08342ea)
1 parent bc9f2be commit 3dcdfe5

File tree

4 files changed

+132
-14
lines changed

4 files changed

+132
-14
lines changed

library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public int compareTo(Segment other) {
7272
}
7373
}
7474

75+
public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20 * C.MILLIS_PER_SECOND;
76+
7577
private static final int BUFFER_SIZE_BYTES = 128 * 1024;
76-
private static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND;
7778

7879
private final DataSpec manifestDataSpec;
7980
private final Parser<M> manifestParser;
@@ -83,6 +84,7 @@ public int compareTo(Segment other) {
8384
private final CacheKeyFactory cacheKeyFactory;
8485
@Nullable private final PriorityTaskManager priorityTaskManager;
8586
private final Executor executor;
87+
private final long maxMergedSegmentStartTimeDiffUs;
8688

8789
/**
8890
* The currently active runnables.
@@ -96,6 +98,24 @@ public int compareTo(Segment other) {
9698

9799
private volatile boolean isCanceled;
98100

101+
/**
102+
* @deprecated Use {@link SegmentDownloader#SegmentDownloader(MediaItem, Parser,
103+
* CacheDataSource.Factory, Executor, long)} instead.
104+
*/
105+
@Deprecated
106+
public SegmentDownloader(
107+
MediaItem mediaItem,
108+
Parser<M> manifestParser,
109+
CacheDataSource.Factory cacheDataSourceFactory,
110+
Executor executor) {
111+
this(
112+
mediaItem,
113+
manifestParser,
114+
cacheDataSourceFactory,
115+
executor,
116+
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
117+
}
118+
99119
/**
100120
* @param mediaItem The {@link MediaItem} to be downloaded.
101121
* @param manifestParser A parser for manifests belonging to the media to be downloaded.
@@ -104,12 +124,16 @@ public int compareTo(Segment other) {
104124
* @param executor An {@link Executor} used to make requests for the media being downloaded.
105125
* Providing an {@link Executor} that uses multiple threads will speed up the download by
106126
* allowing parts of it to be executed in parallel.
127+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
128+
* segments, up to which the segments (of the same URI) should be merged into a single
129+
* download segment, in milliseconds.
107130
*/
108131
public SegmentDownloader(
109132
MediaItem mediaItem,
110133
Parser<M> manifestParser,
111134
CacheDataSource.Factory cacheDataSourceFactory,
112-
Executor executor) {
135+
Executor executor,
136+
long maxMergedSegmentStartTimeDiffMs) {
113137
checkNotNull(mediaItem.localConfiguration);
114138
this.manifestDataSpec = getCompressibleDataSpec(mediaItem.localConfiguration.uri);
115139
this.manifestParser = manifestParser;
@@ -120,6 +144,7 @@ public SegmentDownloader(
120144
cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
121145
priorityTaskManager = cacheDataSourceFactory.getUpstreamPriorityTaskManager();
122146
activeRunnables = new ArrayList<>();
147+
maxMergedSegmentStartTimeDiffUs = Util.msToUs(maxMergedSegmentStartTimeDiffMs);
123148
}
124149

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

147172
// Scan the segments, removing any that are fully downloaded.
148173
int totalSegments = segments.size();
@@ -413,7 +438,8 @@ private void removeActiveRunnable(int index) {
413438
}
414439
}
415440

416-
private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFactory) {
441+
private static void mergeSegments(
442+
List<Segment> segments, CacheKeyFactory keyFactory, long maxMergedSegmentStartTimeDiffUs) {
417443
HashMap<String, Integer> lastIndexByCacheKey = new HashMap<>();
418444
int nextOutIndex = 0;
419445
for (int i = 0; i < segments.size(); i++) {
@@ -422,7 +448,7 @@ private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFac
422448
@Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
423449
@Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
424450
if (lastSegment == null
425-
|| segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US
451+
|| segment.startTimeUs > lastSegment.startTimeUs + maxMergedSegmentStartTimeDiffUs
426452
|| !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
427453
lastIndexByCacheKey.put(cacheKey, nextOutIndex);
428454
segments.set(nextOutIndex, segment);

library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloader.java

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

104127
/**
@@ -111,13 +134,22 @@ public DashDownloader(
111134
* @param executor An {@link Executor} used to make requests for the media being downloaded.
112135
* Providing an {@link Executor} that uses multiple threads will speed up the download by
113136
* allowing parts of it to be executed in parallel.
137+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
138+
* segments, up to which the segments (of the same URI) should be merged into a single
139+
* download segment, in milliseconds.
114140
*/
115141
public DashDownloader(
116142
MediaItem mediaItem,
117143
Parser<DashManifest> manifestParser,
118144
CacheDataSource.Factory cacheDataSourceFactory,
119-
Executor executor) {
120-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
145+
Executor executor,
146+
long maxMergedSegmentStartTimeDiffMs) {
147+
super(
148+
mediaItem,
149+
manifestParser,
150+
cacheDataSourceFactory,
151+
executor,
152+
maxMergedSegmentStartTimeDiffMs);
121153
baseUrlExclusionList = new BaseUrlExclusionList();
122154
}
123155

library/hls/src/main/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloader.java

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

93116
/**
@@ -100,13 +123,22 @@ public HlsDownloader(
100123
* @param executor An {@link Executor} used to make requests for the media being downloaded.
101124
* Providing an {@link Executor} that uses multiple threads will speed up the download by
102125
* allowing parts of it to be executed in parallel.
126+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
127+
* segments, up to which the segments (of the same URI) should be merged into a single
128+
* download segment, in milliseconds.
103129
*/
104130
public HlsDownloader(
105131
MediaItem mediaItem,
106132
Parser<HlsPlaylist> manifestParser,
107133
CacheDataSource.Factory cacheDataSourceFactory,
108-
Executor executor) {
109-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
134+
Executor executor,
135+
long maxMergedSegmentStartTimeDiffMs) {
136+
super(
137+
mediaItem,
138+
manifestParser,
139+
cacheDataSourceFactory,
140+
executor,
141+
maxMergedSegmentStartTimeDiffMs);
110142
}
111143

112144
@Override

library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloader.java

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

97116
/**
@@ -104,13 +123,22 @@ public SsDownloader(
104123
* @param executor An {@link Executor} used to make requests for the media being downloaded.
105124
* Providing an {@link Executor} that uses multiple threads will speed up the download by
106125
* allowing parts of it to be executed in parallel.
126+
* @param maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
127+
* segments, up to which the segments (of the same URI) should be merged into a single
128+
* download segment, in milliseconds.
107129
*/
108130
public SsDownloader(
109131
MediaItem mediaItem,
110132
Parser<SsManifest> manifestParser,
111133
CacheDataSource.Factory cacheDataSourceFactory,
112-
Executor executor) {
113-
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
134+
Executor executor,
135+
long maxMergedSegmentStartTimeDiffMs) {
136+
super(
137+
mediaItem,
138+
manifestParser,
139+
cacheDataSourceFactory,
140+
executor,
141+
maxMergedSegmentStartTimeDiffMs);
114142
}
115143

116144
@Override

0 commit comments

Comments
 (0)