diff --git a/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsParserUtil.java b/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsParserUtil.java deleted file mode 100644 index 9b74865895..0000000000 --- a/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsParserUtil.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * Copyright (C) 2016 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 com.google.android.exoplayer2.ParserException; - -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * Utility methods for HLS manifest parsing. - */ -/* package */ final class HlsParserUtil { - - private static final String BOOLEAN_TRUE_VALUE = "YES"; - private static final String BOOLEAN_FALSE_VALUE = "NO"; - - private HlsParserUtil() {} - - public static String parseStringAttr(String line, Pattern pattern, String tag) - throws ParserException { - Matcher matcher = pattern.matcher(line); - if (matcher.find() && matcher.groupCount() == 1) { - return matcher.group(1); - } - throw new ParserException("Couldn't match " + tag + " tag in " + line); - } - - public static int parseIntAttr(String line, Pattern pattern, String tag) - throws ParserException { - return Integer.parseInt(parseStringAttr(line, pattern, tag)); - } - - public static double parseDoubleAttr(String line, Pattern pattern, String tag) - throws ParserException { - return Double.parseDouble(parseStringAttr(line, pattern, tag)); - } - - public static String parseOptionalStringAttr(String line, Pattern pattern) { - Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - return matcher.group(1); - } - return null; - } - - public static boolean parseBooleanAttribute(String line, Pattern pattern, boolean defaultValue) { - Matcher matcher = pattern.matcher(line); - if (matcher.find()) { - return matcher.group(1).equals(BOOLEAN_TRUE_VALUE); - } - return defaultValue; - } - - public static Pattern compileBooleanAttrPattern(String attribute) { - return Pattern.compile(attribute + "=(" + BOOLEAN_FALSE_VALUE + "|" + BOOLEAN_TRUE_VALUE + ")"); - - } - -} diff --git a/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylistParser.java b/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylistParser.java index 16a8faf7d0..854488002b 100644 --- a/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylistParser.java +++ b/library/src/main/java/com/google/android/exoplayer2/source/hls/playlist/HlsPlaylistParser.java @@ -33,6 +33,7 @@ import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Queue; +import java.util.regex.Matcher; import java.util.regex.Pattern; /** @@ -40,78 +41,53 @@ import java.util.regex.Pattern; */ public final class HlsPlaylistParser implements ParsingLoadable.Parser { - private static final String VERSION_TAG = "#EXT-X-VERSION"; - private static final String STREAM_INF_TAG = "#EXT-X-STREAM-INF"; - private static final String MEDIA_TAG = "#EXT-X-MEDIA"; - private static final String DISCONTINUITY_TAG = "#EXT-X-DISCONTINUITY"; - private static final String DISCONTINUITY_SEQUENCE_TAG = "#EXT-X-DISCONTINUITY-SEQUENCE"; - private static final String MEDIA_DURATION_TAG = "#EXTINF"; - private static final String MEDIA_SEQUENCE_TAG = "#EXT-X-MEDIA-SEQUENCE"; - private static final String TARGET_DURATION_TAG = "#EXT-X-TARGETDURATION"; - private static final String ENDLIST_TAG = "#EXT-X-ENDLIST"; - private static final String KEY_TAG = "#EXT-X-KEY"; - private static final String BYTERANGE_TAG = "#EXT-X-BYTERANGE"; + private static final String TAG_VERSION = "#EXT-X-VERSION"; + private static final String TAG_STREAM_INF = "#EXT-X-STREAM-INF"; + private static final String TAG_MEDIA = "#EXT-X-MEDIA"; + private static final String TAG_DISCONTINUITY = "#EXT-X-DISCONTINUITY"; + private static final String TAG_DISCONTINUITY_SEQUENCE = "#EXT-X-DISCONTINUITY-SEQUENCE"; + private static final String TAG_MEDIA_DURATION = "#EXTINF"; + private static final String TAG_MEDIA_SEQUENCE = "#EXT-X-MEDIA-SEQUENCE"; + private static final String TAG_TARGET_DURATION = "#EXT-X-TARGETDURATION"; + private static final String TAG_ENDLIST = "#EXT-X-ENDLIST"; + private static final String TAG_KEY = "#EXT-X-KEY"; + private static final String TAG_BYTERANGE = "#EXT-X-BYTERANGE"; - private static final String BANDWIDTH_ATTR = "BANDWIDTH"; - private static final String CODECS_ATTR = "CODECS"; - 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 TYPE_ATTR = "TYPE"; - private static final String METHOD_ATTR = "METHOD"; - private static final String URI_ATTR = "URI"; - private static final String IV_ATTR = "IV"; - private static final String INSTREAM_ID_ATTR = "INSTREAM-ID"; - private static final String AUTOSELECT_ATTR = "AUTOSELECT"; - private static final String DEFAULT_ATTR = "DEFAULT"; - private static final String FORCED_ATTR = "FORCED"; - - private static final String AUDIO_TYPE = "AUDIO"; - private static final String VIDEO_TYPE = "VIDEO"; - private static final String SUBTITLES_TYPE = "SUBTITLES"; - private static final String CLOSED_CAPTIONS_TYPE = "CLOSED-CAPTIONS"; + private static final String TYPE_AUDIO = "AUDIO"; + private static final String TYPE_VIDEO = "VIDEO"; + private static final String TYPE_SUBTITLES = "SUBTITLES"; + private static final String TYPE_CLOSED_CAPTIONS = "CLOSED-CAPTIONS"; private static final String METHOD_NONE = "NONE"; private static final String METHOD_AES128 = "AES-128"; - private static final Pattern BANDWIDTH_ATTR_REGEX = - Pattern.compile(BANDWIDTH_ATTR + "=(\\d+)\\b"); - private static final Pattern CODECS_ATTR_REGEX = - Pattern.compile(CODECS_ATTR + "=\"(.+?)\""); - private static final Pattern RESOLUTION_ATTR_REGEX = - Pattern.compile(RESOLUTION_ATTR + "=(\\d+x\\d+)"); - private static final Pattern MEDIA_DURATION_REGEX = - Pattern.compile(MEDIA_DURATION_TAG + ":([\\d.]+)\\b"); - private static final Pattern MEDIA_SEQUENCE_REGEX = - Pattern.compile(MEDIA_SEQUENCE_TAG + ":(\\d+)\\b"); - private static final Pattern TARGET_DURATION_REGEX = - Pattern.compile(TARGET_DURATION_TAG + ":(\\d+)\\b"); - private static final Pattern VERSION_REGEX = - Pattern.compile(VERSION_TAG + ":(\\d+)\\b"); - private static final Pattern BYTERANGE_REGEX = - Pattern.compile(BYTERANGE_TAG + ":(\\d+(?:@\\d+)?)\\b"); + private static final String BOOLEAN_TRUE = "YES"; + private static final String BOOLEAN_FALSE = "NO"; - private static final Pattern METHOD_ATTR_REGEX = - Pattern.compile(METHOD_ATTR + "=(" + METHOD_NONE + "|" + METHOD_AES128 + ")"); - private static final Pattern URI_ATTR_REGEX = - Pattern.compile(URI_ATTR + "=\"(.+?)\""); - private static final Pattern IV_ATTR_REGEX = - Pattern.compile(IV_ATTR + "=([^,.*]+)"); - private static final Pattern TYPE_ATTR_REGEX = - Pattern.compile(TYPE_ATTR + "=(" + AUDIO_TYPE + "|" + VIDEO_TYPE + "|" + SUBTITLES_TYPE + "|" - + CLOSED_CAPTIONS_TYPE + ")"); - private static final Pattern LANGUAGE_ATTR_REGEX = - Pattern.compile(LANGUAGE_ATTR + "=\"(.+?)\""); - private static final Pattern NAME_ATTR_REGEX = - Pattern.compile(NAME_ATTR + "=\"(.+?)\""); - private static final Pattern INSTREAM_ID_ATTR_REGEX = - Pattern.compile(INSTREAM_ID_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 FORCED_ATTR_REGEX = - HlsParserUtil.compileBooleanAttrPattern(FORCED_ATTR); + private static final Pattern REGEX_BANDWIDTH = Pattern.compile("BANDWIDTH=(\\d+)\\b"); + private static final Pattern REGEX_CODECS = Pattern.compile("CODECS=\"(.+?)\""); + private static final Pattern REGEX_RESOLUTION = Pattern.compile("RESOLUTION=(\\d+x\\d+)"); + private static final Pattern REGEX_VERSION = Pattern.compile(TAG_VERSION + ":(\\d+)\\b"); + private static final Pattern REGEX_TARGET_DURATION = Pattern.compile(TAG_TARGET_DURATION + + ":(\\d+)\\b"); + private static final Pattern REGEX_MEDIA_SEQUENCE = Pattern.compile(TAG_MEDIA_SEQUENCE + + ":(\\d+)\\b"); + private static final Pattern REGEX_MEDIA_DURATION = Pattern.compile(TAG_MEDIA_DURATION + + ":([\\d\\.]+)\\b"); + private static final Pattern REGEX_BYTERANGE = Pattern.compile(TAG_BYTERANGE + + ":(\\d+(?:@\\d+)?)\\b"); + private static final Pattern REGEX_METHOD = Pattern.compile("METHOD=(" + METHOD_NONE + "|" + + METHOD_AES128 + ")"); + private static final Pattern REGEX_URI = Pattern.compile("URI=\"(.+?)\""); + private static final Pattern REGEX_IV = Pattern.compile("IV=([^,.*]+)"); + private static final Pattern REGEX_TYPE = Pattern.compile("TYPE=(" + TYPE_AUDIO + "|" + TYPE_VIDEO + + "|" + TYPE_SUBTITLES + "|" + TYPE_CLOSED_CAPTIONS + ")"); + private static final Pattern REGEX_LANGUAGE = Pattern.compile("LANGUAGE=\"(.+?)\""); + private static final Pattern REGEX_NAME = Pattern.compile("NAME=\"(.+?)\""); + private static final Pattern REGEX_INSTREAM_ID = Pattern.compile("INSTREAM-ID=\"(.+?)\""); + private static final Pattern REGEX_AUTOSELECT = compileBooleanAttrPattern("AUTOSELECT"); + private static final Pattern REGEX_DEFAULT = compileBooleanAttrPattern("DEFAULT"); + private static final Pattern REGEX_FORCED = compileBooleanAttrPattern("FORCED"); @Override public HlsPlaylist parse(Uri uri, InputStream inputStream) throws IOException { @@ -123,17 +99,17 @@ public final class HlsPlaylistParser implements ParsingLoadable.Parser 1) { segmentByterangeOffset = Long.parseLong(splitByteRange[1]); } - } else if (line.startsWith(DISCONTINUITY_SEQUENCE_TAG)) { + } else if (line.startsWith(TAG_DISCONTINUITY_SEQUENCE)) { discontinuitySequenceNumber = Integer.parseInt(line.substring(line.indexOf(':') + 1)); - } else if (line.equals(DISCONTINUITY_TAG)) { + } else if (line.equals(TAG_DISCONTINUITY)) { discontinuitySequenceNumber++; } else if (!line.startsWith("#")) { String segmentEncryptionIV; @@ -323,7 +295,7 @@ public final class HlsPlaylistParser implements ParsingLoadable.Parser