Merge pull request #248 from lemondoglol:update-segment-size

PiperOrigin-RevId: 507784608
This commit is contained in:
microkatz 2023-02-08 14:08:36 +00:00
commit ecd91d865c
5 changed files with 135 additions and 14 deletions

View File

@ -20,6 +20,9 @@
for seeking.
* Use theme when loading drawables on API 21+
([#220](https://github.com/androidx/media/issues/220)).
* Make the maximum difference of the start time of two segments to be
merged configurable in `SegmentDownloader` and subclasses
([#248](https://github.com/androidx/media/pull/248)).
* Add `ConcatenatingMediaSource2` that allows combining multiple media
items into a single window
([#247](https://github.com/androidx/media/issues/247)).

View File

@ -75,8 +75,9 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
}
}
public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20 * C.MILLIS_PER_SECOND;
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;
private final DataSpec manifestDataSpec;
private final Parser<M> manifestParser;
@ -86,6 +87,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
private final CacheKeyFactory cacheKeyFactory;
@Nullable private final PriorityTaskManager priorityTaskManager;
private final Executor executor;
private final long maxMergedSegmentStartTimeDiffUs;
/**
* The currently active runnables.
@ -99,6 +101,24 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
private volatile boolean isCanceled;
/**
* @deprecated Use {@link SegmentDownloader#SegmentDownloader(MediaItem, Parser,
* CacheDataSource.Factory, Executor, long)} instead.
*/
@Deprecated
public SegmentDownloader(
MediaItem mediaItem,
Parser<M> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
}
/**
* @param mediaItem The {@link MediaItem} to be downloaded.
* @param manifestParser A parser for manifests belonging to the media to be downloaded.
@ -107,12 +127,16 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
* @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 maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
* segments, up to which the segments (of the same URI) should be merged into a single
* download segment, in milliseconds.
*/
public SegmentDownloader(
MediaItem mediaItem,
Parser<M> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
Executor executor,
long maxMergedSegmentStartTimeDiffMs) {
checkNotNull(mediaItem.localConfiguration);
this.manifestDataSpec = getCompressibleDataSpec(mediaItem.localConfiguration.uri);
this.manifestParser = manifestParser;
@ -123,6 +147,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
cacheKeyFactory = cacheDataSourceFactory.getCacheKeyFactory();
priorityTaskManager = cacheDataSourceFactory.getUpstreamPriorityTaskManager();
activeRunnables = new ArrayList<>();
maxMergedSegmentStartTimeDiffUs = Util.msToUs(maxMergedSegmentStartTimeDiffMs);
}
@Override
@ -145,7 +170,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
// Sort the segments so that we download media in the right order from the start of the
// content, and merge segments where possible to minimize the number of server round trips.
Collections.sort(segments);
mergeSegments(segments, cacheKeyFactory);
mergeSegments(segments, cacheKeyFactory, maxMergedSegmentStartTimeDiffUs);
// Scan the segments, removing any that are fully downloaded.
int totalSegments = segments.size();
@ -416,7 +441,8 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
}
}
private static void mergeSegments(List<Segment> segments, CacheKeyFactory keyFactory) {
private static void mergeSegments(
List<Segment> segments, CacheKeyFactory keyFactory, long maxMergedSegmentStartTimeDiffUs) {
HashMap<String, Integer> lastIndexByCacheKey = new HashMap<>();
int nextOutIndex = 0;
for (int i = 0; i < segments.size(); i++) {
@ -425,7 +451,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
@Nullable Integer lastIndex = lastIndexByCacheKey.get(cacheKey);
@Nullable Segment lastSegment = lastIndex == null ? null : segments.get(lastIndex);
if (lastSegment == null
|| segment.startTimeUs > lastSegment.startTimeUs + MAX_MERGED_SEGMENT_START_TIME_DIFF_US
|| segment.startTimeUs > lastSegment.startTimeUs + maxMergedSegmentStartTimeDiffUs
|| !canMergeSegments(lastSegment.dataSpec, segment.dataSpec)) {
lastIndexByCacheKey.put(cacheKey, nextOutIndex);
segments.set(nextOutIndex, segment);

View File

@ -100,7 +100,30 @@ public final class DashDownloader extends SegmentDownloader<DashManifest> {
*/
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_MS);
}
/**
* @deprecated Use {@link DashDownloader#DashDownloader(MediaItem, Parser,
* CacheDataSource.Factory, Executor, long)} instead.
*/
@Deprecated
public DashDownloader(
MediaItem mediaItem,
Parser<DashManifest> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
}
/**
@ -113,13 +136,22 @@ public final class DashDownloader extends SegmentDownloader<DashManifest> {
* @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 maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
* segments, up to which the segments (of the same URI) should be merged into a single
* download segment, in milliseconds.
*/
public DashDownloader(
MediaItem mediaItem,
Parser<DashManifest> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
Executor executor,
long maxMergedSegmentStartTimeDiffMs) {
super(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
maxMergedSegmentStartTimeDiffMs);
baseUrlExclusionList = new BaseUrlExclusionList();
}

View File

@ -89,7 +89,30 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
*/
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_MS);
}
/**
* @deprecated Use {@link HlsDownloader#HlsDownloader(MediaItem, Parser, CacheDataSource.Factory,
* Executor, long)} instead.
*/
@Deprecated
public HlsDownloader(
MediaItem mediaItem,
Parser<HlsPlaylist> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
}
/**
@ -102,13 +125,22 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
* @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 maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
* segments, up to which the segments (of the same URI) should be merged into a single
* download segment, in milliseconds.
*/
public HlsDownloader(
MediaItem mediaItem,
Parser<HlsPlaylist> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
Executor executor,
long maxMergedSegmentStartTimeDiffMs) {
super(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
maxMergedSegmentStartTimeDiffMs);
}
@Override

View File

@ -93,7 +93,26 @@ public final class SsDownloader extends SegmentDownloader<SsManifest> {
.build(),
new SsManifestParser(),
cacheDataSourceFactory,
executor);
executor,
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
}
/**
* @deprecated Use {@link SsDownloader#SsDownloader(MediaItem, Parser, CacheDataSource.Factory,
* Executor, long)} instead.
*/
@Deprecated
public SsDownloader(
MediaItem mediaItem,
Parser<SsManifest> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS);
}
/**
@ -106,13 +125,22 @@ public final class SsDownloader extends SegmentDownloader<SsManifest> {
* @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 maxMergedSegmentStartTimeDiffMs The maximum difference of the start time of two
* segments, up to which the segments (of the same URI) should be merged into a single
* download segment, in milliseconds.
*/
public SsDownloader(
MediaItem mediaItem,
Parser<SsManifest> manifestParser,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(mediaItem, manifestParser, cacheDataSourceFactory, executor);
Executor executor,
long maxMergedSegmentStartTimeDiffMs) {
super(
mediaItem,
manifestParser,
cacheDataSourceFactory,
executor,
maxMergedSegmentStartTimeDiffMs);
}
@Override