Generate complete silent audio frames

`SilentAudioGenerator` could output a fractional audio frame, and this could cause downstream components to throw because of trying to read a complete audio frame but only seeing a partial one.

Calculate the output buffer size based on the frame size (which is a no-op for stereo 16-bit audio) and calculate a total number of frames to output then multiple by the frame size.

PiperOrigin-RevId: 494992941
This commit is contained in:
andrewlewis 2022-12-13 13:22:37 +00:00 committed by Ian Baker
parent e4f0b73aa3
commit c17c23d1f1
2 changed files with 7 additions and 5 deletions

View File

@ -16,19 +16,21 @@
package com.google.android.exoplayer2.transformer;
import com.google.android.exoplayer2.C;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
/* package */ final class SilentAudioGenerator {
private static final int DEFAULT_BUFFER_SIZE = 4096;
private static final int DEFAULT_BUFFER_SIZE_FRAMES = 1024;
private final ByteBuffer internalBuffer;
private long remainingBytesToOutput;
public SilentAudioGenerator(long totalDurationUs, long sampleRate, int frameSize) {
remainingBytesToOutput = (sampleRate * frameSize * totalDurationUs) / 1_000_000L;
internalBuffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE).order(ByteOrder.nativeOrder());
remainingBytesToOutput = frameSize * ((sampleRate * totalDurationUs) / C.MICROS_PER_SECOND);
internalBuffer =
ByteBuffer.allocate(DEFAULT_BUFFER_SIZE_FRAMES * frameSize).order(ByteOrder.nativeOrder());
internalBuffer.flip();
}

View File

@ -370,8 +370,8 @@ sample:
presentationTimeUs = 998459
sample:
trackIndex = 1
dataHashCode = -1029274849
size = 409
dataHashCode = -587391743
size = 408
isKeyFrame = true
presentationTimeUs = 1021679
sample: