SegmentDownloader: Pull manifest loading up to base class.

This will make it a bit easier to push manifest loads to an Executor.

Issue: #5978
PiperOrigin-RevId: 308608155
This commit is contained in:
olly 2020-04-27 14:26:09 +01:00 committed by Oliver Woodman
parent 2e1024f0d1
commit 0ba397cd4e
4 changed files with 21 additions and 35 deletions

View file

@ -21,6 +21,8 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.ParsingLoadable.Parser;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory;
@ -68,6 +70,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
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;
private final ArrayList<StreamKey> streamKeys;
private final CacheDataSource.Factory cacheDataSourceFactory;
private final Executor executor;
@ -75,6 +78,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
/**
* @param manifestUri The {@link Uri} of the manifest to be downloaded.
* @param manifestParser A parser for the manifest.
* @param streamKeys Keys defining which streams in the manifest should be selected for download.
* If empty, all streams are downloaded.
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the
@ -85,10 +89,12 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
*/
public SegmentDownloader(
Uri manifestUri,
Parser<M> manifestParser,
List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
this.manifestDataSpec = getCompressibleDataSpec(manifestUri);
this.manifestParser = manifestParser;
this.streamKeys = new ArrayList<>(streamKeys);
this.cacheDataSourceFactory = cacheDataSourceFactory;
this.executor = executor;
@ -209,14 +215,16 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
// Internal methods.
/**
* Loads and parses the manifest.
* Loads and parses a manifest.
*
* @param dataSource The {@link DataSource} through which to load.
* @param dataSpec The manifest {@link DataSpec}.
* @return The manifest.
* @throws IOException If an error occurs reading data.
*/
protected abstract M getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException;
protected final M getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return ParsingLoadable.load(dataSource, manifestParser, dataSpec, C.DATA_TYPE_MANIFEST);
}
/**
* Returns a list of all downloadable {@link Segment}s for a given manifest.

View file

@ -33,7 +33,6 @@ import com.google.android.exoplayer2.source.dash.manifest.RangedUri;
import com.google.android.exoplayer2.source.dash.manifest.Representation;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import java.io.IOException;
import java.util.ArrayList;
@ -92,13 +91,7 @@ public final class DashDownloader extends SegmentDownloader<DashManifest> {
List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(manifestUri, streamKeys, cacheDataSourceFactory, executor);
}
@Override
protected DashManifest getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return ParsingLoadable.load(
dataSource, new DashManifestParser(), dataSpec, C.DATA_TYPE_MANIFEST);
super(manifestUri, new DashManifestParser(), streamKeys, cacheDataSourceFactory, executor);
}
@Override

View file

@ -16,7 +16,7 @@
package com.google.android.exoplayer2.source.hls.offline;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import androidx.annotation.Nullable;
import com.google.android.exoplayer2.offline.SegmentDownloader;
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist;
@ -25,7 +25,6 @@ import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylist;
import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.util.UriUtil;
import java.io.IOException;
@ -86,12 +85,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(playlistUri, streamKeys, cacheDataSourceFactory, executor);
}
@Override
protected HlsPlaylist getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return loadManifest(dataSource, dataSpec);
super(playlistUri, new HlsPlaylistParser(), streamKeys, cacheDataSourceFactory, executor);
}
@Override
@ -112,7 +106,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
segments.add(new Segment(/* startTimeUs= */ 0, mediaPlaylistDataSpec));
HlsMediaPlaylist mediaPlaylist;
try {
mediaPlaylist = (HlsMediaPlaylist) loadManifest(dataSource, mediaPlaylistDataSpec);
mediaPlaylist = (HlsMediaPlaylist) getManifest(dataSource, mediaPlaylistDataSpec);
} catch (IOException e) {
if (!allowIncompleteList) {
throw e;
@ -120,7 +114,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
// Generating an incomplete segment list is allowed. Advance to the next media playlist.
continue;
}
HlsMediaPlaylist.Segment lastInitSegment = null;
@Nullable HlsMediaPlaylist.Segment lastInitSegment = null;
List<HlsMediaPlaylist.Segment> hlsSegments = mediaPlaylist.segments;
for (int i = 0; i < hlsSegments.size(); i++) {
HlsMediaPlaylist.Segment segment = hlsSegments.get(i);
@ -141,12 +135,6 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
}
}
private static HlsPlaylist loadManifest(DataSource dataSource, DataSpec dataSpec)
throws IOException {
return ParsingLoadable.load(
dataSource, new HlsPlaylistParser(), dataSpec, C.DATA_TYPE_MANIFEST);
}
private void addSegment(
HlsMediaPlaylist mediaPlaylist,
HlsMediaPlaylist.Segment segment,

View file

@ -16,7 +16,6 @@
package com.google.android.exoplayer2.source.smoothstreaming.offline;
import android.net.Uri;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.SegmentDownloader;
import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest;
@ -25,9 +24,7 @@ import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestP
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsUtil;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec;
import com.google.android.exoplayer2.upstream.ParsingLoadable;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
@ -85,12 +82,12 @@ public final class SsDownloader extends SegmentDownloader<SsManifest> {
List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) {
super(SsUtil.fixManifestUri(manifestUri), streamKeys, cacheDataSourceFactory, executor);
}
@Override
protected SsManifest getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return ParsingLoadable.load(dataSource, new SsManifestParser(), dataSpec, C.DATA_TYPE_MANIFEST);
super(
SsUtil.fixManifestUri(manifestUri),
new SsManifestParser(),
streamKeys,
cacheDataSourceFactory,
executor);
}
@Override