Search in sources :

Example 11 with Cue

use of androidx.media3.common.text.Cue in project media by androidx.

the class CueSerializationTest method serializingCueWithoutSpans.

@Test
public void serializingCueWithoutSpans() {
    CueEncoder encoder = new CueEncoder();
    CueDecoder decoder = new CueDecoder();
    Cue cue = new Cue.Builder().setText(SpannedString.valueOf("text")).setTextAlignment(Layout.Alignment.ALIGN_CENTER).setMultiRowAlignment(Layout.Alignment.ALIGN_NORMAL).setLine(5, Cue.LINE_TYPE_NUMBER).setLineAnchor(Cue.ANCHOR_TYPE_END).setPosition(0.4f).setPositionAnchor(Cue.ANCHOR_TYPE_MIDDLE).setTextSize(0.2f, Cue.TEXT_SIZE_TYPE_FRACTIONAL).setSize(0.8f).setWindowColor(Color.CYAN).setVerticalType(Cue.VERTICAL_TYPE_RL).setShearDegrees(-15f).build();
    // encoding and decoding
    byte[] encodedCues = encoder.encode(ImmutableList.of(cue));
    List<Cue> cuesAfterDecoding = decoder.decode(encodedCues);
    Cue cueAfterDecoding = cuesAfterDecoding.get(0);
    assertThat(cueAfterDecoding.text.toString()).isEqualTo(cue.text.toString());
    assertThat(cueAfterDecoding.textAlignment).isEqualTo(cue.textAlignment);
    assertThat(cueAfterDecoding.multiRowAlignment).isEqualTo(cue.multiRowAlignment);
    assertThat(cueAfterDecoding.line).isEqualTo(cue.line);
    assertThat(cueAfterDecoding.lineType).isEqualTo(cue.lineType);
    assertThat(cueAfterDecoding.position).isEqualTo(cue.position);
    assertThat(cueAfterDecoding.positionAnchor).isEqualTo(cue.positionAnchor);
    assertThat(cueAfterDecoding.textSize).isEqualTo(cue.textSize);
    assertThat(cueAfterDecoding.textSizeType).isEqualTo(cue.textSizeType);
    assertThat(cueAfterDecoding.size).isEqualTo(cue.size);
    assertThat(cueAfterDecoding.windowColor).isEqualTo(cue.windowColor);
    assertThat(cueAfterDecoding.windowColorSet).isEqualTo(cue.windowColorSet);
    assertThat(cueAfterDecoding.verticalType).isEqualTo(cue.verticalType);
    assertThat(cueAfterDecoding.shearDegrees).isEqualTo(cue.shearDegrees);
}
Also used : Cue(androidx.media3.common.text.Cue) Test(org.junit.Test)

Example 12 with Cue

use of androidx.media3.common.text.Cue in project media by androidx.

the class SubtitleExtractorTest method extractor_seekAfterExtracting_outputsCues.

@Test
public void extractor_seekAfterExtracting_outputsCues() throws Exception {
    CueDecoder decoder = new CueDecoder();
    FakeExtractorOutput output = new FakeExtractorOutput();
    FakeExtractorInput input = new FakeExtractorInput.Builder().setData(Util.getUtf8Bytes(TEST_DATA)).setSimulatePartialReads(true).build();
    SubtitleExtractor extractor = new SubtitleExtractor(new WebvttDecoder(), new Format.Builder().setSampleMimeType(MimeTypes.TEXT_VTT).build());
    extractor.init(output);
    FakeTrackOutput trackOutput = output.trackOutputs.get(0);
    while (extractor.read(input, null) != Extractor.RESULT_END_OF_INPUT) {
    }
    extractor.seek((int) output.seekMap.getSeekPoints(2_445_000L).first.position, 2_445_000L);
    input.setPosition((int) output.seekMap.getSeekPoints(2_445_000L).first.position);
    trackOutput.clear();
    while (extractor.read(input, null) != Extractor.RESULT_END_OF_INPUT) {
    }
    assertThat(trackOutput.lastFormat.sampleMimeType).isEqualTo(MimeTypes.TEXT_EXOPLAYER_CUES);
    assertThat(trackOutput.lastFormat.codecs).isEqualTo(MimeTypes.TEXT_VTT);
    assertThat(trackOutput.getSampleCount()).isEqualTo(4);
    // Check sample timestamps.
    assertThat(trackOutput.getSampleTimeUs(0)).isEqualTo(2_345_000L);
    assertThat(trackOutput.getSampleTimeUs(1)).isEqualTo(2_600_000L);
    assertThat(trackOutput.getSampleTimeUs(2)).isEqualTo(3_456_000L);
    assertThat(trackOutput.getSampleTimeUs(3)).isEqualTo(4_567_000L);
    // Check sample content.
    List<Cue> cues0 = decoder.decode(trackOutput.getSampleData(0));
    assertThat(cues0).hasSize(1);
    assertThat(cues0.get(0).text.toString()).isEqualTo("This is the second subtitle.");
    List<Cue> cues1 = decoder.decode(trackOutput.getSampleData(1));
    assertThat(cues1).hasSize(2);
    assertThat(cues1.get(0).text.toString()).isEqualTo("This is the second subtitle.");
    assertThat(cues1.get(1).text.toString()).isEqualTo("This is the third subtitle.");
    List<Cue> cues2 = decoder.decode(trackOutput.getSampleData(2));
    assertThat(cues2).hasSize(1);
    assertThat(cues2.get(0).text.toString()).isEqualTo("This is the third subtitle.");
    List<Cue> cues3 = decoder.decode(trackOutput.getSampleData(3));
    assertThat(cues3).isEmpty();
}
Also used : FakeExtractorInput(androidx.media3.test.utils.FakeExtractorInput) Cue(androidx.media3.common.text.Cue) Format(androidx.media3.common.Format) FakeTrackOutput(androidx.media3.test.utils.FakeTrackOutput) WebvttDecoder(androidx.media3.extractor.text.webvtt.WebvttDecoder) FakeExtractorOutput(androidx.media3.test.utils.FakeExtractorOutput) Test(org.junit.Test)

