From 0d9d1998f457aa4f919f78a7663ac24202aa133b Mon Sep 17 00:00:00 2001 From: olly Date: Wed, 25 Apr 2018 03:54:13 -0700 Subject: [PATCH] Centralize manifest filtering. The generic type for track key will go away soon. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=194220450 --- .../exoplayer2/demo/PlayerActivity.java | 18 ++++--- .../offline/FilterableManifest.java | 36 +++++++++++++ .../offline/FilteringManifestParser.java | 45 +++++++++++++++++ .../source/dash/manifest/DashManifest.java | 3 +- .../manifest/FilteringDashManifestParser.java | 48 ------------------ .../playlist/FilteringHlsPlaylistParser.java | 50 ------------------- .../source/hls/playlist/HlsMediaPlaylist.java | 5 ++ .../source/hls/playlist/HlsPlaylist.java | 7 ++- .../manifest/FilteringSsManifestParser.java | 47 ----------------- .../smoothstreaming/manifest/SsManifest.java | 7 +-- 10 files changed, 107 insertions(+), 159 deletions(-) create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/offline/FilterableManifest.java create mode 100644 library/core/src/main/java/com/google/android/exoplayer2/offline/FilteringManifestParser.java delete mode 100644 library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/FilteringDashManifestParser.java delete mode 100644 library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/FilteringHlsPlaylistParser.java delete mode 100644 library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/FilteringSsManifestParser.java diff --git a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java index 9e98f62fbd..e3f4c01810 100644 --- a/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java +++ b/demos/main/src/main/java/com/google/android/exoplayer2/demo/PlayerActivity.java @@ -48,6 +48,7 @@ import com.google.android.exoplayer2.drm.HttpMediaDrmCallback; import com.google.android.exoplayer2.drm.UnsupportedDrmException; import com.google.android.exoplayer2.mediacodec.MediaCodecRenderer.DecoderInitializationException; import com.google.android.exoplayer2.mediacodec.MediaCodecUtil.DecoderQueryException; +import com.google.android.exoplayer2.offline.FilteringManifestParser; import com.google.android.exoplayer2.source.BehindLiveWindowException; import com.google.android.exoplayer2.source.ConcatenatingMediaSource; import com.google.android.exoplayer2.source.ExtractorMediaSource; @@ -57,14 +58,14 @@ import com.google.android.exoplayer2.source.ads.AdsLoader; import com.google.android.exoplayer2.source.ads.AdsMediaSource; import com.google.android.exoplayer2.source.dash.DashMediaSource; import com.google.android.exoplayer2.source.dash.DefaultDashChunkSource; -import com.google.android.exoplayer2.source.dash.manifest.FilteringDashManifestParser; +import com.google.android.exoplayer2.source.dash.manifest.DashManifestParser; import com.google.android.exoplayer2.source.dash.manifest.RepresentationKey; import com.google.android.exoplayer2.source.hls.HlsMediaSource; -import com.google.android.exoplayer2.source.hls.playlist.FilteringHlsPlaylistParser; +import com.google.android.exoplayer2.source.hls.playlist.HlsPlaylistParser; import com.google.android.exoplayer2.source.hls.playlist.RenditionKey; import com.google.android.exoplayer2.source.smoothstreaming.DefaultSsChunkSource; import com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource; -import com.google.android.exoplayer2.source.smoothstreaming.manifest.FilteringSsManifestParser; +import com.google.android.exoplayer2.source.smoothstreaming.manifest.SsManifestParser; import com.google.android.exoplayer2.source.smoothstreaming.manifest.TrackKey; import com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection; import com.google.android.exoplayer2.trackselection.DefaultTrackSelector; @@ -459,17 +460,22 @@ public class PlayerActivity extends Activity new DefaultDashChunkSource.Factory(mediaDataSourceFactory), buildDataSourceFactory(false)) .setManifestParser( - new FilteringDashManifestParser((List) manifestFilter)) + new FilteringManifestParser<>( + new DashManifestParser(), (List) manifestFilter)) .createMediaSource(uri); case C.TYPE_SS: return new SsMediaSource.Factory( new DefaultSsChunkSource.Factory(mediaDataSourceFactory), buildDataSourceFactory(false)) - .setManifestParser(new FilteringSsManifestParser((List) manifestFilter)) + .setManifestParser( + new FilteringManifestParser<>( + new SsManifestParser(), (List) manifestFilter)) .createMediaSource(uri); case C.TYPE_HLS: return new HlsMediaSource.Factory(mediaDataSourceFactory) - .setPlaylistParser(new FilteringHlsPlaylistParser((List) manifestFilter)) + .setPlaylistParser( + new FilteringManifestParser<>( + new HlsPlaylistParser(), (List) manifestFilter)) .createMediaSource(uri); case C.TYPE_OTHER: return new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri); diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/FilterableManifest.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/FilterableManifest.java new file mode 100644 index 0000000000..909bc81a45 --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/FilterableManifest.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.offline; + +import java.util.List; + +/** + * A manifest that can generate copies of itself including only the tracks specified by the given + * track keys. + * + * @param The manifest type. + * @param The track key type. + */ +public interface FilterableManifest { + + /** + * Returns a copy of the manifest including only the tracks specified by the given track keys. + * + * @param trackKeys A non-empty list of track keys. + * @return The filtered manifest. + */ + T copy(List trackKeys); +} diff --git a/library/core/src/main/java/com/google/android/exoplayer2/offline/FilteringManifestParser.java b/library/core/src/main/java/com/google/android/exoplayer2/offline/FilteringManifestParser.java new file mode 100644 index 0000000000..8fec07552b --- /dev/null +++ b/library/core/src/main/java/com/google/android/exoplayer2/offline/FilteringManifestParser.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.google.android.exoplayer2.offline; + +import android.net.Uri; +import com.google.android.exoplayer2.upstream.ParsingLoadable.Parser; +import java.io.IOException; +import java.io.InputStream; +import java.util.List; + +/** A manifest parser that includes only the tracks identified by the given track keys. */ +public final class FilteringManifestParser, K> + implements Parser { + + private final Parser parser; + private final List trackKeys; + + /** + * @param parser A parser for the manifest that will be filtered. + * @param trackKeys The track keys. If null or empty then filtering will not occur. + */ + public FilteringManifestParser(Parser parser, List trackKeys) { + this.parser = parser; + this.trackKeys = trackKeys; + } + + @Override + public T parse(Uri uri, InputStream inputStream) throws IOException { + T manifest = parser.parse(uri, inputStream); + return trackKeys == null || trackKeys.isEmpty() ? manifest : manifest.copy(trackKeys); + } +} diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/DashManifest.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/DashManifest.java index 95fe938fa4..67c61fe06d 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/DashManifest.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/DashManifest.java @@ -17,6 +17,7 @@ package com.google.android.exoplayer2.source.dash.manifest; import android.net.Uri; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.offline.FilterableManifest; import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; @@ -26,7 +27,7 @@ import java.util.List; * Represents a DASH media presentation description (mpd), as defined by ISO/IEC 23009-1:2014 * Section 5.3.1.2. */ -public class DashManifest { +public class DashManifest implements FilterableManifest { /** * The {@code availabilityStartTime} value in milliseconds since epoch, or {@link C#TIME_UNSET} if diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/FilteringDashManifestParser.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/FilteringDashManifestParser.java deleted file mode 100644 index 4e45a31183..0000000000 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/manifest/FilteringDashManifestParser.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.source.dash.manifest; - -import android.net.Uri; -import android.support.annotation.Nullable; -import com.google.android.exoplayer2.upstream.ParsingLoadable.Parser; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -/** - * A parser of media presentation description files which includes only the representations - * identified by the given keys. - */ -public final class FilteringDashManifestParser implements Parser { - - private final DashManifestParser dashManifestParser; - private final List filter; - - /** - * @param filter The representation keys that should be retained in the parsed manifests. If null, - * all representation are retained. - */ - public FilteringDashManifestParser(@Nullable List filter) { - this.dashManifestParser = new DashManifestParser(); - this.filter = filter; - } - - @Override - public DashManifest parse(Uri uri, InputStream inputStream) throws IOException { - DashManifest manifest = dashManifestParser.parse(uri, inputStream); - return filter != null ? manifest.copy(filter) : manifest; - } -} diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/FilteringHlsPlaylistParser.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/FilteringHlsPlaylistParser.java deleted file mode 100644 index bb888a7bff..0000000000 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/FilteringHlsPlaylistParser.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2018 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.source.hls.playlist; - -import android.net.Uri; -import android.support.annotation.Nullable; -import com.google.android.exoplayer2.upstream.ParsingLoadable.Parser; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -/** A HLS playlists parser which includes only the renditions identified by the given urls. */ -public final class FilteringHlsPlaylistParser implements Parser { - - private final HlsPlaylistParser hlsPlaylistParser; - private final List filter; - - /** - * @param filter The urls to renditions that should be retained in the parsed playlists. If null, - * all renditions are retained. - */ - public FilteringHlsPlaylistParser(@Nullable List filter) { - this.hlsPlaylistParser = new HlsPlaylistParser(); - this.filter = filter; - } - - @Override - public HlsPlaylist parse(Uri uri, InputStream inputStream) throws IOException { - HlsPlaylist hlsPlaylist = hlsPlaylistParser.parse(uri, inputStream); - if (hlsPlaylist instanceof HlsMasterPlaylist) { - HlsMasterPlaylist masterPlaylist = (HlsMasterPlaylist) hlsPlaylist; - return filter != null ? masterPlaylist.copy(filter) : masterPlaylist; - } else { - return hlsPlaylist; - } - } -} diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsMediaPlaylist.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsMediaPlaylist.java index 9a9517e2d4..e54caf47b4 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsMediaPlaylist.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsMediaPlaylist.java @@ -254,6 +254,11 @@ public final class HlsMediaPlaylist extends HlsPlaylist { : startOffsetUs >= 0 ? startOffsetUs : durationUs + startOffsetUs; } + @Override + public HlsMediaPlaylist copy(List renditionKeys) { + return this; + } + /** * Returns whether this playlist is newer than {@code other}. * diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylist.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylist.java index a490c9477c..34ecde229d 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylist.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylist.java @@ -15,13 +15,12 @@ */ package com.google.android.exoplayer2.source.hls.playlist; +import com.google.android.exoplayer2.offline.FilterableManifest; import java.util.Collections; import java.util.List; -/** - * Represents an HLS playlist. - */ -public abstract class HlsPlaylist { +/** Represents an HLS playlist. */ +public abstract class HlsPlaylist implements FilterableManifest { /** * The base uri. Used to resolve relative paths. diff --git a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/FilteringSsManifestParser.java b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/FilteringSsManifestParser.java deleted file mode 100644 index 3f88247ac9..0000000000 --- a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/FilteringSsManifestParser.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2017 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.google.android.exoplayer2.source.smoothstreaming.manifest; - -import android.net.Uri; -import android.support.annotation.Nullable; -import com.google.android.exoplayer2.upstream.ParsingLoadable.Parser; -import java.io.IOException; -import java.io.InputStream; -import java.util.List; - -/** - * A parser of SmoothStreaming manifest which includes only the tracks identified by the given keys. - */ -public final class FilteringSsManifestParser implements Parser { - - private final SsManifestParser ssManifestParser; - private final List filter; - - /** - * @param filter The track keys that should be retained in the parsed manifests. If null, all - * tracks are retained. - */ - public FilteringSsManifestParser(@Nullable List filter) { - this.ssManifestParser = new SsManifestParser(); - this.filter = filter; - } - - @Override - public SsManifest parse(Uri uri, InputStream inputStream) throws IOException { - SsManifest manifest = ssManifestParser.parse(uri, inputStream); - return filter != null ? manifest.copy(filter) : manifest; - } -} diff --git a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/SsManifest.java b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/SsManifest.java index 0df180a5a6..256014e112 100644 --- a/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/SsManifest.java +++ b/library/smoothstreaming/src/main/java/com/google/android/exoplayer2/source/smoothstreaming/manifest/SsManifest.java @@ -18,6 +18,7 @@ package com.google.android.exoplayer2.source.smoothstreaming.manifest; import android.net.Uri; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.Format; +import com.google.android.exoplayer2.offline.FilterableManifest; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.UriUtil; import com.google.android.exoplayer2.util.Util; @@ -30,10 +31,10 @@ import java.util.UUID; /** * Represents a SmoothStreaming manifest. * - * @see - * IIS Smooth Streaming Client Manifest Format + * @see IIS Smooth + * Streaming Client Manifest Format */ -public class SsManifest { +public class SsManifest implements FilterableManifest { public static final int UNSET_LOOKAHEAD = -1;