Commit 7959a691 by aquilescanta Committed by Oliver Woodman

Add sniffing to WebvttExtractor

Preparation for sniffing in HLS

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=211455454
parent 70f8aeb1
......@@ -27,6 +27,11 @@ public class SubtitleDecoderException extends Exception {
super(message);
}
/** @param cause The cause of this exception. */
public SubtitleDecoderException(Exception cause) {
super(cause);
}
/**
* @param message The detail message for this exception.
* @param cause The cause of this exception.
......
......@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.text.webvtt;
import android.text.TextUtils;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.text.SimpleSubtitleDecoder;
import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.util.ParsableByteArray;
......@@ -62,7 +63,11 @@ public final class WebvttDecoder extends SimpleSubtitleDecoder {
definedStyles.clear();
// Validate the first line of the header, and skip the remainder.
try {
WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
} catch (ParserException e) {
throw new SubtitleDecoderException(e);
}
while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {}
int event;
......
......@@ -15,7 +15,7 @@
*/
package com.google.android.exoplayer2.text.webvtt;
import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.util.ParsableByteArray;
import com.google.android.exoplayer2.util.Util;
import java.util.regex.Matcher;
......@@ -27,7 +27,9 @@ import java.util.regex.Pattern;
public final class WebvttParserUtil {
private static final Pattern COMMENT = Pattern.compile("^NOTE((\u0020|\u0009).*)?$");
private static final Pattern HEADER = Pattern.compile("^\uFEFF?WEBVTT((\u0020|\u0009).*)?$");
private static final String WEBVTT_HEADER = "WEBVTT";
private static final int WEBVTT_BOM_BE = 0xfeff;
private static final int WEBVTT_BOM_LE = 0xfffe;
private WebvttParserUtil() {}
......@@ -35,14 +37,33 @@ public final class WebvttParserUtil {
* Reads and validates the first line of a WebVTT file.
*
* @param input The input from which the line should be read.
* @throws SubtitleDecoderException If the line isn't the start of a valid WebVTT file.
* @throws ParserException If the line isn't the start of a valid WebVTT file.
*/
public static void validateWebvttHeaderLine(ParsableByteArray input)
throws SubtitleDecoderException {
String line = input.readLine();
if (line == null || !HEADER.matcher(line).matches()) {
throw new SubtitleDecoderException("Expected WEBVTT. Got " + line);
public static void validateWebvttHeaderLine(ParsableByteArray input) throws ParserException {
int startPosition = input.getPosition();
if (!isWebvttHeaderLine(input)) {
input.setPosition(startPosition);
throw new ParserException("Expected WEBVTT. Got " + input.readLine());
}
}
/**
* Returns whether the given input is the first line of a WebVTT file.
*
* @param input The input from which the line should be read.
*/
public static boolean isWebvttHeaderLine(ParsableByteArray input) {
if (input.bytesLeft() < 2) {
return false;
}
int startPosition = input.getPosition();
int firstTwoBytes = input.readUnsignedShort();
if (firstTwoBytes != WEBVTT_BOM_BE && firstTwoBytes != WEBVTT_BOM_LE) {
// Not the BOM, should not be discarded.
input.setPosition(startPosition);
}
String line = input.readLine();
return line != null && line.startsWith(WEBVTT_HEADER);
}
/**
......
......@@ -499,10 +499,10 @@ public final class ParsableByteArray {
/**
* Reads a line of text.
* <p>
* A line is considered to be terminated by any one of a carriage return ('\r'), a line feed
*
* <p>A line is considered to be terminated by any one of a carriage return ('\r'), a line feed
* ('\n'), or a carriage return followed immediately by a line feed ('\r\n'). The system's default
* charset (UTF-8) is used.
* charset (UTF-8) is used. This method discards leading UTF-8 byte order marks, if present.
*
* @return The line not including any line-termination characters, or null if the end of the data
* has already been reached.
......@@ -517,7 +517,7 @@ public final class ParsableByteArray {
}
if (lineLimit - position >= 3 && data[position] == (byte) 0xEF
&& data[position + 1] == (byte) 0xBB && data[position + 2] == (byte) 0xBF) {
// There's a byte order mark at the start of the line. Discard it.
// There's a UTF-8 byte order mark at the start of the line. Discard it.
position += 3;
}
String line = Util.fromUtf8Bytes(data, position, lineLimit - position);
......
No preview for this file type
......@@ -49,6 +49,7 @@ public class WebvttDecoderTest {
private static final String WITH_TAGS_FILE = "webvtt/with_tags";
private static final String WITH_CSS_STYLES = "webvtt/with_css_styles";
private static final String WITH_CSS_COMPLEX_SELECTORS = "webvtt/with_css_complex_selectors";
private static final String WITH_BOM = "webvtt/with_bom";
private static final String EMPTY_FILE = "webvtt/empty";
@Test
......@@ -56,7 +57,7 @@ public class WebvttDecoderTest {
WebvttDecoder decoder = new WebvttDecoder();
byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, EMPTY_FILE);
try {
decoder.decode(bytes, bytes.length, false);
decoder.decode(bytes, bytes.length, /* reset= */ false);
fail();
} catch (SubtitleDecoderException expected) {
// Do nothing.
......@@ -71,8 +72,40 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
}
@Test
public void testDecodeWithBom() throws IOException, SubtitleDecoderException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_BOM);
// Test event count.
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues.
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
}
@Test
......@@ -83,8 +116,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
}
@Test
......@@ -95,8 +138,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
}
@Test
......@@ -107,8 +160,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// test cues
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
}
@Test
......@@ -119,10 +182,30 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(8);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
assertCue(subtitle, 4, 4000000, 5000000, "This is the third subtitle.");
assertCue(subtitle, 6, 6000000, 7000000, "This is the <fourth> &subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 4,
/* startTimeUs= */ 4000000,
/* endTimeUs= */ 5000000,
"This is the third subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 6,
/* startTimeUs= */ 6000000,
/* endTimeUs= */ 7000000,
"This is the <fourth> &subtitle.");
}
@Test
......@@ -133,82 +216,82 @@ public class WebvttDecoderTest {
// Test cues.
assertCue(
subtitle,
0,
0,
1234000,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.",
Alignment.ALIGN_NORMAL,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.TYPE_UNSET,
0.1f,
Cue.ANCHOR_TYPE_START,
0.35f);
/* line= */ Cue.DIMEN_UNSET,
/* lineType= */ Cue.TYPE_UNSET,
/* lineAnchor= */ Cue.TYPE_UNSET,
/* position= */ 0.1f,
/* positionAnchor= */ Cue.ANCHOR_TYPE_START,
/* size= */ 0.35f);
assertCue(
subtitle,
2,
2345000,
3456000,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.",
Alignment.ALIGN_OPPOSITE,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.TYPE_UNSET,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
0.35f);
/* line= */ Cue.DIMEN_UNSET,
/* lineType= */ Cue.TYPE_UNSET,
/* lineAnchor= */ Cue.TYPE_UNSET,
/* position= */ Cue.DIMEN_UNSET,
/* positionAnchor= */ Cue.TYPE_UNSET,
/* size= */ 0.35f);
assertCue(
subtitle,
4,
4000000,
5000000,
/* eventTimeIndex= */ 4,
/* startTimeUs= */ 4000000,
/* endTimeUs= */ 5000000,
"This is the third subtitle.",
Alignment.ALIGN_CENTER,
0.45f,
Cue.LINE_TYPE_FRACTION,
Cue.ANCHOR_TYPE_END,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
0.35f);
/* line= */ 0.45f,
/* lineType= */ Cue.LINE_TYPE_FRACTION,
/* lineAnchor= */ Cue.ANCHOR_TYPE_END,
/* position= */ Cue.DIMEN_UNSET,
/* positionAnchor= */ Cue.TYPE_UNSET,
/* size= */ 0.35f);
assertCue(
subtitle,
6,
6000000,
7000000,
/* eventTimeIndex= */ 6,
/* startTimeUs= */ 6000000,
/* endTimeUs= */ 7000000,
"This is the fourth subtitle.",
Alignment.ALIGN_CENTER,
-11f,
Cue.LINE_TYPE_NUMBER,
Cue.TYPE_UNSET,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.DIMEN_UNSET);
/* line= */ -11f,
/* lineType= */ Cue.LINE_TYPE_NUMBER,
/* lineAnchor= */ Cue.TYPE_UNSET,
/* position= */ Cue.DIMEN_UNSET,
/* positionAnchor= */ Cue.TYPE_UNSET,
/* size= */ Cue.DIMEN_UNSET);
assertCue(
subtitle,
8,
7000000,
8000000,
/* eventTimeIndex= */ 8,
/* startTimeUs= */ 7000000,
/* endTimeUs= */ 8000000,
"This is the fifth subtitle.",
Alignment.ALIGN_OPPOSITE,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.TYPE_UNSET,
0.1f,
Cue.ANCHOR_TYPE_END,
0.1f);
/* line= */ Cue.DIMEN_UNSET,
/* lineType= */ Cue.TYPE_UNSET,
/* lineAnchor= */ Cue.TYPE_UNSET,
/* position= */ 0.1f,
/* positionAnchor= */ Cue.ANCHOR_TYPE_END,
/* size= */ 0.1f);
assertCue(
subtitle,
10,
10000000,
11000000,
/* eventTimeIndex= */ 10,
/* startTimeUs= */ 10000000,
/* endTimeUs= */ 11000000,
"This is the sixth subtitle.",
Alignment.ALIGN_CENTER,
0.45f,
Cue.LINE_TYPE_FRACTION,
Cue.ANCHOR_TYPE_END,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
0.35f);
/* line= */ 0.45f,
/* lineType= */ Cue.LINE_TYPE_FRACTION,
/* lineAnchor= */ Cue.ANCHOR_TYPE_END,
/* position= */ Cue.DIMEN_UNSET,
/* positionAnchor= */ Cue.TYPE_UNSET,
/* size= */ 0.35f);
}
@Test
......@@ -219,8 +302,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 4000000, 5000000, "This is the third subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 4000000,
/* endTimeUs= */ 5000000,
"This is the third subtitle.");
}
@Test
......@@ -231,57 +324,70 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(8);
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
Spanned s1 = getUniqueSpanTextAt(subtitle, 0);
Spanned s2 = getUniqueSpanTextAt(subtitle, 2345000);
Spanned s3 = getUniqueSpanTextAt(subtitle, 20000000);
Spanned s4 = getUniqueSpanTextAt(subtitle, 25000000);
assertThat(s1.getSpans(0, s1.length(), ForegroundColorSpan.class)).hasLength(1);
assertThat(s1.getSpans(0, s1.length(), BackgroundColorSpan.class)).hasLength(1);
assertThat(s2.getSpans(0, s2.length(), ForegroundColorSpan.class)).hasLength(2);
assertThat(s3.getSpans(10, s3.length(), UnderlineSpan.class)).hasLength(1);
assertThat(s4.getSpans(0, 16, BackgroundColorSpan.class)).hasLength(2);
assertThat(s4.getSpans(17, s4.length(), StyleSpan.class)).hasLength(1);
assertThat(s4.getSpans(17, s4.length(), StyleSpan.class)[0].getStyle())
assertCue(
subtitle,
/* eventTimeIndex= */ 0,
/* startTimeUs= */ 0,
/* endTimeUs= */ 1234000,
"This is the first subtitle.");
assertCue(
subtitle,
/* eventTimeIndex= */ 2,
/* startTimeUs= */ 2345000,
/* endTimeUs= */ 3456000,
"This is the second subtitle.");
Spanned s1 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0);
Spanned s2 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2345000);
Spanned s3 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 20000000);
Spanned s4 = getUniqueSpanTextAt(subtitle, /* timeUs= */ 25000000);
assertThat(s1.getSpans(/* start= */ 0, s1.length(), ForegroundColorSpan.class)).hasLength(1);
assertThat(s1.getSpans(/* start= */ 0, s1.length(), BackgroundColorSpan.class)).hasLength(1);
assertThat(s2.getSpans(/* start= */ 0, s2.length(), ForegroundColorSpan.class)).hasLength(2);
assertThat(s3.getSpans(/* start= */ 10, s3.length(), UnderlineSpan.class)).hasLength(1);
assertThat(s4.getSpans(/* start= */ 0, /* end= */ 16, BackgroundColorSpan.class)).hasLength(2);
assertThat(s4.getSpans(/* start= */ 17, s4.length(), StyleSpan.class)).hasLength(1);
assertThat(s4.getSpans(/* start= */ 17, s4.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD);
}
@Test
public void testWithComplexCssSelectors() throws IOException, SubtitleDecoderException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS);
Spanned text = getUniqueSpanTextAt(subtitle, 0);
assertThat(text.getSpans(30, text.length(), ForegroundColorSpan.class)).hasLength(1);
assertThat(text.getSpans(30, text.length(), ForegroundColorSpan.class)[0].getForegroundColor())
Spanned text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0);
assertThat(text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class))
.hasLength(1);
assertThat(
text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class)[0]
.getForegroundColor())
.isEqualTo(0xFFEE82EE);
assertThat(text.getSpans(30, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(30, text.length(), TypefaceSpan.class)[0].getFamily())
assertThat(text.getSpans(/* start= */ 30, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 30, text.length(), TypefaceSpan.class)[0].getFamily())
.isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 2000000);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)[0].getFamily())
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2000000);
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)[0].getFamily())
.isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 2500000);
assertThat(text.getSpans(5, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(5, text.length(), StyleSpan.class)[0].getStyle())
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2500000);
assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)[0].getFamily())
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 5, text.length(), TypefaceSpan.class)[0].getFamily())
.isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 4000000);
assertThat(text.getSpans(6, 22, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(30, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(30, text.length(), StyleSpan.class)[0].getStyle())
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 4000000);
assertThat(text.getSpans(/* start= */ 6, /* end= */ 22, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD);
text = getUniqueSpanTextAt(subtitle, 5000000);
assertThat(text.getSpans(9, 17, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(19, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(19, text.length(), StyleSpan.class)[0].getStyle())
text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 5000000);
assertThat(text.getSpans(/* start= */ 9, /* end= */ 17, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.ITALIC);
}
......@@ -289,7 +395,7 @@ public class WebvttDecoderTest {
throws IOException, SubtitleDecoderException {
WebvttDecoder decoder = new WebvttDecoder();
byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, asset);
return decoder.decode(bytes, bytes.length, false);
return decoder.decode(bytes, bytes.length, /* reset= */ false);
}
private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) {
......@@ -304,13 +410,13 @@ public class WebvttDecoderTest {
startTimeUs,
endTimeUs,
text,
null,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.TYPE_UNSET,
Cue.DIMEN_UNSET,
Cue.TYPE_UNSET,
Cue.DIMEN_UNSET);
/* textAlignment= */ null,
/* line= */ Cue.DIMEN_UNSET,
/* lineType= */ Cue.TYPE_UNSET,
/* lineAnchor= */ Cue.TYPE_UNSET,
/* position= */ Cue.DIMEN_UNSET,
/* positionAnchor= */ Cue.TYPE_UNSET,
/* size= */ Cue.DIMEN_UNSET);
}
private static void assertCue(
......
......@@ -25,7 +25,6 @@ import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.text.webvtt.WebvttParserUtil;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray;
......@@ -47,6 +46,7 @@ public final class WebvttExtractor implements Extractor {
private static final Pattern LOCAL_TIMESTAMP = Pattern.compile("LOCAL:([^,]+)");
private static final Pattern MEDIA_TIMESTAMP = Pattern.compile("MPEGTS:(\\d+)");
private static final int HEADER_MAX_LENGTH = 2 /* optional Byte Order Mark */ + 6 /* "WEBVTT" */;
private final String language;
private final TimestampAdjuster timestampAdjuster;
......@@ -68,8 +68,10 @@ public final class WebvttExtractor implements Extractor {
@Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
// This extractor is only used for the HLS use case, which should not call this method.
throw new IllegalStateException();
input.peekFully(
sampleData, /* offset= */ 0, /* length= */ HEADER_MAX_LENGTH, /* allowEndOfInput= */ false);
sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
}
@Override
......@@ -118,11 +120,7 @@ public final class WebvttExtractor implements Extractor {
ParsableByteArray webvttData = new ParsableByteArray(sampleData);
// Validate the first line of the header.
try {
WebvttParserUtil.validateWebvttHeaderLine(webvttData);
} catch (SubtitleDecoderException e) {
throw new ParserException(e);
}
// Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
long vttTimestampUs = 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;
import static com.google.common.truth.Truth.assertThat;
import com.google.android.exoplayer2.extractor.ExtractorInput;
import com.google.android.exoplayer2.testutil.FakeExtractorInput;
import com.google.android.exoplayer2.util.TimestampAdjuster;
import java.io.IOException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
/** Tests for {@link WebvttExtractor}. */
@RunWith(RobolectricTestRunner.class)
public class WebvttExtractorTest {
@Test
public void sniff_sniffsWebvttHeaderWithTrailingSpace() throws IOException, InterruptedException {
byte[] data = new byte[] {'W', 'E', 'B', 'V', 'T', 'T', ' ', '\t'};
assertThat(sniffData(data)).isTrue();
}
@Test
public void sniff_discardsByteOrderMark() throws IOException, InterruptedException {
byte[] data = new byte[] {(byte) 0xFE, (byte) 0xFF, 'W', 'E', 'B', 'V', 'T', 'T', '\n', ' '};
assertThat(sniffData(data)).isTrue();
}
@Test
public void sniff_failsForIncorrectBom() throws IOException, InterruptedException {
byte[] data = new byte[] {(byte) 0xFE, (byte) 0xFE, 'W', 'E', 'B', 'V', 'T', 'T', '\n'};
assertThat(sniffData(data)).isFalse();
}
@Test
public void sniff_failsForIncompleteHeader() throws IOException, InterruptedException {
byte[] data = new byte[] {(byte) 0xFE, (byte) 0xFE, 'W', 'E', 'B', 'V', 'T', '\n'};
assertThat(sniffData(data)).isFalse();
}
@Test
public void sniff_failsForIncorrectHeader() throws IOException, InterruptedException {
byte[] data = new byte[] {(byte) 0xFE, (byte) 0xFE, 'W', 'e', 'B', 'V', 'T', 'T', '\n'};
assertThat(sniffData(data)).isFalse();
}
private static boolean sniffData(byte[] data) throws IOException, InterruptedException {
ExtractorInput input = new FakeExtractorInput.Builder().setData(data).build();
return new WebvttExtractor(/* language= */ null, new TimestampAdjuster(0)).sniff(input);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment