WAV: Don't output data beyond the data limit

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=211446207
This commit is contained in:
olly 2018-09-04 06:20:00 -07:00 committed by Oliver Woodman
parent 29ab6f7466
commit 80e64e58ec
4 changed files with 27 additions and 8 deletions

View File

@ -2,6 +2,8 @@
### dev-v2 (not yet released) ### ### dev-v2 (not yet released) ###
* WAV: Fix issue where white noise would be output at the end of playback
([#4724](https://github.com/google/ExoPlayer/issues/4724)).
* Add a flag to opt-in to automatic audio focus handling via * Add a flag to opt-in to automatic audio focus handling via
`SimpleExoPlayer.setAudioAttributes`. `SimpleExoPlayer.setAudioAttributes`.
* Distribute Cronet extension via jCenter. * Distribute Cronet extension via jCenter.

View File

@ -24,6 +24,7 @@ import com.google.android.exoplayer2.extractor.ExtractorOutput;
import com.google.android.exoplayer2.extractor.ExtractorsFactory; import com.google.android.exoplayer2.extractor.ExtractorsFactory;
import com.google.android.exoplayer2.extractor.PositionHolder; import com.google.android.exoplayer2.extractor.PositionHolder;
import com.google.android.exoplayer2.extractor.TrackOutput; import com.google.android.exoplayer2.extractor.TrackOutput;
import com.google.android.exoplayer2.util.Assertions;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
import java.io.IOException; import java.io.IOException;
@ -88,7 +89,16 @@ public final class WavExtractor implements Extractor {
extractorOutput.seekMap(wavHeader); extractorOutput.seekMap(wavHeader);
} }
int bytesAppended = trackOutput.sampleData(input, MAX_INPUT_SIZE - pendingBytes, true); long dataLimit = wavHeader.getDataLimit();
Assertions.checkState(dataLimit != C.POSITION_UNSET);
long bytesLeft = dataLimit - input.getPosition();
if (bytesLeft <= 0) {
return Extractor.RESULT_END_OF_INPUT;
}
int maxBytesToRead = (int) Math.min(MAX_INPUT_SIZE - pendingBytes, bytesLeft);
int bytesAppended = trackOutput.sampleData(input, maxBytesToRead, true);
if (bytesAppended != RESULT_END_OF_INPUT) { if (bytesAppended != RESULT_END_OF_INPUT) {
pendingBytes += bytesAppended; pendingBytes += bytesAppended;
} }

View File

@ -52,7 +52,7 @@ import com.google.android.exoplayer2.util.Util;
this.encoding = encoding; this.encoding = encoding;
} }
// Setting bounds. // Data bounds.
/** /**
* Sets the data start position and size in bytes of sample data in this WAV. * Sets the data start position and size in bytes of sample data in this WAV.
@ -65,6 +65,11 @@ import com.google.android.exoplayer2.util.Util;
this.dataSize = dataSize; this.dataSize = dataSize;
} }
/** Returns the data limit, or {@link C#POSITION_UNSET} if the data bounds have not been set. */
public long getDataLimit() {
return hasDataBounds() ? (dataStartPosition + dataSize) : C.POSITION_UNSET;
}
/** Returns whether the data start position and size have been set. */ /** Returns whether the data start position and size have been set. */
public boolean hasDataBounds() { public boolean hasDataBounds() {
return dataStartPosition != 0 && dataSize != 0; return dataStartPosition != 0 && dataSize != 0;

View File

@ -97,13 +97,15 @@ import java.io.IOException;
} }
/** /**
* Skips to the data in the given WAV input stream and returns its data size. After calling, the * Skips to the data in the given WAV input stream. After calling, the input stream's position
* input stream's position will point to the start of sample data in the WAV. * will point to the start of sample data in the WAV, and the data bounds of the provided {@link
* <p> * WavHeader} will have been set.
* If an exception is thrown, the input position will be left pointing to a chunk header.
* *
* @param input Input stream to skip to the data chunk in. Its peek position must be pointing to * <p>If an exception is thrown, the input position will be left pointing to a chunk header and
* a valid chunk header. * the bounds of the provided {@link WavHeader} will not have been set.
*
* @param input Input stream to skip to the data chunk in. Its peek position must be pointing to a
* valid chunk header.
* @param wavHeader WAV header to populate with data bounds. * @param wavHeader WAV header to populate with data bounds.
* @throws ParserException If an error occurs parsing chunks. * @throws ParserException If an error occurs parsing chunks.
* @throws IOException If reading from the input fails. * @throws IOException If reading from the input fails.