Verify the audio properties being used within Transformer.

Add TransformerAudioEndToEndTest to emulated tests.

PiperOrigin-RevId: 514991434
This commit is contained in:
samrobinson 2023-03-08 11:30:04 +00:00 committed by tonihei
parent bb9ac21434
commit 73e4348411

View File

@ -25,8 +25,12 @@ import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.MediaItem;
import com.google.android.exoplayer2.audio.AudioProcessor; import com.google.android.exoplayer2.audio.AudioProcessor;
import com.google.android.exoplayer2.audio.AudioProcessor.AudioFormat;
import com.google.android.exoplayer2.audio.TeeAudioProcessor;
import com.google.android.exoplayer2.audio.ToInt16PcmAudioProcessor; import com.google.android.exoplayer2.audio.ToInt16PcmAudioProcessor;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.nio.ByteBuffer;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -38,6 +42,31 @@ import org.junit.runner.RunWith;
public class TransformerAudioEndToEndTest { public class TransformerAudioEndToEndTest {
private final Context context = ApplicationProvider.getApplicationContext(); private final Context context = ApplicationProvider.getApplicationContext();
@Test
public void defaultTransformer_processesInInt16Pcm() throws Exception {
String testId = "defaultTransformer_processesInInt16Pcm";
FormatTrackingAudioBufferSink audioFormatTracker = new FormatTrackingAudioBufferSink();
Transformer transformer =
new Transformer.Builder(context)
.setEncoderFactory(new AndroidTestUtil.ForceEncodeEncoderFactory(context))
.build();
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setEffects(createForAudioProcessors(new TeeAudioProcessor(audioFormatTracker)))
.setRemoveVideo(true)
.build();
new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);
ImmutableList<AudioFormat> audioFormats = audioFormatTracker.getFlushedAudioFormats().asList();
assertThat(audioFormats).hasSize(1);
assertThat(audioFormats.get(0).encoding).isEqualTo(C.ENCODING_PCM_16BIT);
}
@Test @Test
public void mixMonoToStereo_outputsStereo() throws Exception { public void mixMonoToStereo_outputsStereo() throws Exception {
String testId = "mixMonoToStereo_outputsStereo"; String testId = "mixMonoToStereo_outputsStereo";
@ -62,53 +91,83 @@ public class TransformerAudioEndToEndTest {
} }
@Test @Test
public void mixingChannels_outputsFloatPcm() throws Exception { public void channelMixing_outputsFloatPcm() throws Exception {
final String testId = "mixingChannels_outputsFloatPcm"; final String testId = "channelMixing_outputsFloatPcm";
FormatTrackingAudioBufferSink audioFormatTracker = new FormatTrackingAudioBufferSink();
Effects effects =
createForAudioProcessors(
new ChannelMixingAudioProcessor(
ChannelMixingMatrix.create(
/* inputChannelCount= */ 1, /* outputChannelCount= */ 2)));
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setRemoveVideo(true)
.setEffects(effects)
.build();
ExportTestResult result =
new TransformerAndroidTestRunner.Builder(context, new Transformer.Builder(context).build())
.build()
.run(testId, editedMediaItem);
assertThat(result.exportResult.pcmEncoding).isEqualTo(C.ENCODING_PCM_FLOAT);
}
@Test
public void mixChannelsThenToInt16Pcm_outputsInt16Pcm() throws Exception {
final String testId = "mixChannelsThenToInt16Pcm_outputsInt16Pcm";
Effects effects = Effects effects =
createForAudioProcessors( createForAudioProcessors(
new ChannelMixingAudioProcessor( new ChannelMixingAudioProcessor(
ChannelMixingMatrix.create( ChannelMixingMatrix.create(
/* inputChannelCount= */ 1, /* outputChannelCount= */ 2)), /* inputChannelCount= */ 1, /* outputChannelCount= */ 2)),
new ToInt16PcmAudioProcessor()); new TeeAudioProcessor(audioFormatTracker));
EditedMediaItem editedMediaItem = EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING))) new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setRemoveVideo(true) .setRemoveVideo(true)
.setEffects(effects) .setEffects(effects)
.build(); .build();
ExportTestResult result =
new TransformerAndroidTestRunner.Builder(context, new Transformer.Builder(context).build()) new TransformerAndroidTestRunner.Builder(context, new Transformer.Builder(context).build())
.build() .build()
.run(testId, editedMediaItem); .run(testId, editedMediaItem);
assertThat(result.exportResult.pcmEncoding).isEqualTo(C.ENCODING_PCM_16BIT); ImmutableList<AudioFormat> audioFormats = audioFormatTracker.getFlushedAudioFormats().asList();
assertThat(audioFormats).hasSize(1);
assertThat(audioFormats.get(0).encoding).isEqualTo(C.ENCODING_PCM_FLOAT);
}
@Test
public void channelMixingThenToInt16Pcm_outputsInt16Pcm() throws Exception {
final String testId = "channelMixingThenToInt16Pcm_outputsInt16Pcm";
FormatTrackingAudioBufferSink audioFormatTracker = new FormatTrackingAudioBufferSink();
Effects effects =
createForAudioProcessors(
new ChannelMixingAudioProcessor(
ChannelMixingMatrix.create(
/* inputChannelCount= */ 1, /* outputChannelCount= */ 2)),
new ToInt16PcmAudioProcessor(),
new TeeAudioProcessor(audioFormatTracker));
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setRemoveVideo(true)
.setEffects(effects)
.build();
new TransformerAndroidTestRunner.Builder(context, new Transformer.Builder(context).build())
.build()
.run(testId, editedMediaItem);
ImmutableList<AudioFormat> audioFormats = audioFormatTracker.getFlushedAudioFormats().asList();
assertThat(audioFormats).hasSize(1);
assertThat(audioFormats.get(0).encoding).isEqualTo(C.ENCODING_PCM_16BIT);
} }
private static Effects createForAudioProcessors(AudioProcessor... audioProcessors) { private static Effects createForAudioProcessors(AudioProcessor... audioProcessors) {
return new Effects(ImmutableList.copyOf(audioProcessors), ImmutableList.of()); return new Effects(ImmutableList.copyOf(audioProcessors), ImmutableList.of());
} }
private static final class FormatTrackingAudioBufferSink
implements TeeAudioProcessor.AudioBufferSink {
private final ImmutableSet.Builder<AudioFormat> flushedAudioFormats;
public FormatTrackingAudioBufferSink() {
this.flushedAudioFormats = new ImmutableSet.Builder<>();
}
@Override
public void flush(int sampleRateHz, int channelCount, @C.PcmEncoding int encoding) {
flushedAudioFormats.add(new AudioFormat(sampleRateHz, channelCount, encoding));
}
@Override
public void handleBuffer(ByteBuffer buffer) {
// Do nothing.
}
public ImmutableSet<AudioFormat> getFlushedAudioFormats() {
return flushedAudioFormats.build();
}
}
} }