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);
}
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();
}
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);
}
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);
}
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();
}
Aggregations