Commit 0ba397cd by olly Committed by Oliver Woodman

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
parent 2e1024f0
...@@ -21,6 +21,8 @@ import androidx.annotation.Nullable; ...@@ -21,6 +21,8 @@ import androidx.annotation.Nullable;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec; 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.Cache;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource; import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory; import com.google.android.exoplayer2.upstream.cache.CacheKeyFactory;
...@@ -68,6 +70,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -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 static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND;
private final DataSpec manifestDataSpec; private final DataSpec manifestDataSpec;
private final Parser<M> manifestParser;
private final ArrayList<StreamKey> streamKeys; private final ArrayList<StreamKey> streamKeys;
private final CacheDataSource.Factory cacheDataSourceFactory; private final CacheDataSource.Factory cacheDataSourceFactory;
private final Executor executor; private final Executor executor;
...@@ -75,6 +78,7 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -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 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. * @param streamKeys Keys defining which streams in the manifest should be selected for download.
* If empty, all streams are downloaded. * If empty, all streams are downloaded.
* @param cacheDataSourceFactory A {@link CacheDataSource.Factory} for the cache into which the * @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 ...@@ -85,10 +89,12 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
*/ */
public SegmentDownloader( public SegmentDownloader(
Uri manifestUri, Uri manifestUri,
Parser<M> manifestParser,
List<StreamKey> streamKeys, List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory, CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) { Executor executor) {
this.manifestDataSpec = getCompressibleDataSpec(manifestUri); this.manifestDataSpec = getCompressibleDataSpec(manifestUri);
this.manifestParser = manifestParser;
this.streamKeys = new ArrayList<>(streamKeys); this.streamKeys = new ArrayList<>(streamKeys);
this.cacheDataSourceFactory = cacheDataSourceFactory; this.cacheDataSourceFactory = cacheDataSourceFactory;
this.executor = executor; this.executor = executor;
...@@ -209,14 +215,16 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme ...@@ -209,14 +215,16 @@ public abstract class SegmentDownloader<M extends FilterableManifest<M>> impleme
// Internal methods. // Internal methods.
/** /**
* Loads and parses the manifest. * Loads and parses a manifest.
* *
* @param dataSource The {@link DataSource} through which to load. * @param dataSource The {@link DataSource} through which to load.
* @param dataSpec The manifest {@link DataSpec}. * @param dataSpec The manifest {@link DataSpec}.
* @return The manifest. * @return The manifest.
* @throws IOException If an error occurs reading data. * @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. * Returns a list of all downloadable {@link Segment}s for a given manifest.
......
...@@ -33,7 +33,6 @@ import com.google.android.exoplayer2.source.dash.manifest.RangedUri; ...@@ -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.source.dash.manifest.Representation;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec; 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.upstream.cache.CacheDataSource;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -92,13 +91,7 @@ public final class DashDownloader extends SegmentDownloader<DashManifest> { ...@@ -92,13 +91,7 @@ public final class DashDownloader extends SegmentDownloader<DashManifest> {
List<StreamKey> streamKeys, List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory, CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) { Executor executor) {
super(manifestUri, streamKeys, cacheDataSourceFactory, executor); super(manifestUri, new DashManifestParser(), streamKeys, cacheDataSourceFactory, executor);
}
@Override
protected DashManifest getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return ParsingLoadable.load(
dataSource, new DashManifestParser(), dataSpec, C.DATA_TYPE_MANIFEST);
} }
@Override @Override
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
package com.google.android.exoplayer2.source.hls.offline; package com.google.android.exoplayer2.source.hls.offline;
import android.net.Uri; 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.SegmentDownloader;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist; import com.google.android.exoplayer2.source.hls.playlist.HlsMasterPlaylist;
...@@ -25,7 +25,6 @@ import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylist; ...@@ -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.source.hls.playlist.HlsPlaylistParser;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec; 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.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.util.UriUtil; import com.google.android.exoplayer2.util.UriUtil;
import java.io.IOException; import java.io.IOException;
...@@ -86,12 +85,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> { ...@@ -86,12 +85,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
List<StreamKey> streamKeys, List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory, CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) { Executor executor) {
super(playlistUri, streamKeys, cacheDataSourceFactory, executor); super(playlistUri, new HlsPlaylistParser(), streamKeys, cacheDataSourceFactory, executor);
}
@Override
protected HlsPlaylist getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException {
return loadManifest(dataSource, dataSpec);
} }
@Override @Override
...@@ -112,7 +106,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> { ...@@ -112,7 +106,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
segments.add(new Segment(/* startTimeUs= */ 0, mediaPlaylistDataSpec)); segments.add(new Segment(/* startTimeUs= */ 0, mediaPlaylistDataSpec));
HlsMediaPlaylist mediaPlaylist; HlsMediaPlaylist mediaPlaylist;
try { try {
mediaPlaylist = (HlsMediaPlaylist) loadManifest(dataSource, mediaPlaylistDataSpec); mediaPlaylist = (HlsMediaPlaylist) getManifest(dataSource, mediaPlaylistDataSpec);
} catch (IOException e) { } catch (IOException e) {
if (!allowIncompleteList) { if (!allowIncompleteList) {
throw e; throw e;
...@@ -120,7 +114,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> { ...@@ -120,7 +114,7 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> {
// Generating an incomplete segment list is allowed. Advance to the next media playlist. // Generating an incomplete segment list is allowed. Advance to the next media playlist.
continue; continue;
} }
HlsMediaPlaylist.Segment lastInitSegment = null; @Nullable HlsMediaPlaylist.Segment lastInitSegment = null;
List<HlsMediaPlaylist.Segment> hlsSegments = mediaPlaylist.segments; List<HlsMediaPlaylist.Segment> hlsSegments = mediaPlaylist.segments;
for (int i = 0; i < hlsSegments.size(); i++) { for (int i = 0; i < hlsSegments.size(); i++) {
HlsMediaPlaylist.Segment segment = hlsSegments.get(i); HlsMediaPlaylist.Segment segment = hlsSegments.get(i);
...@@ -141,12 +135,6 @@ public final class HlsDownloader extends SegmentDownloader<HlsPlaylist> { ...@@ -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( private void addSegment(
HlsMediaPlaylist mediaPlaylist, HlsMediaPlaylist mediaPlaylist,
HlsMediaPlaylist.Segment segment, HlsMediaPlaylist.Segment segment,
......
...@@ -16,7 +16,6 @@ ...@@ -16,7 +16,6 @@
package com.google.android.exoplayer2.source.smoothstreaming.offline; package com.google.android.exoplayer2.source.smoothstreaming.offline;
import android.net.Uri; import android.net.Uri;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.offline.SegmentDownloader; import com.google.android.exoplayer2.offline.SegmentDownloader;
import com.google.android.exoplayer2.offline.StreamKey; import com.google.android.exoplayer2.offline.StreamKey;
import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest; import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifest;
...@@ -25,9 +24,7 @@ import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestP ...@@ -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.source.smoothstreaming.manifest.SsUtil;
import com.google.android.exoplayer2.upstream.DataSource; import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSpec; 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.upstream.cache.CacheDataSource;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
...@@ -85,12 +82,12 @@ public final class SsDownloader extends SegmentDownloader<SsManifest> { ...@@ -85,12 +82,12 @@ public final class SsDownloader extends SegmentDownloader<SsManifest> {
List<StreamKey> streamKeys, List<StreamKey> streamKeys,
CacheDataSource.Factory cacheDataSourceFactory, CacheDataSource.Factory cacheDataSourceFactory,
Executor executor) { Executor executor) {
super(SsUtil.fixManifestUri(manifestUri), streamKeys, cacheDataSourceFactory, executor); super(
} SsUtil.fixManifestUri(manifestUri),
new SsManifestParser(),
@Override streamKeys,
protected SsManifest getManifest(DataSource dataSource, DataSpec dataSpec) throws IOException { cacheDataSourceFactory,
return ParsingLoadable.load(dataSource, new SsManifestParser(), dataSpec, C.DATA_TYPE_MANIFEST); executor);
} }
@Override @Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment