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 { ...@@ -27,6 +27,11 @@ public class SubtitleDecoderException extends Exception {
super(message); super(message);
} }
/** @param cause The cause of this exception. */
public SubtitleDecoderException(Exception cause) {
super(cause);
}
/** /**
* @param message The detail message for this exception. * @param message The detail message for this exception.
* @param cause The cause of this exception. * @param cause The cause of this exception.
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
package com.google.android.exoplayer2.text.webvtt; package com.google.android.exoplayer2.text.webvtt;
import android.text.TextUtils; import android.text.TextUtils;
import com.google.android.exoplayer2.ParserException;
import com.google.android.exoplayer2.text.SimpleSubtitleDecoder; import com.google.android.exoplayer2.text.SimpleSubtitleDecoder;
import com.google.android.exoplayer2.text.SubtitleDecoderException; import com.google.android.exoplayer2.text.SubtitleDecoderException;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
...@@ -62,7 +63,11 @@ public final class WebvttDecoder extends SimpleSubtitleDecoder { ...@@ -62,7 +63,11 @@ public final class WebvttDecoder extends SimpleSubtitleDecoder {
definedStyles.clear(); definedStyles.clear();
// Validate the first line of the header, and skip the remainder. // Validate the first line of the header, and skip the remainder.
try {
WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData); WebvttParserUtil.validateWebvttHeaderLine(parsableWebvttData);
} catch (ParserException e) {
throw new SubtitleDecoderException(e);
}
while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {} while (!TextUtils.isEmpty(parsableWebvttData.readLine())) {}
int event; int event;
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
package com.google.android.exoplayer2.text.webvtt; 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.ParsableByteArray;
import com.google.android.exoplayer2.util.Util; import com.google.android.exoplayer2.util.Util;
import java.util.regex.Matcher; import java.util.regex.Matcher;
...@@ -27,7 +27,9 @@ import java.util.regex.Pattern; ...@@ -27,7 +27,9 @@ import java.util.regex.Pattern;
public final class WebvttParserUtil { public final class WebvttParserUtil {
private static final Pattern COMMENT = Pattern.compile("^NOTE((\u0020|\u0009).*)?$"); 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() {} private WebvttParserUtil() {}
...@@ -35,14 +37,33 @@ public final class WebvttParserUtil { ...@@ -35,14 +37,33 @@ public final class WebvttParserUtil {
* Reads and validates the first line of a WebVTT file. * Reads and validates the first line of a WebVTT file.
* *
* @param input The input from which the line should be read. * @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) public static void validateWebvttHeaderLine(ParsableByteArray input) throws ParserException {
throws SubtitleDecoderException { int startPosition = input.getPosition();
String line = input.readLine(); if (!isWebvttHeaderLine(input)) {
if (line == null || !HEADER.matcher(line).matches()) { input.setPosition(startPosition);
throw new SubtitleDecoderException("Expected WEBVTT. Got " + line); 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 { ...@@ -499,10 +499,10 @@ public final class ParsableByteArray {
/** /**
* Reads a line of text. * 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 * ('\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 * @return The line not including any line-termination characters, or null if the end of the data
* has already been reached. * has already been reached.
...@@ -517,7 +517,7 @@ public final class ParsableByteArray { ...@@ -517,7 +517,7 @@ public final class ParsableByteArray {
} }
if (lineLimit - position >= 3 && data[position] == (byte) 0xEF if (lineLimit - position >= 3 && data[position] == (byte) 0xEF
&& data[position + 1] == (byte) 0xBB && data[position + 2] == (byte) 0xBF) { && 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; position += 3;
} }
String line = Util.fromUtf8Bytes(data, position, lineLimit - position); String line = Util.fromUtf8Bytes(data, position, lineLimit - position);
......
No preview for this file type
...@@ -49,6 +49,7 @@ public class WebvttDecoderTest { ...@@ -49,6 +49,7 @@ public class WebvttDecoderTest {
private static final String WITH_TAGS_FILE = "webvtt/with_tags"; 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_STYLES = "webvtt/with_css_styles";
private static final String WITH_CSS_COMPLEX_SELECTORS = "webvtt/with_css_complex_selectors"; 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"; private static final String EMPTY_FILE = "webvtt/empty";
@Test @Test
...@@ -56,7 +57,7 @@ public class WebvttDecoderTest { ...@@ -56,7 +57,7 @@ public class WebvttDecoderTest {
WebvttDecoder decoder = new WebvttDecoder(); WebvttDecoder decoder = new WebvttDecoder();
byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, EMPTY_FILE); byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, EMPTY_FILE);
try { try {
decoder.decode(bytes, bytes.length, false); decoder.decode(bytes, bytes.length, /* reset= */ false);
fail(); fail();
} catch (SubtitleDecoderException expected) { } catch (SubtitleDecoderException expected) {
// Do nothing. // Do nothing.
...@@ -71,8 +72,40 @@ public class WebvttDecoderTest { ...@@ -71,8 +72,40 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); 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 @Test
...@@ -83,8 +116,18 @@ public class WebvttDecoderTest { ...@@ -83,8 +116,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); 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 @Test
...@@ -95,8 +138,18 @@ public class WebvttDecoderTest { ...@@ -95,8 +138,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); 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 @Test
...@@ -107,8 +160,18 @@ public class WebvttDecoderTest { ...@@ -107,8 +160,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// test cues // test cues
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); 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 @Test
...@@ -119,10 +182,30 @@ public class WebvttDecoderTest { ...@@ -119,10 +182,30 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(8); assertThat(subtitle.getEventTimeCount()).isEqualTo(8);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); subtitle,
assertCue(subtitle, 4, 4000000, 5000000, "This is the third subtitle."); /* eventTimeIndex= */ 0,
assertCue(subtitle, 6, 6000000, 7000000, "This is the <fourth> &subtitle."); /* 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 @Test
...@@ -133,82 +216,82 @@ public class WebvttDecoderTest { ...@@ -133,82 +216,82 @@ public class WebvttDecoderTest {
// Test cues. // Test cues.
assertCue( assertCue(
subtitle, subtitle,
0, /* eventTimeIndex= */ 0,
0, /* startTimeUs= */ 0,
1234000, /* endTimeUs= */ 1234000,
"This is the first subtitle.", "This is the first subtitle.",
Alignment.ALIGN_NORMAL, Alignment.ALIGN_NORMAL,
Cue.DIMEN_UNSET, /* line= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* lineType= */ Cue.TYPE_UNSET,
Cue.TYPE_UNSET, /* lineAnchor= */ Cue.TYPE_UNSET,
0.1f, /* position= */ 0.1f,
Cue.ANCHOR_TYPE_START, /* positionAnchor= */ Cue.ANCHOR_TYPE_START,
0.35f); /* size= */ 0.35f);
assertCue( assertCue(
subtitle, subtitle,
2, /* eventTimeIndex= */ 2,
2345000, /* startTimeUs= */ 2345000,
3456000, /* endTimeUs= */ 3456000,
"This is the second subtitle.", "This is the second subtitle.",
Alignment.ALIGN_OPPOSITE, Alignment.ALIGN_OPPOSITE,
Cue.DIMEN_UNSET, /* line= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* lineType= */ Cue.TYPE_UNSET,
Cue.TYPE_UNSET, /* lineAnchor= */ Cue.TYPE_UNSET,
Cue.DIMEN_UNSET, /* position= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* positionAnchor= */ Cue.TYPE_UNSET,
0.35f); /* size= */ 0.35f);
assertCue( assertCue(
subtitle, subtitle,
4, /* eventTimeIndex= */ 4,
4000000, /* startTimeUs= */ 4000000,
5000000, /* endTimeUs= */ 5000000,
"This is the third subtitle.", "This is the third subtitle.",
Alignment.ALIGN_CENTER, Alignment.ALIGN_CENTER,
0.45f, /* line= */ 0.45f,
Cue.LINE_TYPE_FRACTION, /* lineType= */ Cue.LINE_TYPE_FRACTION,
Cue.ANCHOR_TYPE_END, /* lineAnchor= */ Cue.ANCHOR_TYPE_END,
Cue.DIMEN_UNSET, /* position= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* positionAnchor= */ Cue.TYPE_UNSET,
0.35f); /* size= */ 0.35f);
assertCue( assertCue(
subtitle, subtitle,
6, /* eventTimeIndex= */ 6,
6000000, /* startTimeUs= */ 6000000,
7000000, /* endTimeUs= */ 7000000,
"This is the fourth subtitle.", "This is the fourth subtitle.",
Alignment.ALIGN_CENTER, Alignment.ALIGN_CENTER,
-11f, /* line= */ -11f,
Cue.LINE_TYPE_NUMBER, /* lineType= */ Cue.LINE_TYPE_NUMBER,
Cue.TYPE_UNSET, /* lineAnchor= */ Cue.TYPE_UNSET,
Cue.DIMEN_UNSET, /* position= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* positionAnchor= */ Cue.TYPE_UNSET,
Cue.DIMEN_UNSET); /* size= */ Cue.DIMEN_UNSET);
assertCue( assertCue(
subtitle, subtitle,
8, /* eventTimeIndex= */ 8,
7000000, /* startTimeUs= */ 7000000,
8000000, /* endTimeUs= */ 8000000,
"This is the fifth subtitle.", "This is the fifth subtitle.",
Alignment.ALIGN_OPPOSITE, Alignment.ALIGN_OPPOSITE,
Cue.DIMEN_UNSET, /* line= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* lineType= */ Cue.TYPE_UNSET,
Cue.TYPE_UNSET, /* lineAnchor= */ Cue.TYPE_UNSET,
0.1f, /* position= */ 0.1f,
Cue.ANCHOR_TYPE_END, /* positionAnchor= */ Cue.ANCHOR_TYPE_END,
0.1f); /* size= */ 0.1f);
assertCue( assertCue(
subtitle, subtitle,
10, /* eventTimeIndex= */ 10,
10000000, /* startTimeUs= */ 10000000,
11000000, /* endTimeUs= */ 11000000,
"This is the sixth subtitle.", "This is the sixth subtitle.",
Alignment.ALIGN_CENTER, Alignment.ALIGN_CENTER,
0.45f, /* line= */ 0.45f,
Cue.LINE_TYPE_FRACTION, /* lineType= */ Cue.LINE_TYPE_FRACTION,
Cue.ANCHOR_TYPE_END, /* lineAnchor= */ Cue.ANCHOR_TYPE_END,
Cue.DIMEN_UNSET, /* position= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* positionAnchor= */ Cue.TYPE_UNSET,
0.35f); /* size= */ 0.35f);
} }
@Test @Test
...@@ -219,8 +302,18 @@ public class WebvttDecoderTest { ...@@ -219,8 +302,18 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(4); assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 4000000, 5000000, "This is the third subtitle."); 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 @Test
...@@ -231,57 +324,70 @@ public class WebvttDecoderTest { ...@@ -231,57 +324,70 @@ public class WebvttDecoderTest {
assertThat(subtitle.getEventTimeCount()).isEqualTo(8); assertThat(subtitle.getEventTimeCount()).isEqualTo(8);
// Test cues. // Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle."); assertCue(
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle."); subtitle,
/* eventTimeIndex= */ 0,
Spanned s1 = getUniqueSpanTextAt(subtitle, 0); /* startTimeUs= */ 0,
Spanned s2 = getUniqueSpanTextAt(subtitle, 2345000); /* endTimeUs= */ 1234000,
Spanned s3 = getUniqueSpanTextAt(subtitle, 20000000); "This is the first subtitle.");
Spanned s4 = getUniqueSpanTextAt(subtitle, 25000000); assertCue(
assertThat(s1.getSpans(0, s1.length(), ForegroundColorSpan.class)).hasLength(1); subtitle,
assertThat(s1.getSpans(0, s1.length(), BackgroundColorSpan.class)).hasLength(1); /* eventTimeIndex= */ 2,
assertThat(s2.getSpans(0, s2.length(), ForegroundColorSpan.class)).hasLength(2); /* startTimeUs= */ 2345000,
assertThat(s3.getSpans(10, s3.length(), UnderlineSpan.class)).hasLength(1); /* endTimeUs= */ 3456000,
assertThat(s4.getSpans(0, 16, BackgroundColorSpan.class)).hasLength(2); "This is the second subtitle.");
assertThat(s4.getSpans(17, s4.length(), StyleSpan.class)).hasLength(1);
assertThat(s4.getSpans(17, s4.length(), StyleSpan.class)[0].getStyle()) 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); .isEqualTo(Typeface.BOLD);
} }
@Test @Test
public void testWithComplexCssSelectors() throws IOException, SubtitleDecoderException { public void testWithComplexCssSelectors() throws IOException, SubtitleDecoderException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS); WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_CSS_COMPLEX_SELECTORS);
Spanned text = getUniqueSpanTextAt(subtitle, 0); Spanned text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 0);
assertThat(text.getSpans(30, text.length(), ForegroundColorSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class))
assertThat(text.getSpans(30, text.length(), ForegroundColorSpan.class)[0].getForegroundColor()) .hasLength(1);
assertThat(
text.getSpans(/* start= */ 30, text.length(), ForegroundColorSpan.class)[0]
.getForegroundColor())
.isEqualTo(0xFFEE82EE); .isEqualTo(0xFFEE82EE);
assertThat(text.getSpans(30, text.length(), TypefaceSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 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)[0].getFamily())
.isEqualTo("courier"); .isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 2000000); text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2000000);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 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)[0].getFamily())
.isEqualTo("courier"); .isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 2500000); text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 2500000);
assertThat(text.getSpans(5, text.length(), StyleSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(5, text.length(), StyleSpan.class)[0].getStyle()) assertThat(text.getSpans(/* start= */ 5, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD); .isEqualTo(Typeface.BOLD);
assertThat(text.getSpans(5, text.length(), TypefaceSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 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)[0].getFamily())
.isEqualTo("courier"); .isEqualTo("courier");
text = getUniqueSpanTextAt(subtitle, 4000000); text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 4000000);
assertThat(text.getSpans(6, 22, StyleSpan.class)).hasLength(0); assertThat(text.getSpans(/* start= */ 6, /* end= */ 22, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(30, text.length(), StyleSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(30, text.length(), StyleSpan.class)[0].getStyle()) assertThat(text.getSpans(/* start= */ 30, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.BOLD); .isEqualTo(Typeface.BOLD);
text = getUniqueSpanTextAt(subtitle, 5000000); text = getUniqueSpanTextAt(subtitle, /* timeUs= */ 5000000);
assertThat(text.getSpans(9, 17, StyleSpan.class)).hasLength(0); assertThat(text.getSpans(/* start= */ 9, /* end= */ 17, StyleSpan.class)).hasLength(0);
assertThat(text.getSpans(19, text.length(), StyleSpan.class)).hasLength(1); assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)).hasLength(1);
assertThat(text.getSpans(19, text.length(), StyleSpan.class)[0].getStyle()) assertThat(text.getSpans(/* start= */ 19, text.length(), StyleSpan.class)[0].getStyle())
.isEqualTo(Typeface.ITALIC); .isEqualTo(Typeface.ITALIC);
} }
...@@ -289,7 +395,7 @@ public class WebvttDecoderTest { ...@@ -289,7 +395,7 @@ public class WebvttDecoderTest {
throws IOException, SubtitleDecoderException { throws IOException, SubtitleDecoderException {
WebvttDecoder decoder = new WebvttDecoder(); WebvttDecoder decoder = new WebvttDecoder();
byte[] bytes = TestUtil.getByteArray(RuntimeEnvironment.application, asset); 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) { private Spanned getUniqueSpanTextAt(WebvttSubtitle sub, long timeUs) {
...@@ -304,13 +410,13 @@ public class WebvttDecoderTest { ...@@ -304,13 +410,13 @@ public class WebvttDecoderTest {
startTimeUs, startTimeUs,
endTimeUs, endTimeUs,
text, text,
null, /* textAlignment= */ null,
Cue.DIMEN_UNSET, /* line= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* lineType= */ Cue.TYPE_UNSET,
Cue.TYPE_UNSET, /* lineAnchor= */ Cue.TYPE_UNSET,
Cue.DIMEN_UNSET, /* position= */ Cue.DIMEN_UNSET,
Cue.TYPE_UNSET, /* positionAnchor= */ Cue.TYPE_UNSET,
Cue.DIMEN_UNSET); /* size= */ Cue.DIMEN_UNSET);
} }
private static void assertCue( private static void assertCue(
......
...@@ -25,7 +25,6 @@ import com.google.android.exoplayer2.extractor.ExtractorOutput; ...@@ -25,7 +25,6 @@ import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.PositionHolder; import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.SeekMap; import com.google.android.exoplayer2.extractor.SeekMap;
import com.google.android.exoplayer2.extractor.TrackOutput; 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.text.webvtt.WebvttParserUtil;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.util.ParsableByteArray; import com.google.android.exoplayer2.util.ParsableByteArray;
...@@ -47,6 +46,7 @@ public final class WebvttExtractor implements Extractor { ...@@ -47,6 +46,7 @@ public final class WebvttExtractor implements Extractor {
private static final Pattern LOCAL_TIMESTAMP = Pattern.compile("LOCAL:([^,]+)"); private static final Pattern LOCAL_TIMESTAMP = Pattern.compile("LOCAL:([^,]+)");
private static final Pattern MEDIA_TIMESTAMP = Pattern.compile("MPEGTS:(\\d+)"); 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 String language;
private final TimestampAdjuster timestampAdjuster; private final TimestampAdjuster timestampAdjuster;
...@@ -68,8 +68,10 @@ public final class WebvttExtractor implements Extractor { ...@@ -68,8 +68,10 @@ public final class WebvttExtractor implements Extractor {
@Override @Override
public boolean sniff(ExtractorInput input) throws IOException, InterruptedException { public boolean sniff(ExtractorInput input) throws IOException, InterruptedException {
// This extractor is only used for the HLS use case, which should not call this method. input.peekFully(
throw new IllegalStateException(); sampleData, /* offset= */ 0, /* length= */ HEADER_MAX_LENGTH, /* allowEndOfInput= */ false);
sampleDataWrapper.reset(sampleData, HEADER_MAX_LENGTH);
return WebvttParserUtil.isWebvttHeaderLine(sampleDataWrapper);
} }
@Override @Override
...@@ -118,11 +120,7 @@ public final class WebvttExtractor implements Extractor { ...@@ -118,11 +120,7 @@ public final class WebvttExtractor implements Extractor {
ParsableByteArray webvttData = new ParsableByteArray(sampleData); ParsableByteArray webvttData = new ParsableByteArray(sampleData);
// Validate the first line of the header. // Validate the first line of the header.
try {
WebvttParserUtil.validateWebvttHeaderLine(webvttData); WebvttParserUtil.validateWebvttHeaderLine(webvttData);
} catch (SubtitleDecoderException e) {
throw new ParserException(e);
}
// Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header. // Defaults to use if the header doesn't contain an X-TIMESTAMP-MAP header.
long vttTimestampUs = 0; 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