From 0ba397cd4ed85cf13d5e1e49deab549323a7c2fb Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 27 Apr 2020 14:26:09 +0100 Subject: [PATCH] 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 --- .../exoplayer2/offline/SegmentDownloader.java | 12 +++++++++-- .../source/dash/offline/DashDownloader.java | 9 +-------- .../source/hls/offline/HlsDownloader.java | 20 ++++--------------- .../smoothstreaming/offline/SsDownloader.java | 15 ++++++-------- 4 files changed, 21 insertions(+), 35 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java index 3bf792988e..56f85c2d53 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/SegmentDownloader.java @@ -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> impleme private static final long MAX_MERGED_SEGMENT_START_TIME_DIFF_US = 20 * C.MICROS_PER_SECOND; private final DataSpec manifestDataSpec; + private final Parser manifestParser; private final ArrayList streamKeys; private final CacheDataSource.Factory cacheDataSourceFactory; private final Executor executor; @@ -75,6 +78,7 @@ public abstract class SegmentDownloader> 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> impleme */ public SegmentDownloader( Uri manifestUri, + Parser manifestParser, List 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> 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. diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloader.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloader.java index 8629c919d0..30da21d35d 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloader.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/offline/DashDownloader.java @@ -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 { List 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 diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloader.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloader.java index cd8651deff..2e97c4bc58 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloader.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/offline/HlsDownloader.java @@ -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 { List 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 { 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 { // Generating an incomplete segment list is allowed. Advance to the next media playlist. continue; } - HlsMediaPlaylist.Segment lastInitSegment = null; + @Nullable HlsMediaPlaylist.Segment lastInitSegment = null; List 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 { } } - 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, diff --git a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloader.java b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloader.java index dc141a212f..3a2cf10439 100644 --- a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloader.java +++ b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/offline/SsDownloader.java @@ -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 { List 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