Search in sources :

Example 46 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttDecoderTest method decodeWithOverlappingTimestamps.

@Test
public void decodeWithOverlappingTimestamps() throws Exception {
    WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_OVERLAPPING_TIMESTAMPS_FILE);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(8);
    Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    assertThat(firstCue.text.toString()).isEqualTo("Displayed at the bottom for 3 seconds.");
    assertThat(firstCue.line).isEqualTo(-1f);
    assertThat(firstCue.lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    List<Cue> firstAndSecondCue = subtitle.getCues(subtitle.getEventTime(1));
    assertThat(firstAndSecondCue).hasSize(2);
    assertThat(firstAndSecondCue.get(0).text.toString()).isEqualTo("Displayed at the bottom for 3 seconds.");
    assertThat(firstAndSecondCue.get(0).line).isEqualTo(-1f);
    assertThat(firstAndSecondCue.get(0).lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    assertThat(firstAndSecondCue.get(1).text.toString()).isEqualTo("Appears directly above for 1 second.");
    assertThat(firstAndSecondCue.get(1).line).isEqualTo(-2f);
    assertThat(firstAndSecondCue.get(1).lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4)));
    assertThat(thirdCue.text.toString()).isEqualTo("Displayed at the bottom for 2 seconds.");
    assertThat(thirdCue.line).isEqualTo(-1f);
    assertThat(thirdCue.lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    List<Cue> thirdAndFourthCue = subtitle.getCues(subtitle.getEventTime(5));
    assertThat(thirdAndFourthCue).hasSize(2);
    assertThat(thirdAndFourthCue.get(0).text.toString()).isEqualTo("Displayed at the bottom for 2 seconds.");
    assertThat(thirdAndFourthCue.get(0).line).isEqualTo(-1f);
    assertThat(thirdAndFourthCue.get(0).lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    assertThat(thirdAndFourthCue.get(1).text.toString()).isEqualTo("Appears directly above the previous cue, then replaces it after 1 second.");
    assertThat(thirdAndFourthCue.get(1).line).isEqualTo(-2f);
    assertThat(thirdAndFourthCue.get(1).lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    Cue fourthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6)));
    assertThat(fourthCue.text.toString()).isEqualTo("Appears directly above the previous cue, then replaces it after 1 second.");
    assertThat(fourthCue.line).isEqualTo(-1f);
    assertThat(fourthCue.lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Test(org.junit.Test)

Example 47 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttDecoderTest method decodeTypicalWithBadTimestamps.

@Test
public void decodeTypicalWithBadTimestamps() throws Exception {
    WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_BAD_TIMESTAMPS);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
    assertThat(subtitle.getEventTime(0)).isEqualTo(0L);
    assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L);
    Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle.");
    assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L);
    assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L);
    Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2)));
    assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle.");
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Test(org.junit.Test)

Example 48 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttDecoderTest method decodeWithPositioning.

@Test
public void decodeWithPositioning() throws Exception {
    WebvttSubtitle subtitle = getSubtitleForTestAsset(WITH_POSITIONING_FILE);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(16);
    assertThat(subtitle.getEventTime(0)).isEqualTo(0L);
    assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L);
    Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle.");
    assertThat(firstCue.position).isEqualTo(0.6f);
    assertThat(firstCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
    assertThat(firstCue.textAlignment).isEqualTo(Alignment.ALIGN_NORMAL);
    assertThat(firstCue.size).isEqualTo(0.35f);
    // Unspecified values should use WebVTT defaults
    assertThat(firstCue.line).isEqualTo(-1f);
    assertThat(firstCue.lineType).isEqualTo(Cue.LINE_TYPE_NUMBER);
    assertThat(firstCue.verticalType).isEqualTo(Cue.TYPE_UNSET);
    assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L);
    assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L);
    Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2)));
    assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle.");
    // Position is invalid so defaults to 0.5
    assertThat(secondCue.position).isEqualTo(0.5f);
    assertThat(secondCue.textAlignment).isEqualTo(Alignment.ALIGN_OPPOSITE);
    assertThat(subtitle.getEventTime(4)).isEqualTo(4_000_000L);
    assertThat(subtitle.getEventTime(5)).isEqualTo(5_000_000L);
    Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4)));
    assertThat(thirdCue.text.toString()).isEqualTo("This is the third subtitle.");
    assertThat(thirdCue.line).isEqualTo(0.45f);
    assertThat(thirdCue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION);
    assertThat(thirdCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
    assertThat(thirdCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER);
    // Derived from `align:middle`:
    assertThat(thirdCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);
    assertThat(subtitle.getEventTime(6)).isEqualTo(6_000_000L);
    assertThat(subtitle.getEventTime(7)).isEqualTo(7_000_000L);
    Cue fourthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6)));
    assertThat(fourthCue.text.toString()).isEqualTo("This is the fourth subtitle.");
    assertThat(fourthCue.line).isEqualTo(-10f);
    assertThat(fourthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
    assertThat(fourthCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER);
    // Derived from `align:middle`:
    assertThat(fourthCue.position).isEqualTo(0.5f);
    assertThat(fourthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);
    assertThat(subtitle.getEventTime(8)).isEqualTo(8_000_000L);
    assertThat(subtitle.getEventTime(9)).isEqualTo(9_000_000L);
    Cue fifthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(8)));
    assertThat(fifthCue.text.toString()).isEqualTo("This is the fifth subtitle.");
    assertThat(fifthCue.textAlignment).isEqualTo(Alignment.ALIGN_OPPOSITE);
    // Derived from `align:right`:
    assertThat(fifthCue.position).isEqualTo(1.0f);
    assertThat(fifthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
    assertThat(subtitle.getEventTime(10)).isEqualTo(10_000_000L);
    assertThat(subtitle.getEventTime(11)).isEqualTo(11_000_000L);
    Cue sixthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(10)));
    assertThat(sixthCue.text.toString()).isEqualTo("This is the sixth subtitle.");
    assertThat(sixthCue.textAlignment).isEqualTo(Alignment.ALIGN_CENTER);
    // Derived from `align:center`:
    assertThat(sixthCue.position).isEqualTo(0.5f);
    assertThat(sixthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);
    assertThat(subtitle.getEventTime(12)).isEqualTo(12_000_000L);
    assertThat(subtitle.getEventTime(13)).isEqualTo(13_000_000L);
    Cue seventhCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(12)));
    assertThat(seventhCue.text.toString()).isEqualTo("This is the seventh subtitle.");
    assertThat(seventhCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
    assertThat(subtitle.getEventTime(14)).isEqualTo(14_000_000L);
    assertThat(subtitle.getEventTime(15)).isEqualTo(15_000_000L);
    Cue eighthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(14)));
    assertThat(eighthCue.text.toString()).isEqualTo("This is the eighth subtitle.");
    assertThat(eighthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Test(org.junit.Test)

Example 49 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class WebvttDecoderTest method decodeTypicalWithIds.

@Test
public void decodeTypicalWithIds() throws Exception {
    WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_IDS_FILE);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(4);
    assertThat(subtitle.getEventTime(0)).isEqualTo(0L);
    assertThat(subtitle.getEventTime(1)).isEqualTo(1_234_000L);
    Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    assertThat(firstCue.text.toString()).isEqualTo("This is the first subtitle.");
    assertThat(subtitle.getEventTime(2)).isEqualTo(2_345_000L);
    assertThat(subtitle.getEventTime(3)).isEqualTo(3_456_000L);
    Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2)));
    assertThat(secondCue.text.toString()).isEqualTo("This is the second subtitle.");
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Test(org.junit.Test)

