Search in sources :

Example 11 with Cue

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

the class SubtitleViewUtilsTest method testRemoveEmbeddedFontSizes.

@Test
public void testRemoveEmbeddedFontSizes() {
    Cue.Builder cueBuilder = CUE.buildUpon();
    SubtitleViewUtils.removeEmbeddedFontSizes(cueBuilder);
    Cue strippedCue = cueBuilder.build();
    Spanned originalText = (Spanned) CUE.text;
    Spanned strippedText = (Spanned) strippedCue.text;
    // Assert all non text-size properties and spans are kept
    assertThat(strippedCue.textAlignment).isEqualTo(CUE.textAlignment);
    assertThat(strippedCue.multiRowAlignment).isEqualTo(CUE.multiRowAlignment);
    assertThat(strippedCue.line).isEqualTo(CUE.line);
    assertThat(strippedCue.lineType).isEqualTo(CUE.lineType);
    assertThat(strippedCue.position).isEqualTo(CUE.position);
    assertThat(strippedCue.positionAnchor).isEqualTo(CUE.positionAnchor);
    assertThat(strippedCue.size).isEqualTo(CUE.size);
    assertThat(strippedCue.windowColor).isEqualTo(CUE.windowColor);
    assertThat(strippedCue.windowColorSet).isEqualTo(CUE.windowColorSet);
    assertThat(strippedCue.verticalType).isEqualTo(CUE.verticalType);
    assertThat(strippedCue.shearDegrees).isEqualTo(CUE.shearDegrees);
    TextEmphasisSpan expectedTextEmphasisSpan = originalText.getSpans(0, originalText.length(), TextEmphasisSpan.class)[0];
    assertThat(strippedText).hasTextEmphasisSpanBetween(originalText.getSpanStart(expectedTextEmphasisSpan), originalText.getSpanEnd(expectedTextEmphasisSpan));
    RubySpan expectedRubySpan = originalText.getSpans(0, originalText.length(), RubySpan.class)[0];
    assertThat(strippedText).hasRubySpanBetween(originalText.getSpanStart(expectedRubySpan), originalText.getSpanEnd(expectedRubySpan));
    HorizontalTextInVerticalContextSpan expectedHorizontalTextInVerticalContextSpan = originalText.getSpans(0, originalText.length(), HorizontalTextInVerticalContextSpan.class)[0];
    assertThat(strippedText).hasHorizontalTextInVerticalContextSpanBetween(originalText.getSpanStart(expectedHorizontalTextInVerticalContextSpan), originalText.getSpanEnd(expectedHorizontalTextInVerticalContextSpan));
    UnderlineSpan expectedUnderlineSpan = originalText.getSpans(0, originalText.length(), UnderlineSpan.class)[0];
    assertThat(strippedText).hasUnderlineSpanBetween(originalText.getSpanStart(expectedUnderlineSpan), originalText.getSpanEnd(expectedUnderlineSpan));
    // Assert the text-size properties and spans are removed
    assertThat(strippedCue.textSize).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(strippedCue.textSizeType).isEqualTo(Cue.TYPE_UNSET);
    assertThat(strippedText).hasNoRelativeSizeSpanBetween(0, strippedText.length());
    assertThat(strippedText).hasNoAbsoluteSizeSpanBetween(0, strippedText.length());
}
Also used : RubySpan(com.google.android.exoplayer2.text.span.RubySpan) Cue(com.google.android.exoplayer2.text.Cue) HorizontalTextInVerticalContextSpan(com.google.android.exoplayer2.text.span.HorizontalTextInVerticalContextSpan) TextEmphasisSpan(com.google.android.exoplayer2.text.span.TextEmphasisSpan) Spanned(android.text.Spanned) UnderlineSpan(android.text.style.UnderlineSpan) Test(org.junit.Test)

Example 12 with Cue

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

the class CanvasSubtitleOutput method dispatchDraw.

@Override
public void dispatchDraw(Canvas canvas) {
    @Nullable List<Cue> cues = this.cues;
    if (cues.isEmpty()) {
        return;
    }
    int rawViewHeight = getHeight();
    // Calculate the cue box bounds relative to the canvas after padding is taken into account.
    int left = getPaddingLeft();
    int top = getPaddingTop();
    int right = getWidth() - getPaddingRight();
    int bottom = rawViewHeight - getPaddingBottom();
    if (bottom <= top || right <= left) {
        // No space to draw subtitles.
        return;
    }
    int viewHeightMinusPadding = bottom - top;
    float defaultViewTextSizePx = SubtitleViewUtils.resolveTextSize(textSizeType, textSize, rawViewHeight, viewHeightMinusPadding);
    if (defaultViewTextSizePx <= 0) {
        // Text has no height.
        return;
    }
    int cueCount = cues.size();
    for (int i = 0; i < cueCount; i++) {
        Cue cue = cues.get(i);
        if (cue.verticalType != Cue.TYPE_UNSET) {
            cue = repositionVerticalCue(cue);
        }
        float cueTextSizePx = SubtitleViewUtils.resolveTextSize(cue.textSizeType, cue.textSize, rawViewHeight, viewHeightMinusPadding);
        SubtitlePainter painter = painters.get(i);
        painter.draw(cue, style, defaultViewTextSizePx, cueTextSizePx, bottomPaddingFraction, canvas, left, top, right, bottom);
    }
}
Also used : Cue(com.google.android.exoplayer2.text.Cue) Nullable(androidx.annotation.Nullable)

