Skip to content

Commit

Permalink
Merge TransformerAudio E2E tests into Transformer E2E class.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 557113117
  • Loading branch information
Samrobbo authored and microkatz committed Sep 29, 2023
1 parent cd7063c commit 276e6d3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 123 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
import androidx.media3.common.OnInputFrameProcessedListener;
import androidx.media3.common.VideoFrameProcessingException;
import androidx.media3.common.audio.AudioProcessor;
import androidx.media3.common.audio.AudioProcessor.AudioFormat;
import androidx.media3.common.audio.ChannelMixingAudioProcessor;
import androidx.media3.common.audio.ChannelMixingMatrix;
import androidx.media3.common.audio.SonicAudioProcessor;
import androidx.media3.common.util.GlUtil;
import androidx.media3.datasource.DataSourceBitmapLoader;
Expand All @@ -54,9 +57,12 @@
import androidx.media3.effect.Presentation;
import androidx.media3.effect.RgbFilter;
import androidx.media3.effect.TimestampWrapper;
import androidx.media3.exoplayer.audio.TeeAudioProcessor;
import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.nio.ByteBuffer;
import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -627,6 +633,60 @@ public void loopingImage_producesExpectedResult() throws Exception {
assertThat(result.exportResult.durationMs).isEqualTo(3100);
}

@Test
public void audioTranscode_processesInInt16Pcm() throws Exception {
String testId = "audioTranscode_processesInInt16Pcm";
FormatTrackingAudioBufferSink audioFormatTracker = new FormatTrackingAudioBufferSink();

Transformer transformer = new Transformer.Builder(context).build();
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setEffects(
new Effects(
ImmutableList.of(audioFormatTracker.createTeeAudioProcessor()),
/* videoEffects= */ ImmutableList.of()))
.setRemoveVideo(true)
.build();

new TransformerAndroidTestRunner.Builder(context, transformer)
.build()
.run(testId, editedMediaItem);

ImmutableList<AudioFormat> audioFormats = audioFormatTracker.getFlushedAudioFormats();
assertThat(audioFormats).hasSize(1);
assertThat(audioFormats.get(0).encoding).isEqualTo(C.ENCODING_PCM_16BIT);
}

@Test
public void audioEditing_monoToStereo_outputsStereo() throws Exception {
String testId = "audioEditing_monoToStereo_outputsStereo";

ChannelMixingAudioProcessor channelMixingAudioProcessor = new ChannelMixingAudioProcessor();
channelMixingAudioProcessor.putChannelMixingMatrix(
ChannelMixingMatrix.create(/* inputChannelCount= */ 1, /* outputChannelCount= */ 2));
EditedMediaItem editedMediaItem =
new EditedMediaItem.Builder(MediaItem.fromUri(Uri.parse(MP4_ASSET_URI_STRING)))
.setRemoveVideo(true)
.setEffects(
new Effects(
ImmutableList.of(channelMixingAudioProcessor),
/* videoEffects= */ ImmutableList.of()))
.build();

ExportTestResult result =
new TransformerAndroidTestRunner.Builder(context, new Transformer.Builder(context).build())
.build()
.run(testId, editedMediaItem);

assertThat(result.exportResult.channelCount).isEqualTo(2);
}

private static AudioProcessor createSonic(float pitch) {
SonicAudioProcessor sonic = new SonicAudioProcessor();
sonic.setPitch(pitch);
return sonic;
}

private final class TestTextureAssetLoaderFactory implements AssetLoader.Factory {

private final int width;
Expand Down Expand Up @@ -690,9 +750,30 @@ public boolean videoNeedsEncoding() {
}
}

private static AudioProcessor createSonic(float pitch) {
SonicAudioProcessor sonic = new SonicAudioProcessor();
sonic.setPitch(pitch);
return sonic;
private static final class FormatTrackingAudioBufferSink
implements TeeAudioProcessor.AudioBufferSink {
private final ImmutableSet.Builder<AudioFormat> flushedAudioFormats;

public FormatTrackingAudioBufferSink() {
this.flushedAudioFormats = new ImmutableSet.Builder<>();
}

public TeeAudioProcessor createTeeAudioProcessor() {
return new TeeAudioProcessor(this);
}

@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 ImmutableList<AudioFormat> getFlushedAudioFormats() {
return flushedAudioFormats.build().asList();
}
}
}

0 comments on commit 276e6d3

Please sign in to comment.