Skip to content

Commit

Permalink
Fix 2 CEA decoder bugs
Browse files Browse the repository at this point in the history
1- Avoid dropped buffers by using a PriorityQueue instead of a set.
2- Process the end of stream after non-EOS buffers.

Issue:#3250

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=169077365
  • Loading branch information
AquilesCanta authored and ojw28 committed Sep 19, 2017
1 parent 74b67d3 commit e9baef6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public SubtitleInputBuffer() {

@Override
public int compareTo(@NonNull SubtitleInputBuffer other) {
if (isEndOfStream() != other.isEndOfStream()) {
return isEndOfStream() ? 1 : -1;
}
long delta = timeUs - other.timeUs;
if (delta == 0) {
return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ private void replaceDecoder() {
}

private long getNextEventTime() {
return ((nextSubtitleEventIndex == C.INDEX_UNSET)
|| (nextSubtitleEventIndex >= subtitle.getEventTimeCount())) ? Long.MAX_VALUE
: (subtitle.getEventTime(nextSubtitleEventIndex));
return nextSubtitleEventIndex == C.INDEX_UNSET
|| nextSubtitleEventIndex >= subtitle.getEventTimeCount()
? Long.MAX_VALUE : subtitle.getEventTime(nextSubtitleEventIndex);
}

private void updateOutput(List<Cue> cues) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.google.android.exoplayer2.text.SubtitleOutputBuffer;
import com.google.android.exoplayer2.util.Assertions;
import java.util.LinkedList;
import java.util.TreeSet;
import java.util.PriorityQueue;

/**
* Base class for subtitle parsers for CEA captions.
Expand All @@ -36,7 +36,7 @@

private final LinkedList<SubtitleInputBuffer> availableInputBuffers;
private final LinkedList<SubtitleOutputBuffer> availableOutputBuffers;
private final TreeSet<SubtitleInputBuffer> queuedInputBuffers;
private final PriorityQueue<SubtitleInputBuffer> queuedInputBuffers;

private SubtitleInputBuffer dequeuedInputBuffer;
private long playbackPositionUs;
Expand All @@ -50,7 +50,7 @@ public CeaDecoder() {
for (int i = 0; i < NUM_OUTPUT_BUFFERS; i++) {
availableOutputBuffers.add(new CeaOutputBuffer(this));
}
queuedInputBuffers = new TreeSet<>();
queuedInputBuffers = new PriorityQueue<>();
}

@Override
Expand All @@ -73,7 +73,6 @@ public SubtitleInputBuffer dequeueInputBuffer() throws SubtitleDecoderException

@Override
public void queueInputBuffer(SubtitleInputBuffer inputBuffer) throws SubtitleDecoderException {
Assertions.checkArgument(inputBuffer != null);
Assertions.checkArgument(inputBuffer == dequeuedInputBuffer);
if (inputBuffer.isDecodeOnly()) {
// We can drop this buffer early (i.e. before it would be decoded) as the CEA formats allow
Expand All @@ -90,13 +89,12 @@ public SubtitleOutputBuffer dequeueOutputBuffer() throws SubtitleDecoderExceptio
if (availableOutputBuffers.isEmpty()) {
return null;
}

// iterate through all available input buffers whose timestamps are less than or equal
// to the current playback position; processing input buffers for future content should
// be deferred until they would be applicable
while (!queuedInputBuffers.isEmpty()
&& queuedInputBuffers.first().timeUs <= playbackPositionUs) {
SubtitleInputBuffer inputBuffer = queuedInputBuffers.pollFirst();
&& queuedInputBuffers.peek().timeUs <= playbackPositionUs) {
SubtitleInputBuffer inputBuffer = queuedInputBuffers.poll();

// If the input buffer indicates we've reached the end of the stream, we can
// return immediately with an output buffer propagating that
Expand Down Expand Up @@ -142,7 +140,7 @@ protected void releaseOutputBuffer(SubtitleOutputBuffer outputBuffer) {
public void flush() {
playbackPositionUs = 0;
while (!queuedInputBuffers.isEmpty()) {
releaseInputBuffer(queuedInputBuffers.pollFirst());
releaseInputBuffer(queuedInputBuffers.poll());
}
if (dequeuedInputBuffer != null) {
releaseInputBuffer(dequeuedInputBuffer);
Expand Down

0 comments on commit e9baef6

Please sign in to comment.