Merge TransformerAudio E2E tests into Transformer E2E class.

PiperOrigin-RevId: 557113117
This commit is contained in:
samrobinson 2023-08-15 14:24:19 +01:00 committed by oceanjules
parent cacf9b33b6
commit 86b9fdae63
2 changed files with 85 additions and 123 deletions

View File

@ -1,119 +0,0 @@
/*
* Copyright 2023 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package androidx.media3.transformer;
import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_URI_STRING;
import static com.google.common.truth.Truth.assertThat;
import android.content.Context;
import android.net.Uri;
import androidx.media3.common.C;
import androidx.media3.common.MediaItem;
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.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.junit.Test;
import org.junit.runner.RunWith;
/**
* End-to-end instrumentation test for {@link Transformer} audio test cases that cannot be tested
* using robolectric.
*/
@RunWith(AndroidJUnit4.class)
public class TransformerAudioEndToEndTest {
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
public void mixMonoToStereo_outputsStereo() throws Exception {
String testId = "mixMonoToStereo_outputsStereo";
ChannelMixingAudioProcessor channelMixingAudioProcessor = new ChannelMixingAudioProcessor();
channelMixingAudioProcessor.putChannelMixingMatrix(
ChannelMixingMatrix.create(/* inputChannelCount= */ 1, /* outputChannelCount= */ 2));
Effects effects = createForAudioProcessors(channelMixingAudioProcessor);
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.channelCount).isEqualTo(2);
}
private static Effects createForAudioProcessors(AudioProcessor... audioProcessors) {
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();
}
}
}

View File

@ -44,6 +44,9 @@ import androidx.media3.common.MediaItem;
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;
@ -54,9 +57,12 @@ import androidx.media3.effect.FrameCache;
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;
@ -627,6 +633,60 @@ public class TransformerEndToEndTest {
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;
@ -690,9 +750,30 @@ public class TransformerEndToEndTest {
}
}
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();
}
}
}