Revert DefaultCodec to final

The class is made non-final for a test in 12584fbb20.

PiperOrigin-RevId: 546718462
This commit is contained in:
claincly 2023-07-09 23:48:43 +01:00 committed by Rohit Singh
parent ca483a3c2c
commit 9c74e78f33
3 changed files with 83 additions and 50 deletions

View File

@ -21,18 +21,20 @@ import static androidx.media3.transformer.AndroidTestUtil.MP4_ASSET_FRAME_COUNT;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import android.content.Context; import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaFormat; import android.media.MediaFormat;
import android.util.Pair;
import android.view.Surface; import android.view.Surface;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.Format; import androidx.media3.common.Format;
import androidx.media3.common.MediaItem; import androidx.media3.common.MediaItem;
import androidx.media3.common.util.Clock; import androidx.media3.common.util.Clock;
import androidx.media3.common.util.Util; import androidx.media3.common.util.Util;
import androidx.media3.decoder.DecoderInputBuffer;
import androidx.test.core.app.ApplicationProvider; import androidx.test.core.app.ApplicationProvider;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import org.json.JSONException; import org.json.JSONException;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -119,13 +121,11 @@ public class ForceEndOfStreamTest {
} }
private static final class FrameDroppingDecoderFactory implements Codec.DecoderFactory { private static final class FrameDroppingDecoderFactory implements Codec.DecoderFactory {
private final Context context;
private final DefaultDecoderFactory defaultDecoderFactory; private final DefaultDecoderFactory defaultDecoderFactory;
private final int sourceFrameCount; private final int sourceFrameCount;
private final int framesToDrop; private final int framesToDrop;
private FrameDroppingDecoderFactory(Context context, int sourceFrameCount, int framesToDrop) { private FrameDroppingDecoderFactory(Context context, int sourceFrameCount, int framesToDrop) {
this.context = context;
this.defaultDecoderFactory = new DefaultDecoderFactory(context); this.defaultDecoderFactory = new DefaultDecoderFactory(context);
this.sourceFrameCount = sourceFrameCount; this.sourceFrameCount = sourceFrameCount;
this.framesToDrop = framesToDrop; this.framesToDrop = framesToDrop;
@ -140,52 +140,103 @@ public class ForceEndOfStreamTest {
public Codec createForVideoDecoding( public Codec createForVideoDecoding(
Format format, Surface outputSurface, boolean requestSdrToneMapping) Format format, Surface outputSurface, boolean requestSdrToneMapping)
throws ExportException { throws ExportException {
Pair<MediaFormat, String> videoDecoderMediaFormatAndName =
defaultDecoderFactory.findVideoDecoder(format, requestSdrToneMapping);
return new FrameDroppingDecoder( return new FrameDroppingDecoder(
context, defaultDecoderFactory.createForVideoDecoding(
format, format, outputSurface, requestSdrToneMapping),
videoDecoderMediaFormatAndName.first,
videoDecoderMediaFormatAndName.second,
outputSurface,
sourceFrameCount, sourceFrameCount,
framesToDrop); 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 sourceFrameCount;
private final int framesToDrop; private final int framesToDrop;
private int framesReceived; private int framesReceived;
public FrameDroppingDecoder( public FrameDroppingDecoder(DefaultCodec decoder, int sourceFrameCount, int framesToDrop)
Context context,
Format configurationFormat,
MediaFormat configurationMediaFormat,
String mediaCodecName,
@Nullable Surface outputSurface,
int sourceFrameCount,
int framesToDrop)
throws ExportException { throws ExportException {
super( wrappedDecoder = decoder;
context,
configurationFormat,
configurationMediaFormat,
mediaCodecName,
/* isDecoder= */ true,
outputSurface);
this.sourceFrameCount = sourceFrameCount; this.sourceFrameCount = sourceFrameCount;
this.framesToDrop = framesToDrop; 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 @Override
public void releaseOutputBuffer(long renderPresentationTimeUs) throws ExportException { public void releaseOutputBuffer(long renderPresentationTimeUs) throws ExportException {
framesReceived++; framesReceived++;
super.releaseOutputBuffer( wrappedDecoder.releaseOutputBuffer(
/* render= */ sourceFrameCount - framesReceived >= framesToDrop, /* render= */ sourceFrameCount - framesReceived >= framesToDrop,
renderPresentationTimeUs); renderPresentationTimeUs);
} }
@Override
public boolean isEnded() {
return wrappedDecoder.isEnded();
}
@Override
public void release() {
wrappedDecoder.release();
}
} }
} }
} }

View File

@ -51,7 +51,7 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
/** A default {@link Codec} implementation that uses {@link MediaCodec}. */ /** A default {@link Codec} implementation that uses {@link MediaCodec}. */
@UnstableApi @UnstableApi
public class DefaultCodec implements Codec { public final class DefaultCodec implements Codec {
// MediaCodec decoders output 16 bit PCM, unless configured to output PCM float. // MediaCodec decoders output 16 bit PCM, unless configured to output PCM float.
// https://developer.android.com/reference/android/media/MediaCodec#raw-audio-buffers. // https://developer.android.com/reference/android/media/MediaCodec#raw-audio-buffers.
public static final int DEFAULT_PCM_ENCODING = C.ENCODING_PCM_16BIT; public static final int DEFAULT_PCM_ENCODING = C.ENCODING_PCM_16BIT;

View File

@ -65,7 +65,7 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory {
} }
@Override @Override
public Codec createForAudioDecoding(Format format) throws ExportException { public DefaultCodec createForAudioDecoding(Format format) throws ExportException {
checkNotNull(format.sampleMimeType); checkNotNull(format.sampleMimeType);
MediaFormat mediaFormat = createMediaFormatFromFormat(format); MediaFormat mediaFormat = createMediaFormatFromFormat(format);
@ -96,26 +96,8 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory {
@SuppressLint("InlinedApi") @SuppressLint("InlinedApi")
@Override @Override
public Codec createForVideoDecoding( public DefaultCodec createForVideoDecoding(
Format format, Surface outputSurface, boolean requestSdrToneMapping) throws ExportException { Format format, Surface outputSurface, boolean requestSdrToneMapping) throws ExportException {
Pair<MediaFormat, String> 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<MediaFormat, String> findVideoDecoder(
Format format, boolean requestSdrToneMapping) throws ExportException {
checkNotNull(format.sampleMimeType); checkNotNull(format.sampleMimeType);
if (ColorInfo.isTransferHdr(format.colorInfo)) { if (ColorInfo.isTransferHdr(format.colorInfo)) {
@ -173,8 +155,8 @@ public final class DefaultDecoderFactory implements Codec.DecoderFactory {
MediaFormatUtil.maybeSetInteger( MediaFormatUtil.maybeSetInteger(
mediaFormat, MediaFormat.KEY_LEVEL, codecProfileAndLevel.second); mediaFormat, MediaFormat.KEY_LEVEL, codecProfileAndLevel.second);
} }
return new DefaultCodec(
return Pair.create(mediaFormat, mediaCodecName); context, format, mediaFormat, mediaCodecName, /* isDecoder= */ true, outputSurface);
} }
private static boolean deviceNeedsDisable8kWorkaround(Format format) { private static boolean deviceNeedsDisable8kWorkaround(Format format) {