Skip to content

Commit

Permalink
Fix handling of MP3s with appended data
Browse files Browse the repository at this point in the history
Issue: #4954

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=218357113
  • Loading branch information
andrewlewis authored and ojw28 committed Oct 24, 2018
1 parent 13e0513 commit e6b49a5
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 17 deletions.
7 changes: 5 additions & 2 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
network type.
* SubRip: Add support for alignment tags, and remove tags from the displayed
captions ([#4306](https://github.com/google/ExoPlayer/issues/4306)).
* MP3: Support seeking based on MLLT metadata
([#3241](https://github.com/google/ExoPlayer/issues/3241)).
* MP3:
* Support seeking based on MLLT metadata
([#3241](https://github.com/google/ExoPlayer/issues/3241)).
* Fix handling of streams with appending data
([#4954](https://github.com/google/ExoPlayer/issues/4954)).
* IMA extension: For preroll to live stream transitions, project forward the
loading position to avoid being behind the live window.
* Fix issue with blind seeking to windows with non-zero offset in a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,9 @@ public ConstantBitrateSeeker(
public long getTimeUs(long position) {
return getTimeUsAtPosition(position);
}

@Override
public long getDataEndPosition() {
return C.POSITION_UNSET;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,8 @@ private static Pair<Long, Long> linearlyInterpolate(
}
}

@Override
public long getDataEndPosition() {
return C.POSITION_UNSET;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public int read(ExtractorInput input, PositionHolder seekPosition)
private int readSample(ExtractorInput extractorInput) throws IOException, InterruptedException {
if (sampleBytesRemaining == 0) {
extractorInput.resetPeekPosition();
if (!extractorInput.peekFully(scratch.data, 0, 4, true)) {
if (peekEndOfStreamOrHeader(extractorInput)) {
return RESULT_END_OF_INPUT;
}
scratch.setPosition(0);
Expand Down Expand Up @@ -285,9 +285,12 @@ private boolean synchronize(ExtractorInput input, boolean sniffing)
}
}
while (true) {
if (!input.peekFully(scratch.data, 0, 4, validFrameCount > 0)) {
// We reached the end of the stream but found at least one valid frame.
break;
if (peekEndOfStreamOrHeader(input)) {
if (validFrameCount > 0) {
// We reached the end of the stream but found at least one valid frame.
break;
}
throw new EOFException();
}
scratch.setPosition(0);
int headerData = scratch.readInt();
Expand Down Expand Up @@ -332,6 +335,17 @@ private boolean synchronize(ExtractorInput input, boolean sniffing)
return true;
}

/**
* Returns whether the extractor input is peeking the end of the stream. If {@code false},
* populates the scratch buffer with the next four bytes.
*/
private boolean peekEndOfStreamOrHeader(ExtractorInput extractorInput)
throws IOException, InterruptedException {
return (seeker != null && extractorInput.getPeekPosition() == seeker.getDataEndPosition())
|| !extractorInput.peekFully(
scratch.data, /* offset= */ 0, /* length= */ 4, /* allowEndOfInput= */ true);
}

/**
* Consumes the next frame from the {@code input} if it contains VBRI or Xing seeking metadata,
* returning a {@link Seeker} if the metadata was present and valid, or {@code null} otherwise.
Expand Down Expand Up @@ -433,8 +447,9 @@ private static MlltSeeker maybeHandleSeekMetadata(Metadata metadata, long firstF
}

/**
* {@link SeekMap} that also allows mapping from position (byte offset) back to time, which can be
* used to work out the new sample basis timestamp after seeking and resynchronization.
* {@link SeekMap} that provides the end position of audio data and also allows mapping from
* position (byte offset) back to time, which can be used to work out the new sample basis
* timestamp after seeking and resynchronization.
*/
/* package */ interface Seeker extends SeekMap {

Expand All @@ -446,6 +461,11 @@ private static MlltSeeker maybeHandleSeekMetadata(Metadata metadata, long firstF
*/
long getTimeUs(long position);

/**
* Returns the position (byte offset) in the stream that is immediately after audio data, or
* {@link C#POSITION_UNSET} if not known.
*/
long getDataEndPosition();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,19 @@
if (inputLength != C.LENGTH_UNSET && inputLength != position) {
Log.w(TAG, "VBRI data size mismatch: " + inputLength + ", " + position);
}
return new VbriSeeker(timesUs, positions, durationUs);
return new VbriSeeker(timesUs, positions, durationUs, /* dataEndPosition= */ position);
}

private final long[] timesUs;
private final long[] positions;
private final long durationUs;
private final long dataEndPosition;

private VbriSeeker(long[] timesUs, long[] positions, long durationUs) {
private VbriSeeker(long[] timesUs, long[] positions, long durationUs, long dataEndPosition) {
this.timesUs = timesUs;
this.positions = positions;
this.durationUs = durationUs;
this.dataEndPosition = dataEndPosition;
}

@Override
Expand Down Expand Up @@ -129,4 +131,8 @@ public long getDurationUs() {
return durationUs;
}

@Override
public long getDataEndPosition() {
return dataEndPosition;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,30 @@
if (inputLength != C.LENGTH_UNSET && inputLength != position + dataSize) {
Log.w(TAG, "XING data size mismatch: " + inputLength + ", " + (position + dataSize));
}
return new XingSeeker(position, mpegAudioHeader.frameSize, durationUs, dataSize,
tableOfContents);
return new XingSeeker(
position, mpegAudioHeader.frameSize, durationUs, dataSize, tableOfContents);
}

private final long dataStartPosition;
private final int xingFrameSize;
private final long durationUs;
/**
* Data size, including the XING frame.
*/
/** Data size, including the XING frame. */
private final long dataSize;

private final long dataEndPosition;
/**
* Entries are in the range [0, 255], but are stored as long integers for convenience. Null if the
* table of contents was missing from the header, in which case seeking is not be supported.
*/
private final @Nullable long[] tableOfContents;

private XingSeeker(long dataStartPosition, int xingFrameSize, long durationUs) {
this(dataStartPosition, xingFrameSize, durationUs, C.LENGTH_UNSET, null);
this(
dataStartPosition,
xingFrameSize,
durationUs,
/* dataSize= */ C.LENGTH_UNSET,
/* tableOfContents= */ null);
}

private XingSeeker(
Expand All @@ -105,8 +110,9 @@ private XingSeeker(
this.dataStartPosition = dataStartPosition;
this.xingFrameSize = xingFrameSize;
this.durationUs = durationUs;
this.dataSize = dataSize;
this.tableOfContents = tableOfContents;
this.dataSize = dataSize;
dataEndPosition = dataSize == C.LENGTH_UNSET ? C.POSITION_UNSET : dataStartPosition + dataSize;
}

@Override
Expand Down Expand Up @@ -166,6 +172,11 @@ public long getDurationUs() {
return durationUs;
}

@Override
public long getDataEndPosition() {
return dataEndPosition;
}

/**
* Returns the time in microseconds for a given table index.
*
Expand Down

0 comments on commit e6b49a5

Please sign in to comment.