Example 13 with Cue

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

the class SubripDecoderTest method assertAlignmentCue.

private static void assertAlignmentCue(Subtitle subtitle, int eventIndex, @Cue.AnchorType int lineAnchor, @Cue.AnchorType int positionAnchor) {
    long eventTimeUs = subtitle.getEventTime(eventIndex);
    Cue cue = subtitle.getCues(eventTimeUs).get(0);
    assertThat(cue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION);
    assertThat(cue.lineAnchor).isEqualTo(lineAnchor);
    assertThat(cue.line).isEqualTo(SubripDecoder.getFractionalPositionForAnchorType(lineAnchor));
    assertThat(cue.positionAnchor).isEqualTo(positionAnchor);
    assertThat(cue.position).isEqualTo(SubripDecoder.getFractionalPositionForAnchorType(positionAnchor));
}
Also used : Cue(com.google.android.exoplayer2.text.Cue)

Example 14 with Cue

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

the class SubripDecoderTest method decodeTypicalNegativeTimestamps.

@Test
public void decodeTypicalNegativeTimestamps() throws IOException {
    // Parsing should succeed, parsing the third cue only.
    SubripDecoder decoder = new SubripDecoder();
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), TYPICAL_NEGATIVE_TIMESTAMPS);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
    assertTypicalCue3(subtitle, 0);
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) Test(org.junit.Test)

Example 15 with Cue

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

the class SsaDecoderTest method decodeOverlappingTimecodes.

@Test
public void decodeOverlappingTimecodes() throws IOException {
    SsaDecoder decoder = new SsaDecoder();
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), OVERLAPPING_TIMECODES);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    assertThat(subtitle.getEventTime(0)).isEqualTo(1_000_000);
    assertThat(subtitle.getEventTime(1)).isEqualTo(2_000_000);
    assertThat(subtitle.getEventTime(2)).isEqualTo(4_230_000);
    assertThat(subtitle.getEventTime(3)).isEqualTo(5_230_000);
    assertThat(subtitle.getEventTime(4)).isEqualTo(6_000_000);
    assertThat(subtitle.getEventTime(5)).isEqualTo(8_440_000);
    assertThat(subtitle.getEventTime(6)).isEqualTo(9_440_000);
    assertThat(subtitle.getEventTime(7)).isEqualTo(10_720_000);
    assertThat(subtitle.getEventTime(8)).isEqualTo(13_220_000);
    assertThat(subtitle.getEventTime(9)).isEqualTo(14_220_000);
    assertThat(subtitle.getEventTime(10)).isEqualTo(15_650_000);
    String firstSubtitleText = "First subtitle - end overlaps second";
    String secondSubtitleText = "Second subtitle - beginning overlaps first";
    String thirdSubtitleText = "Third subtitle - out of order";
    String fourthSubtitleText = "Fourth subtitle - same timings as fifth";
    String fifthSubtitleText = "Fifth subtitle - same timings as fourth";
    String sixthSubtitleText = "Sixth subtitle - fully encompasses seventh";
    String seventhSubtitleText = "Seventh subtitle - nested fully inside sixth";
    assertThat(Iterables.transform(subtitle.getCues(1_000_010), cue -> cue.text.toString())).containsExactly(firstSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(2_000_010), cue -> cue.text.toString())).containsExactly(firstSubtitleText, secondSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(4_230_010), cue -> cue.text.toString())).containsExactly(secondSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(5_230_010), cue -> cue.text.toString())).isEmpty();
    assertThat(Iterables.transform(subtitle.getCues(6_000_010), cue -> cue.text.toString())).containsExactly(thirdSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(8_440_010), cue -> cue.text.toString())).containsExactly(fourthSubtitleText, fifthSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(9_440_010), cue -> cue.text.toString())).isEmpty();
    assertThat(Iterables.transform(subtitle.getCues(10_720_010), cue -> cue.text.toString())).containsExactly(sixthSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(13_220_010), cue -> cue.text.toString())).containsExactly(sixthSubtitleText, seventhSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(14_220_010), cue -> cue.text.toString())).containsExactly(sixthSubtitleText);
    assertThat(Iterables.transform(subtitle.getCues(15_650_010), cue -> cue.text.toString())).isEmpty();
}
Also used : Subtitle(com.google.android.exoplayer2.text.Subtitle) 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