Commit dfc69dee by samrobinson Committed by Marc Baechinger

Use AudioFormat in SilentAudioGenerator.

PiperOrigin-RevId: 521790733
parent da0324e7
......@@ -18,7 +18,7 @@ package com.google.android.exoplayer2.transformer;
import static com.google.android.exoplayer2.decoder.DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DIRECT;
import static com.google.android.exoplayer2.decoder.DecoderInputBuffer.BUFFER_REPLACEMENT_MODE_DISABLED;
import static com.google.android.exoplayer2.transformer.DefaultCodec.DEFAULT_PCM_ENCODING;
import static com.google.android.exoplayer2.util.Assertions.checkArgument;
import static com.google.android.exoplayer2.util.Assertions.checkNotNull;
import static com.google.android.exoplayer2.util.Assertions.checkState;
import static java.lang.Math.min;
......@@ -71,7 +71,6 @@ import org.checkerframework.dataflow.qual.Pure;
throws ExportException {
super(firstAssetLoaderInputFormat, muxerWrapper);
silentAudioGenerator = new SilentAudioGenerator(firstPipelineInputFormat);
availableInputBuffers = new ConcurrentLinkedDeque<>();
ByteBuffer emptyBuffer = ByteBuffer.allocateDirect(0).order(ByteOrder.nativeOrder());
for (int i = 0; i < MAX_INPUT_BUFFER_COUNT; i++) {
......@@ -84,6 +83,15 @@ import org.checkerframework.dataflow.qual.Pure;
encoderInputBuffer = new DecoderInputBuffer(BUFFER_REPLACEMENT_MODE_DISABLED);
encoderOutputBuffer = new DecoderInputBuffer(BUFFER_REPLACEMENT_MODE_DISABLED);
checkArgument(firstPipelineInputFormat.pcmEncoding != Format.NO_VALUE);
AudioFormat inputAudioFormat =
new AudioFormat(
firstPipelineInputFormat.sampleRate,
firstPipelineInputFormat.channelCount,
firstPipelineInputFormat.pcmEncoding);
silentAudioGenerator = new SilentAudioGenerator(inputAudioFormat);
if (flattenForSlowMotion && firstAssetLoaderInputFormat.metadata != null) {
audioProcessors =
new ImmutableList.Builder<AudioProcessor>()
......@@ -95,20 +103,12 @@ import org.checkerframework.dataflow.qual.Pure;
}
audioProcessingPipeline = new AudioProcessingPipeline(audioProcessors);
// TODO(b/267301878): Once decoder format propagated, remove setting default PCM encoding.
AudioFormat pipelineInputAudioFormat =
new AudioFormat(
firstPipelineInputFormat.sampleRate,
firstPipelineInputFormat.channelCount,
firstPipelineInputFormat.pcmEncoding != Format.NO_VALUE
? firstPipelineInputFormat.pcmEncoding
: DEFAULT_PCM_ENCODING);
try {
encoderInputAudioFormat = audioProcessingPipeline.configure(pipelineInputAudioFormat);
encoderInputAudioFormat = audioProcessingPipeline.configure(inputAudioFormat);
} catch (AudioProcessor.UnhandledAudioFormatException unhandledAudioFormatException) {
throw ExportException.createForAudioProcessing(
unhandledAudioFormatException, pipelineInputAudioFormat);
unhandledAudioFormatException, inputAudioFormat);
}
audioProcessingPipeline.flush();
......
......@@ -17,7 +17,7 @@
package com.google.android.exoplayer2.transformer;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat;
import com.google.android.exoplayer2.util.Util;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
......@@ -31,12 +31,9 @@ import java.util.concurrent.atomic.AtomicLong;
private final ByteBuffer internalBuffer;
private final AtomicLong remainingBytesToOutput;
public SilentAudioGenerator(Format format) {
public SilentAudioGenerator(AudioFormat format) {
sampleRate = format.sampleRate;
frameSize =
Util.getPcmFrameSize(
format.pcmEncoding == Format.NO_VALUE ? C.ENCODING_PCM_16BIT : format.pcmEncoding,
format.channelCount);
frameSize = Util.getPcmFrameSize(format.encoding, format.channelCount);
internalBuffer =
ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE_FRAMES * frameSize)
.order(ByteOrder.nativeOrder());
......
......@@ -19,7 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.Format;
import com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat;
import java.nio.ByteBuffer;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -32,11 +32,7 @@ public class SilentAudioGeneratorTest {
public void addSilenceOnce_numberOfBytesProduced_isCorrect() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
new Format.Builder()
.setSampleRate(88_200)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(6)
.build());
new AudioFormat(/* sampleRate= */ 88_200, /* channelCount= */ 6, C.ENCODING_PCM_16BIT));
generator.addSilence(/* durationUs= */ 3_000_000);
int bytesOutput = drainGenerator(generator);
......@@ -49,11 +45,7 @@ public class SilentAudioGeneratorTest {
public void addSilenceTwice_numberOfBytesProduced_isCorrect() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
new Format.Builder()
.setSampleRate(88_200)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(6)
.build());
new AudioFormat(/* sampleRate= */ 88_200, /* channelCount= */ 6, C.ENCODING_PCM_16BIT));
generator.addSilence(/* durationUs= */ 3_000_000);
int bytesOutput = drainGenerator(generator);
......@@ -68,11 +60,8 @@ public class SilentAudioGeneratorTest {
public void lastBufferProduced_isCorrectSize() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
new Format.Builder()
.setSampleRate(44_100)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(2)
.build());
new AudioFormat(/* sampleRate= */ 44_100, /* channelCount= */ 2, C.ENCODING_PCM_16BIT));
generator.addSilence(/* durationUs= */ 1_000_000);
int currentBufferSize = 0;
......@@ -92,11 +81,7 @@ public class SilentAudioGeneratorTest {
public void totalBytesLowerThanDefaultBufferSize_smallBufferProduced() {
SilentAudioGenerator generator =
new SilentAudioGenerator(
new Format.Builder()
.setSampleRate(48_000)
.setPcmEncoding(C.ENCODING_PCM_16BIT)
.setChannelCount(2)
.build());
new AudioFormat(/* sampleRate= */ 48_000, /* channelCount= */ 2, C.ENCODING_PCM_16BIT));
generator.addSilence(/* durationUs= */ 5_000);
......
......@@ -115,7 +115,9 @@ public final class TestUtil {
.setChannelCount(2)
.build();
try {
listener.onTrackAdded(format, supportedOutputTypes);
if (listener.onTrackAdded(format, supportedOutputTypes)) {
format = format.buildUpon().setPcmEncoding(C.ENCODING_PCM_16BIT).build();
}
SampleConsumer sampleConsumer = listener.onOutputFormat(format);
if (sampleConsumerRef != null) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment