From 9c74e78f33a30468d43a5ef30af3e94874005080 Mon Sep 17 00:00:00 2001 From: claincly Date: Sun, 9 Jul 2023 23:48:43 +0100 Subject: [PATCH] Revert DefaultCodec to final The class is made non-final for a test in https://github.com/androidx/media/commit/12584fbb204043c0e5b64f7759da22e3e924b3d8. PiperOrigin-RevId: 546718462 --- .../transformer/ForceEndOfStreamTest.java | 105 +++++++++++++----- .../media3/transformer/DefaultCodec.java | 2 +- .../transformer/DefaultDecoderFactory.java | 26 +---- 3 files changed, 83 insertions(+), 50 deletions(-) diff --git a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/ForceEndOfStreamTest.java b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/ForceEndOfStreamTest.java index 8d64f3c852..57b32fcd9b 100644 --- a/libraries/transformer/src/androidTest/java/androidx/media3/transformer/ForceEndOfStreamTest.java +++ b/libraries/transformer/src/androidTest/java/androidx/media3/transformer/ForceEndOfStreamTest.java @@ -21,18 +21,20 @@ import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_FRAME_COUNT; import static com.google.common.truth.Truth.assertThat; import android.content.Context; +import android.media.MediaCodec; import android.media.MediaFormat; -import android.util.Pair; import android.view.Surface; import androidx.annotation.Nullable; import androidx.media3.common.Format; import androidx.media3.common.MediaItem; import androidx.media3.common.util.Clock; import androidx.media3.common.util.Util; +import androidx.media3.decoder.DecoderInputBuffer; import androidx.test.core.app.ApplicationProvider; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.common.collect.ImmutableList; import java.io.IOException; +import java.nio.ByteBuffer; import org.json.JSONException; import org.junit.Test; import org.junit.runner.RunWith; @@ -119,13 +121,11 @@ public class ForceEndOfStreamTest { } private static final class FrameDroppingDecoderFactory implements Codec.DecoderFactory { - private final Context context; private final DefaultDecoderFactory defaultDecoderFactory; private final int sourceFrameCount; private final int framesToDrop; private FrameDroppingDecoderFactory(Context context, int sourceFrameCount, int framesToDrop) { - this.context = context; this.defaultDecoderFactory = new DefaultDecoderFactory(context); this.sourceFrameCount = sourceFrameCount; this.framesToDrop = framesToDrop; @@ -140,52 +140,103 @@ public class ForceEndOfStreamTest { public Codec createForVideoDecoding( Format format, Surface outputSurface, boolean requestSdrToneMapping) throws ExportException { - Pair videoDecoderMediaFormatAndName = - defaultDecoderFactory.findVideoDecoder(format, requestSdrToneMapping); return new FrameDroppingDecoder( - context, - format, - videoDecoderMediaFormatAndName.first, - videoDecoderMediaFormatAndName.second, - outputSurface, + defaultDecoderFactory.createForVideoDecoding( + format, outputSurface, requestSdrToneMapping), sourceFrameCount, framesToDrop); } - private static final class FrameDroppingDecoder extends DefaultCodec { + public static final class FrameDroppingDecoder implements Codec { + private final DefaultCodec wrappedDecoder; private final int sourceFrameCount; private final int framesToDrop; private int framesReceived; - public FrameDroppingDecoder( - Context context, - Format configurationFormat, - MediaFormat configurationMediaFormat, - String mediaCodecName, - @Nullable Surface outputSurface, - int sourceFrameCount, - int framesToDrop) + public FrameDroppingDecoder(DefaultCodec decoder, int sourceFrameCount, int framesToDrop) throws ExportException { - super( - context, - configurationFormat, - configurationMediaFormat, - mediaCodecName, - /* isDecoder= */ true, - outputSurface); + wrappedDecoder = decoder; this.sourceFrameCount = sourceFrameCount; this.framesToDrop = framesToDrop; } + @Override + public Format getConfigurationFormat() { + return wrappedDecoder.getConfigurationFormat(); + } + + @Override + public String getName() { + return wrappedDecoder.getName(); + } + + @Override + public Surface getInputSurface() { + throw new UnsupportedOperationException(); + } + + @Override + public int getMaxPendingFrameCount() { + return wrappedDecoder.getMaxPendingFrameCount(); + } + + @Override + public boolean maybeDequeueInputBuffer(DecoderInputBuffer inputBuffer) + throws ExportException { + return wrappedDecoder.maybeDequeueInputBuffer(inputBuffer); + } + + @Override + public void queueInputBuffer(DecoderInputBuffer inputBuffer) throws ExportException { + wrappedDecoder.queueInputBuffer(inputBuffer); + } + + @Override + public void signalEndOfInputStream() throws ExportException { + wrappedDecoder.signalEndOfInputStream(); + } + + @Nullable + @Override + public Format getOutputFormat() throws ExportException { + return wrappedDecoder.getOutputFormat(); + } + + @Override + public ByteBuffer getOutputBuffer() throws ExportException { + throw new UnsupportedOperationException(); + } + + @Nullable + @Override + public MediaCodec.BufferInfo getOutputBufferInfo() throws ExportException { + return wrappedDecoder.getOutputBufferInfo(); + } + + @Override + public void releaseOutputBuffer(boolean render) throws ExportException { + wrappedDecoder.releaseOutputBuffer(render); + } + @Override public void releaseOutputBuffer(long renderPresentationTimeUs) throws ExportException { framesReceived++; - super.releaseOutputBuffer( + wrappedDecoder.releaseOutputBuffer( /* render= */ sourceFrameCount - framesReceived >= framesToDrop, renderPresentationTimeUs); } + + @Override + public boolean isEnded() { + return wrappedDecoder.isEnded(); + } + + @Override + public void release() { + wrappedDecoder.release(); + } } } } diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultCodec.java b/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultCodec.java index a45918fbcb..2e70713378 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultCodec.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultCodec.java @@ -51,7 +51,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** A default {@link Codec} implementation that uses {@link MediaCodec}. */ @UnstableApi -public class DefaultCodec implements Codec { +public final class DefaultCodec implements Codec { // MediaCodec decoders output 16 bit PCM, unless configured to output PCM float. // https://developer.android.com/reference/android/media/MediaCodec#raw-audio-buffers. public static final int DEFAULT_PCM_ENCODING = C.ENCODING_PCM_16BIT; diff --git a/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultDecoderFactory.java b/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultDecoderFactory.java index 51b903566d..9baacc4332 100644 --- a/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultDecoderFactory.java +++ b/libraries/transformer/src/main/java/androidx/media3/transformer/DefaultDecoderFactory.java @@ -65,7 +65,7 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory { } @Override - public Codec createForAudioDecoding(Format format) throws ExportException { + public DefaultCodec createForAudioDecoding(Format format) throws ExportException { checkNotNull(format.sampleMimeType); MediaFormat mediaFormat = createMediaFormatFromFormat(format); @@ -96,26 +96,8 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory { @SuppressLint("InlinedApi") @Override - public Codec createForVideoDecoding( + public DefaultCodec createForVideoDecoding( Format format, Surface outputSurface, boolean requestSdrToneMapping) throws ExportException { - Pair videoDecoderMediaFormatAndName = - findVideoDecoder(format, requestSdrToneMapping); - return new DefaultCodec( - context, - format, - videoDecoderMediaFormatAndName.first, - videoDecoderMediaFormatAndName.second, - /* isDecoder= */ true, - outputSurface); - } - - /** - * Returns the {@link MediaFormat} to configure the {@linkplain Codec decoder}, and the name of - * the {@link MediaCodec} decoder. - */ - @VisibleForTesting - /* package */ Pair findVideoDecoder( - Format format, boolean requestSdrToneMapping) throws ExportException { checkNotNull(format.sampleMimeType); if (ColorInfo.isTransferHdr(format.colorInfo)) { @@ -173,8 +155,8 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory { MediaFormatUtil.maybeSetInteger( mediaFormat, MediaFormat.KEY_LEVEL, codecProfileAndLevel.second); } - - return Pair.create(mediaFormat, mediaCodecName); + return new DefaultCodec( + context, format, mediaFormat, mediaCodecName, /* isDecoder= */ true, outputSurface); } private static boolean deviceNeedsDisable8kWorkaround(Format format) {