Skip to content

Commit

Permalink
Shorten data length if it exceeds length of input
Browse files Browse the repository at this point in the history
Issue: #6241
PiperOrigin-RevId: 261126968
  • Loading branch information
ojw28 committed Aug 1, 2019
1 parent 6d20a5c commit f5e9213
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 22 deletions.
2 changes: 2 additions & 0 deletions RELEASENOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
tags instead of 3-letter ISO 639-2 language tags.
* Ensure the `SilenceMediaSource` position is in range
([#6229](https:/google/ExoPlayer/issues/6229)).
* Calculate correct duration for clipped WAV streams
([#6241](https:/google/ExoPlayer/issues/6241)).
* Fix issue where initial seek positions get ignored when playing a preroll ad
([#6201](https:/google/ExoPlayer/issues/6201)).
* Fix issue where invalid language tags were normalized to "und" instead of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ public int read(ExtractorInput input, PositionHolder seekPosition)
input.skipFully(wavHeader.getDataStartPosition());
}

long dataLimit = wavHeader.getDataLimit();
Assertions.checkState(dataLimit != C.POSITION_UNSET);
long dataEndPosition = wavHeader.getDataEndPosition();
Assertions.checkState(dataEndPosition != C.POSITION_UNSET);

long bytesLeft = dataLimit - input.getPosition();
long bytesLeft = dataEndPosition - input.getPosition();
if (bytesLeft <= 0) {
return Extractor.RESULT_END_OF_INPUT;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,29 @@
private final int blockAlignment;
/** Bits per sample for the audio data. */
private final int bitsPerSample;
/** The PCM encoding */
@C.PcmEncoding
private final int encoding;
/** The PCM encoding. */
@C.PcmEncoding private final int encoding;

/** Position of the start of the sample data, in bytes. */
private int dataStartPosition;
/** Total size of the sample data, in bytes. */
private long dataSize;

public WavHeader(int numChannels, int sampleRateHz, int averageBytesPerSecond, int blockAlignment,
int bitsPerSample, @C.PcmEncoding int encoding) {
/** Position of the end of the sample data (exclusive), in bytes. */
private long dataEndPosition;

public WavHeader(
int numChannels,
int sampleRateHz,
int averageBytesPerSecond,
int blockAlignment,
int bitsPerSample,
@C.PcmEncoding int encoding) {
this.numChannels = numChannels;
this.sampleRateHz = sampleRateHz;
this.averageBytesPerSecond = averageBytesPerSecond;
this.blockAlignment = blockAlignment;
this.bitsPerSample = bitsPerSample;
this.encoding = encoding;
dataStartPosition = C.POSITION_UNSET;
dataEndPosition = C.POSITION_UNSET;
}

// Data bounds.
Expand All @@ -59,11 +64,11 @@ public WavHeader(int numChannels, int sampleRateHz, int averageBytesPerSecond, i
* Sets the data start position and size in bytes of sample data in this WAV.
*
* @param dataStartPosition The position of the start of the sample data, in bytes.
* @param dataSize The total size of the sample data, in bytes.
* @param dataEndPosition The position of the end of the sample data (exclusive), in bytes.
*/
public void setDataBounds(int dataStartPosition, long dataSize) {
public void setDataBounds(int dataStartPosition, long dataEndPosition) {
this.dataStartPosition = dataStartPosition;
this.dataSize = dataSize;
this.dataEndPosition = dataEndPosition;
}

/**
Expand All @@ -75,11 +80,11 @@ public int getDataStartPosition() {
}

/**
* Returns the limit of the sample data, in bytes, or {@link C#POSITION_UNSET} if the data bounds
* have not been set.
* Returns the position of the end of the sample data (exclusive), in bytes, or {@link
* C#POSITION_UNSET} if the data bounds have not been set.
*/
public long getDataLimit() {
return hasDataBounds() ? (dataStartPosition + dataSize) : C.POSITION_UNSET;
public long getDataEndPosition() {
return dataEndPosition;
}

/** Returns whether the data start position and size have been set. */
Expand All @@ -96,12 +101,13 @@ public boolean isSeekable() {

@Override
public long getDurationUs() {
long numFrames = dataSize / blockAlignment;
long numFrames = (dataEndPosition - dataStartPosition) / blockAlignment;
return (numFrames * C.MICROS_PER_SECOND) / sampleRateHz;
}

@Override
public SeekPoints getSeekPoints(long timeUs) {
long dataSize = dataEndPosition - dataStartPosition;
long positionOffset = (timeUs * averageBytesPerSecond) / C.MICROS_PER_SECOND;
// Constrain to nearest preceding frame offset.
positionOffset = (positionOffset / blockAlignment) * blockAlignment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ public static WavHeader peek(ExtractorInput input) throws IOException, Interrupt
// If present, skip extensionSize, validBitsPerSample, channelMask, subFormatGuid, ...
input.advancePeekPosition((int) chunkHeader.size - 16);

return new WavHeader(numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment,
bitsPerSample, encoding);
return new WavHeader(
numChannels, sampleRateHz, averageBytesPerSecond, blockAlignment, bitsPerSample, encoding);
}

/**
Expand Down Expand Up @@ -139,7 +139,14 @@ public static void skipToData(ExtractorInput input, WavHeader wavHeader)
// Skip past the "data" header.
input.skipFully(ChunkHeader.SIZE_IN_BYTES);

wavHeader.setDataBounds((int) input.getPosition(), chunkHeader.size);
int dataStartPosition = (int) input.getPosition();
long dataEndPosition = dataStartPosition + chunkHeader.size;
long inputLength = input.getLength();
if (inputLength != C.LENGTH_UNSET && dataEndPosition > inputLength) {
Log.w(TAG, "Data exceeds input length: " + dataEndPosition + ", " + inputLength);
dataEndPosition = inputLength;
}
wavHeader.setDataBounds(dataStartPosition, dataEndPosition);
}

private WavHeaderReader() {
Expand Down

0 comments on commit f5e9213

Please sign in to comment.