Subtitle renditions can be represented by Variant instances.

This will make it easier to use HlsChunkSource with WebVTT HLS
media playlists.
This commit is contained in:
Oliver Woodman 2015-11-25 17:07:40 +00:00
parent 0236831e48
commit 4bb8bea952
5 changed files with 28 additions and 63 deletions

View File

@ -32,6 +32,7 @@ import com.google.android.exoplayer.upstream.DataSource;
import com.google.android.exoplayer.upstream.DataSpec;
import com.google.android.exoplayer.upstream.HttpDataSource.InvalidResponseCodeException;
import com.google.android.exoplayer.util.Assertions;
import com.google.android.exoplayer.util.MimeTypes;
import com.google.android.exoplayer.util.UriUtil;
import com.google.android.exoplayer.util.Util;
@ -184,7 +185,9 @@ public class HlsChunkSource {
playlistParser = new HlsPlaylistParser();
if (playlist.type == HlsPlaylist.TYPE_MEDIA) {
variants = new Variant[] {new Variant(0, null, playlistUrl, 0, null, -1, -1)};
Format format = new Format("0", MimeTypes.APPLICATION_M3U8, -1, -1, -1, -1, -1, -1, null,
null);
variants = new Variant[] {new Variant(playlistUrl, format)};
variantPlaylists = new HlsMediaPlaylist[1];
variantLastPlaylistLoadTimesMs = new long[1];
variantBlacklistTimes = new long[1];

View File

@ -15,6 +15,7 @@
*/
package com.google.android.exoplayer.hls;
import java.util.Collections;
import java.util.List;
/**
@ -23,12 +24,12 @@ import java.util.List;
public final class HlsMasterPlaylist extends HlsPlaylist {
public final List<Variant> variants;
public final List<Subtitle> subtitles;
public final List<Variant> subtitles;
public HlsMasterPlaylist(String baseUri, List<Variant> variants, List<Subtitle> subtitles) {
public HlsMasterPlaylist(String baseUri, List<Variant> variants, List<Variant> subtitles) {
super(baseUri, HlsPlaylist.TYPE_MASTER);
this.variants = variants;
this.subtitles = subtitles;
this.variants = Collections.unmodifiableList(variants);
this.subtitles = Collections.unmodifiableList(subtitles);
}
}

View File

@ -17,8 +17,10 @@ package com.google.android.exoplayer.hls;
import com.google.android.exoplayer.C;
import com.google.android.exoplayer.ParserException;
import com.google.android.exoplayer.chunk.Format;
import com.google.android.exoplayer.hls.HlsMediaPlaylist.Segment;
import com.google.android.exoplayer.upstream.UriLoadable;
import com.google.android.exoplayer.util.MimeTypes;
import java.io.BufferedReader;
import java.io.IOException;
@ -53,8 +55,6 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
private static final String RESOLUTION_ATTR = "RESOLUTION";
private static final String LANGUAGE_ATTR = "LANGUAGE";
private static final String NAME_ATTR = "NAME";
private static final String AUTOSELECT_ATTR = "AUTOSELECT";
private static final String DEFAULT_ATTR = "DEFAULT";
private static final String TYPE_ATTR = "TYPE";
private static final String METHOD_ATTR = "METHOD";
private static final String URI_ATTR = "URI";
@ -98,10 +98,10 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
Pattern.compile(LANGUAGE_ATTR + "=\"(.+?)\"");
private static final Pattern NAME_ATTR_REGEX =
Pattern.compile(NAME_ATTR + "=\"(.+?)\"");
private static final Pattern AUTOSELECT_ATTR_REGEX =
HlsParserUtil.compileBooleanAttrPattern(AUTOSELECT_ATTR);
private static final Pattern DEFAULT_ATTR_REGEX =
HlsParserUtil.compileBooleanAttrPattern(DEFAULT_ATTR);
// private static final Pattern AUTOSELECT_ATTR_REGEX =
// HlsParserUtil.compileBooleanAttrPattern(AUTOSELECT_ATTR);
// private static final Pattern DEFAULT_ATTR_REGEX =
// HlsParserUtil.compileBooleanAttrPattern(DEFAULT_ATTR);
@Override
public HlsPlaylist parse(String connectionUrl, InputStream inputStream)
@ -140,7 +140,7 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
private static HlsMasterPlaylist parseMasterPlaylist(LineIterator iterator, String baseUri)
throws IOException {
ArrayList<Variant> variants = new ArrayList<>();
ArrayList<Subtitle> subtitles = new ArrayList<>();
ArrayList<Variant> subtitles = new ArrayList<>();
int bitrate = 0;
String codecs = null;
int width = -1;
@ -158,9 +158,9 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
String subtitleName = HlsParserUtil.parseStringAttr(line, NAME_ATTR_REGEX, NAME_ATTR);
String uri = HlsParserUtil.parseStringAttr(line, URI_ATTR_REGEX, URI_ATTR);
String language = HlsParserUtil.parseOptionalStringAttr(line, LANGUAGE_ATTR_REGEX);
boolean isDefault = HlsParserUtil.parseOptionalBooleanAttr(line, DEFAULT_ATTR_REGEX);
boolean autoSelect = HlsParserUtil.parseOptionalBooleanAttr(line, AUTOSELECT_ATTR_REGEX);
subtitles.add(new Subtitle(subtitleName, uri, language, isDefault, autoSelect));
Format format = new Format(subtitleName, MimeTypes.TEXT_VTT, -1, -1, -1, -1, -1, -1,
language, codecs);
subtitles.add(new Variant(uri, format));
} else {
// TODO: Support other types of media tag.
}
@ -188,7 +188,12 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
}
expectingStreamInfUrl = true;
} else if (!line.startsWith("#") && expectingStreamInfUrl) {
variants.add(new Variant(variants.size(), name, line, bitrate, codecs, width, height));
if (name == null) {
name = Integer.toString(variants.size());
}
Format format = new Format(name, MimeTypes.APPLICATION_M3U8, width, height, -1, -1, -1,
bitrate, null, codecs);
variants.add(new Variant(line, format));
bitrate = 0;
codecs = null;
name = null;
@ -197,8 +202,7 @@ public final class HlsPlaylistParser implements UriLoadable.Parser<HlsPlaylist>
expectingStreamInfUrl = false;
}
}
return new HlsMasterPlaylist(baseUri, Collections.unmodifiableList(variants),
Collections.unmodifiableList(subtitles));
return new HlsMasterPlaylist(baseUri, variants, subtitles);
}
private static HlsMediaPlaylist parseMediaPlaylist(LineIterator iterator, String baseUri)

View File

@ -1,37 +0,0 @@
/*
* Copyright (C) 2014 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.exoplayer.hls;
/**
* Subtitle media tag.
*/
public final class Subtitle {
public final String name;
public final String uri;
public final String language;
public final boolean isDefault;
public final boolean autoSelect;
public Subtitle(String name, String uri, String language, boolean isDefault, boolean autoSelect) {
this.name = name;
this.uri = uri;
this.language = language;
this.autoSelect = autoSelect;
this.isDefault = isDefault;
}
}

View File

@ -17,7 +17,6 @@ package com.google.android.exoplayer.hls;
import com.google.android.exoplayer.chunk.Format;
import com.google.android.exoplayer.chunk.FormatWrapper;
import com.google.android.exoplayer.util.MimeTypes;
/**
* Variant stream reference.
@ -26,15 +25,10 @@ public final class Variant implements FormatWrapper {
public final String url;
public final Format format;
public final String name;
public Variant(int index, String name, String url, int bitrate, String codecs, int width,
int height) {
public Variant(String url, Format format) {
this.url = url;
this.name = name;
String formatName = name != null ? name : Integer.toString(index);
format = new Format(formatName, MimeTypes.APPLICATION_M3U8, width, height, -1, -1,
-1, bitrate, null, codecs);
this.format = format;
}
@Override