Skip to content

Commit

Permalink
Fix index out of bounds exception when a Subtitle is empty
Browse files Browse the repository at this point in the history
Issue: #1516

#cherrypick

PiperOrigin-RevId: 648416119
  • Loading branch information
icbaker authored and copybara-github committed Jul 1, 2024
1 parent 70c0639 commit 711d18d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
3 changes: 3 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
* Audio:
* Video:
* Text:
* Fix an `IllegalArgumentException` from `LegacySubtitleUtil` when a
WebVTT subtitle sample contains no cues, e.g. as part of a DASH stream
([#1516](https://github.com/androidx/media/issues/1516)).
* Metadata:
* Image:
* DRM:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ private LegacySubtitleUtil() {}
* Converts a {@link Subtitle} to a list of {@link CuesWithTiming} representing it, emitted to
* {@code output}.
*
* <p>This may only be called with {@link Subtitle} instances where the first event is non-empty
* and the last event is an empty cue list.
* <p>This may only be called with empty {@link Subtitle} instances, or those where the first
* event is non-empty and the last event is an empty cue list.
*/
public static void toCuesWithTiming(
Subtitle subtitle, OutputOptions outputOptions, Consumer<CuesWithTiming> output) {
if (subtitle.getEventTimeCount() == 0) {
return;
}
int startIndex = getStartIndex(subtitle, outputOptions);
boolean startedInMiddleOfCue = false;
if (outputOptions.startTimeUs != C.TIME_UNSET) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public class LegacySubtitleUtilWebvttTest {
private static final String FIRST_SUBTITLE_STRING = "This is the first subtitle.";
private static final String SECOND_SUBTITLE_STRING = "This is the second subtitle.";

private static final WebvttSubtitle EMPTY_SUBTITLE = new WebvttSubtitle(ImmutableList.of());

private static final WebvttSubtitle SIMPLE_SUBTITLE =
new WebvttSubtitle(
Arrays.asList(
Expand Down Expand Up @@ -135,6 +137,14 @@ public void toCuesWithTiming_allCues_overlappingSubtitle() {
.containsExactly(SECOND_SUBTITLE_STRING);
}

@Test
public void toCuesWithTiming_allCues_emptySubtitle() {
ImmutableList<CuesWithTiming> cuesWithTimingsList =
toCuesWithTimingList(EMPTY_SUBTITLE, SubtitleParser.OutputOptions.allCues());

assertThat(cuesWithTimingsList).isEmpty();
}

@Test
public void toCuesWithTiming_onlyEmitCuesAfterStartTime_startBetweenCues_simpleSubtitle() {
ImmutableList<CuesWithTiming> cuesWithTimingsList =
Expand Down Expand Up @@ -252,6 +262,14 @@ public void toCuesWithTiming_onlyEmitCuesAfterStartTime_overlappingSubtitle() {
.containsExactly(SECOND_SUBTITLE_STRING);
}

@Test
public void toCuesWithTiming_onlyEmitCuesAfterStartTime_emptySubtitle() {
ImmutableList<CuesWithTiming> cuesWithTimingsList =
toCuesWithTimingList(EMPTY_SUBTITLE, SubtitleParser.OutputOptions.onlyCuesAfter(0));

assertThat(cuesWithTimingsList).isEmpty();
}

@Test
public void
toCuesWithTiming_emitCuesAfterStartTimeThenThoseBefore_startAtStartOfCue_simpleSubtitle() {
Expand Down Expand Up @@ -405,6 +423,14 @@ public void toCuesWithTiming_emitCuesAfterStartTimeThenThoseBefore_overlappingSu
.inOrder();
}

@Test
public void toCuesWithTiming_emitCuesAfterStartTimeThenThoseBefore_emptySubtitle() {
ImmutableList<CuesWithTiming> cuesWithTimingsList =
toCuesWithTimingList(EMPTY_SUBTITLE, SubtitleParser.OutputOptions.onlyCuesAfter(0));

assertThat(cuesWithTimingsList).isEmpty();
}

private static ImmutableList<CuesWithTiming> toCuesWithTimingList(
Subtitle subtitle, SubtitleParser.OutputOptions outputOptions) {
ImmutableList.Builder<CuesWithTiming> result = ImmutableList.builder();
Expand Down

0 comments on commit 711d18d

Please sign in to comment.