Example 50 with Cue

use of com.google.android.exoplayer2.text.Cue in project ExoPlayer by google.

the class TtmlDecoderTest method bitmapPercentageRegion.

@Test
public void bitmapPercentageRegion() throws IOException, SubtitleDecoderException {
    TtmlSubtitle subtitle = getSubtitle(BITMAP_REGION_FILE);
    Cue cue = getOnlyCueAtTimeUs(subtitle, 1_000_000);
    assertThat(cue.text).isNull();
    assertThat(cue.bitmap).isNotNull();
    assertThat(cue.position).isEqualTo(24f / 100f);
    assertThat(cue.line).isEqualTo(28f / 100f);
    assertThat(cue.size).isEqualTo(51f / 100f);
    assertThat(cue.bitmapHeight).isEqualTo(12f / 100f);
    cue = getOnlyCueAtTimeUs(subtitle, 4_000_000);
    assertThat(cue.text).isNull();
    assertThat(cue.bitmap).isNotNull();
    assertThat(cue.position).isEqualTo(21f / 100f);
    assertThat(cue.line).isEqualTo(35f / 100f);
    assertThat(cue.size).isEqualTo(57f / 100f);
    assertThat(cue.bitmapHeight).isEqualTo(6f / 100f);
    cue = getOnlyCueAtTimeUs(subtitle, 7_500_000);
    assertThat(cue.text).isNull();
    assertThat(cue.bitmap).isNotNull();
    assertThat(cue.position).isEqualTo(24f / 100f);
    assertThat(cue.line).isEqualTo(28f / 100f);
    assertThat(cue.size).isEqualTo(51f / 100f);
    assertThat(cue.bitmapHeight).isEqualTo(12f / 100f);
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Test(org.junit.Test)

Aggregations

Cue (com.google.android.exoplayer2.text.Cue)58 Test (org.junit.Test)40 Subtitle (com.google.android.exoplayer2.text.Subtitle)13 ArrayList (java.util.ArrayList)12 Nullable (androidx.annotation.Nullable)10 Spanned (android.text.Spanned)8 SpannableStringBuilder (android.text.SpannableStringBuilder)7 ParsableByteArray (com.google.android.exoplayer2.util.ParsableByteArray)5 FakeExtractorOutput (com.google.android.exoplayer2.testutil.FakeExtractorOutput)4 SpannableString (android.text.SpannableString)3 UnderlineSpan (android.text.style.UnderlineSpan)3 Format (com.google.android.exoplayer2.Format)3 AdPlaybackState (com.google.android.exoplayer2.source.ads.AdPlaybackState)3 FakeExtractorInput (com.google.android.exoplayer2.testutil.FakeExtractorInput)3 FakeTrackOutput (com.google.android.exoplayer2.testutil.FakeTrackOutput)3 HorizontalTextInVerticalContextSpan (com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan)3 RubySpan (com.google.android.exoplayer2.text.span.RubySpan)3 TextEmphasisSpan (com.google.android.exoplayer2.text.span.TextEmphasisSpan)3 WebvttDecoder (com.google.android.exoplayer2.text.webvtt.WebvttDecoder)3 Matcher (java.util.regex.Matcher)3