Example 13 with Cue

use of androidx.media3.common.text.Cue in project media by androidx.

the class SsaDecoderTest method decodePositions.

@Test
public void decodePositions() throws IOException {
    SsaDecoder decoder = new SsaDecoder();
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), POSITIONS);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, false);
    // Check \pos() sets position & line
    Cue firstCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    assertThat(firstCue.position).isEqualTo(0.5f);
    assertThat(firstCue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION);
    assertThat(firstCue.line).isEqualTo(0.25f);
    // Check the \pos() doesn't need to be at the start of the line.
    Cue secondCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(2)));
    assertThat(secondCue.position).isEqualTo(0.25f);
    assertThat(secondCue.line).isEqualTo(0.25f);
    // Check only the last \pos() value is used.
    Cue thirdCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(4)));
    assertThat(thirdCue.position).isEqualTo(0.25f);
    // Check \move() is treated as \pos()
    Cue fourthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(6)));
    assertThat(fourthCue.position).isEqualTo(0.5f);
    assertThat(fourthCue.line).isEqualTo(0.25f);
    // Check alignment override in a separate brace (to bottom-center) affects textAlignment and
    // both line & position anchors.
    Cue fifthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(8)));
    assertThat(fifthCue.position).isEqualTo(0.5f);
    assertThat(fifthCue.line).isEqualTo(0.5f);
    assertWithMessage("Cue.positionAnchor").that(fifthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_MIDDLE);
    assertThat(fifthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
    assertWithMessage("Cue.textAlignment").that(fifthCue.textAlignment).isEqualTo(Layout.Alignment.ALIGN_CENTER);
    // Check alignment override in the same brace (to top-right) affects textAlignment and both line
    // & position anchors.
    Cue sixthCue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(10)));
    assertThat(sixthCue.position).isEqualTo(0.5f);
    assertThat(sixthCue.line).isEqualTo(0.5f);
    assertWithMessage("Cue.positionAnchor").that(sixthCue.positionAnchor).isEqualTo(Cue.ANCHOR_TYPE_END);
    assertThat(sixthCue.lineAnchor).isEqualTo(Cue.ANCHOR_TYPE_START);
    assertWithMessage("Cue.textAlignment").that(sixthCue.textAlignment).isEqualTo(Layout.Alignment.ALIGN_OPPOSITE);
}
Also used : Subtitle(androidx.media3.extractor.text.Subtitle) Cue(androidx.media3.common.text.Cue) Test(org.junit.Test)

Example 14 with Cue

use of androidx.media3.common.text.Cue in project media by androidx.

the class SsaDecoderTest method decodeEmptyStyleLine.

@Test
public void decodeEmptyStyleLine() throws IOException {
    SsaDecoder decoder = new SsaDecoder();
    byte[] bytes = TestUtil.getByteArray(ApplicationProvider.getApplicationContext(), EMPTY_STYLE_LINE);
    Subtitle subtitle = decoder.decode(bytes, bytes.length, /* reset= */
    false);
    assertThat(subtitle.getEventTimeCount()).isEqualTo(2);
    Cue cue = Iterables.getOnlyElement(subtitle.getCues(subtitle.getEventTime(0)));
    SpannedSubject.assertThat((Spanned) cue.text).hasNoSpans();
    assertThat(cue.textSize).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(cue.textSizeType).isEqualTo(Cue.TYPE_UNSET);
    assertThat(cue.textAlignment).isNull();
    assertThat(cue.positionAnchor).isEqualTo(Cue.TYPE_UNSET);
    assertThat(cue.position).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(cue.size).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(cue.lineAnchor).isEqualTo(Cue.TYPE_UNSET);
    assertThat(cue.line).isEqualTo(Cue.DIMEN_UNSET);
    assertThat(cue.lineType).isEqualTo(Cue.LINE_TYPE_FRACTION);
}
Also used : Subtitle(androidx.media3.extractor.text.Subtitle) Cue(androidx.media3.common.text.Cue) Spanned(android.text.Spanned) Test(org.junit.Test)

Example 15 with Cue

use of androidx.media3.common.text.Cue in project media by androidx.

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(androidx.media3.extractor.text.Subtitle) Test(org.junit.Test)

Aggregations

Cue (androidx.media3.common.text.Cue)58 Test (org.junit.Test)47 Nullable (androidx.annotation.Nullable)12 ArrayList (java.util.ArrayList)12 Subtitle (androidx.media3.extractor.text.Subtitle)11 Spanned (android.text.Spanned)8 Bundle (android.os.Bundle)4 UnderlineSpan (android.text.style.UnderlineSpan)4 ParsableByteArray (androidx.media3.common.util.ParsableByteArray)4 LargeTest (androidx.test.filters.LargeTest)4 List (java.util.List)4 SpannableString (android.text.SpannableString)3 AdPlaybackState (androidx.media3.common.AdPlaybackState)3 Format (androidx.media3.common.Format)3 Player (androidx.media3.common.Player)3 HorizontalTextInVerticalContextSpan (androidx.media3.common.text.HorizontalTextInVerticalContextSpan)3 RubySpan (androidx.media3.common.text.RubySpan)3 FakeExtractorOutput (androidx.media3.test.utils.FakeExtractorOutput)3 Context (android.content.Context)2 Bitmap (android.graphics.Bitmap)2