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
This commit is contained in:
olly 2018-04-25 03:54:13 -07:00 committed by Oliver Woodman
parent b77d6c4ef4
commit 0d9d1998f4
10 changed files with 107 additions and 159 deletions

View file

@ -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<RepresentationKey>) manifestFilter))
new FilteringManifestParser<>(
new DashManifestParser(), (List<RepresentationKey>) manifestFilter))
.createMediaSource(uri);
case C.TYPE_SS:
return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory),
buildDataSourceFactory(false))
.setManifestParser(new FilteringSsManifestParser((List<TrackKey>) manifestFilter))
.setManifestParser(
new FilteringManifestParser<>(
new SsManifestParser(), (List<TrackKey>) manifestFilter))
.createMediaSource(uri);
case C.TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory)
.setPlaylistParser(new FilteringHlsPlaylistParser((List<RenditionKey>) manifestFilter))
.setPlaylistParser(
new FilteringManifestParser<>(
new HlsPlaylistParser(), (List<RenditionKey>) manifestFilter))
.createMediaSource(uri);
case C.TYPE_OTHER:
return new ExtractorMediaSource.Factory(mediaDataSourceFactory).createMediaSource(uri);

View file

@ -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 <T> The manifest type.
* @param <K> The track key type.
*/
public interface FilterableManifest<T, K> {
/**
* 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<K> trackKeys);
}

View file

@ -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<T extends FilterableManifest<T, K>, K>
implements Parser<T> {
private final Parser<T> parser;
private final List<K> 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<T> parser, List<K> 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);
}
}

View file

@ -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<DashManifest, RepresentationKey> {
/**
* The {@code availabilityStartTime} value in milliseconds since epoch, or {@link C#TIME_UNSET} if

View file

@ -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<DashManifest> {
private final DashManifestParser dashManifestParser;
private final List<RepresentationKey> filter;
/**
* @param filter The representation keys that should be retained in the parsed manifests. If null,
* all representation are retained.
*/
public FilteringDashManifestParser(@Nullable List<RepresentationKey> 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;
}
}

View file

@ -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<HlsPlaylist> {
private final HlsPlaylistParser hlsPlaylistParser;
private final List<RenditionKey> 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<RenditionKey> 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;
}
}
}

View file

@ -254,6 +254,11 @@ public final class HlsMediaPlaylist extends HlsPlaylist {
: startOffsetUs >= 0 ? startOffsetUs : durationUs + startOffsetUs;
}
@Override
public HlsMediaPlaylist copy(List<RenditionKey> renditionKeys) {
return this;
}
/**
* Returns whether this playlist is newer than {@code other}.
*

View file

@ -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<HlsPlaylist, RenditionKey> {
/**
* The base uri. Used to resolve relative paths.

View file

@ -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<SsManifest> {
private final SsManifestParser ssManifestParser;
private final List<TrackKey> filter;
/**
* @param filter The track keys that should be retained in the parsed manifests. If null, all
* tracks are retained.
*/
public FilteringSsManifestParser(@Nullable List<TrackKey> 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;
}
}

View file

@ -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 <a href="http://msdn.microsoft.com/en-us/library/ee673436(v=vs.90).aspx">
* IIS Smooth Streaming Client Manifest Format</a>
* @see <a href="http://msdn.microsoft.com/en-us/library/ee673436(v=vs.90).aspx">IIS Smooth
* Streaming Client Manifest Format</a>
*/
public class SsManifest {
public class SsManifest implements FilterableManifest<SsManifest, TrackKey> {
public static final int UNSET_LOOKAHEAD = -1;