Remove Transformer-specific things from MediaCodecAdapter.

PiperOrigin-RevId: 421514944
This commit is contained in:
hschlueter 2022-01-13 10:43:52 +00:00 committed by Ian Baker
parent b09b8dc2ab
commit 755df46a6b
4 changed files with 8 additions and 170 deletions

View File

@ -109,8 +109,7 @@ import java.nio.ByteBuffer;
configuration.mediaFormat, configuration.mediaFormat,
configuration.surface, configuration.surface,
configuration.crypto, configuration.crypto,
configuration.flags, configuration.flags);
configuration.createInputSurface);
return codecAdapter; return codecAdapter;
} catch (Exception e) { } catch (Exception e) {
if (codecAdapter != null) { if (codecAdapter != null) {
@ -139,7 +138,6 @@ import java.nio.ByteBuffer;
private final boolean enableImmediateCodecStartAfterFlush; private final boolean enableImmediateCodecStartAfterFlush;
private boolean codecReleased; private boolean codecReleased;
@State private int state; @State private int state;
@Nullable private Surface inputSurface;
private AsynchronousMediaCodecAdapter( private AsynchronousMediaCodecAdapter(
MediaCodec codec, MediaCodec codec,
@ -159,15 +157,11 @@ import java.nio.ByteBuffer;
@Nullable MediaFormat mediaFormat, @Nullable MediaFormat mediaFormat,
@Nullable Surface surface, @Nullable Surface surface,
@Nullable MediaCrypto crypto, @Nullable MediaCrypto crypto,
int flags, int flags) {
boolean createInputSurface) {
asynchronousMediaCodecCallback.initialize(codec); asynchronousMediaCodecCallback.initialize(codec);
TraceUtil.beginSection("configureCodec"); TraceUtil.beginSection("configureCodec");
codec.configure(mediaFormat, surface, crypto, flags); codec.configure(mediaFormat, surface, crypto, flags);
TraceUtil.endSection(); TraceUtil.endSection();
if (createInputSurface) {
inputSurface = codec.createInputSurface();
}
bufferEnqueuer.start(); bufferEnqueuer.start();
TraceUtil.beginSection("startCodec"); TraceUtil.beginSection("startCodec");
codec.start(); codec.start();
@ -223,12 +217,6 @@ import java.nio.ByteBuffer;
return codec.getInputBuffer(index); return codec.getInputBuffer(index);
} }
@Override
@Nullable
public Surface getInputSurface() {
return inputSurface;
}
@Override @Override
@Nullable @Nullable
public ByteBuffer getOutputBuffer(int index) { public ByteBuffer getOutputBuffer(int index) {
@ -263,9 +251,6 @@ import java.nio.ByteBuffer;
} }
state = STATE_SHUT_DOWN; state = STATE_SHUT_DOWN;
} finally { } finally {
if (inputSurface != null) {
inputSurface.release();
}
if (!codecReleased) { if (!codecReleased) {
codec.release(); codec.release();
codecReleased = true; codecReleased = true;
@ -301,12 +286,6 @@ import java.nio.ByteBuffer;
codec.setVideoScalingMode(scalingMode); codec.setVideoScalingMode(scalingMode);
} }
@Override
public void signalEndOfInputStream() {
maybeBlockOnQueueing();
codec.signalEndOfInputStream();
}
@Override @Override
@RequiresApi(26) @RequiresApi(26)
public PersistableBundle getMetrics() { public PersistableBundle getMetrics() {

View File

@ -55,13 +55,7 @@ public interface MediaCodecAdapter {
Format format, Format format,
@Nullable MediaCrypto crypto) { @Nullable MediaCrypto crypto) {
return new Configuration( return new Configuration(
codecInfo, codecInfo, mediaFormat, format, /* surface= */ null, crypto, /* flags= */ 0);
mediaFormat,
format,
/* surface= */ null,
crypto,
/* flags= */ 0,
/* createInputSurface= */ false);
} }
/** /**
@ -80,55 +74,7 @@ public interface MediaCodecAdapter {
Format format, Format format,
@Nullable Surface surface, @Nullable Surface surface,
@Nullable MediaCrypto crypto) { @Nullable MediaCrypto crypto) {
return new Configuration( return new Configuration(codecInfo, mediaFormat, format, surface, crypto, /* flags= */ 0);
codecInfo,
mediaFormat,
format,
surface,
crypto,
/* flags= */ 0,
/* createInputSurface= */ false);
}
/**
* Creates a configuration for audio encoding.
*
* @param codecInfo See {@link #codecInfo}.
* @param mediaFormat See {@link #mediaFormat}.
* @param format See {@link #format}.
* @return The created instance.
*/
public static Configuration createForAudioEncoding(
MediaCodecInfo codecInfo, MediaFormat mediaFormat, Format format) {
return new Configuration(
codecInfo,
mediaFormat,
format,
/* surface= */ null,
/* crypto= */ null,
MediaCodec.CONFIGURE_FLAG_ENCODE,
/* createInputSurface= */ false);
}
/**
* Creates a configuration for video encoding.
*
* @param codecInfo See {@link #codecInfo}.
* @param mediaFormat See {@link #mediaFormat}.
* @param format See {@link #format}.
* @return The created instance.
*/
@RequiresApi(18)
public static Configuration createForVideoEncoding(
MediaCodecInfo codecInfo, MediaFormat mediaFormat, Format format) {
return new Configuration(
codecInfo,
mediaFormat,
format,
/* surface= */ null,
/* crypto= */ null,
MediaCodec.CONFIGURE_FLAG_ENCODE,
/* createInputSurface= */ true);
} }
/** Information about the {@link MediaCodec} being configured. */ /** Information about the {@link MediaCodec} being configured. */
@ -145,17 +91,8 @@ public interface MediaCodecAdapter {
@Nullable public final Surface surface; @Nullable public final Surface surface;
/** For DRM protected playbacks, a {@link MediaCrypto} to use for decryption. */ /** For DRM protected playbacks, a {@link MediaCrypto} to use for decryption. */
@Nullable public final MediaCrypto crypto; @Nullable public final MediaCrypto crypto;
/** /** See {@link MediaCodec#configure}. */
* Specify CONFIGURE_FLAG_ENCODE to configure the component as an encoder.
*
* @see MediaCodec#configure
*/
public final int flags; public final int flags;
/**
* Whether to request a {@link Surface} and use it as to the input to an encoder. This can only
* be set to {@code true} on API 18+.
*/
public final boolean createInputSurface;
private Configuration( private Configuration(
MediaCodecInfo codecInfo, MediaCodecInfo codecInfo,
@ -163,15 +100,13 @@ public interface MediaCodecAdapter {
Format format, Format format,
@Nullable Surface surface, @Nullable Surface surface,
@Nullable MediaCrypto crypto, @Nullable MediaCrypto crypto,
int flags, int flags) {
boolean createInputSurface) {
this.codecInfo = codecInfo; this.codecInfo = codecInfo;
this.mediaFormat = mediaFormat; this.mediaFormat = mediaFormat;
this.format = format; this.format = format;
this.surface = surface; this.surface = surface;
this.crypto = crypto; this.crypto = crypto;
this.flags = flags; this.flags = flags;
this.createInputSurface = createInputSurface;
} }
} }
@ -229,14 +164,6 @@ public interface MediaCodecAdapter {
@Nullable @Nullable
ByteBuffer getInputBuffer(int index); ByteBuffer getInputBuffer(int index);
/**
* Returns the input {@link Surface}, or null if the input is not a surface.
*
* @see MediaCodec#createInputSurface()
*/
@Nullable
Surface getInputSurface();
/** /**
* Returns a read-only ByteBuffer for a dequeued output buffer index. * Returns a read-only ByteBuffer for a dequeued output buffer index.
* *
@ -329,15 +256,6 @@ public interface MediaCodecAdapter {
/** Whether the adapter needs to be reconfigured before it is used. */ /** Whether the adapter needs to be reconfigured before it is used. */
boolean needsReconfiguration(); boolean needsReconfiguration();
/**
* Signals the encoder of end-of-stream on input. The call can only be used when the encoder
* receives its input from a {@link Surface surface}.
*
* @see MediaCodec#signalEndOfInputStream()
*/
@RequiresApi(18)
void signalEndOfInputStream();
/** /**
* Returns metrics data about the current codec instance. * Returns metrics data about the current codec instance.
* *

View File

@ -25,7 +25,6 @@ import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.os.PersistableBundle; import android.os.PersistableBundle;
import android.view.Surface; import android.view.Surface;
import androidx.annotation.DoNotInline;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi; import androidx.annotation.RequiresApi;
import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.C;
@ -46,7 +45,6 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
@Override @Override
public MediaCodecAdapter createAdapter(Configuration configuration) throws IOException { public MediaCodecAdapter createAdapter(Configuration configuration) throws IOException {
@Nullable MediaCodec codec = null; @Nullable MediaCodec codec = null;
@Nullable Surface inputSurface = null;
try { try {
codec = createCodec(configuration); codec = createCodec(configuration);
TraceUtil.beginSection("configureCodec"); TraceUtil.beginSection("configureCodec");
@ -56,24 +54,11 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
configuration.crypto, configuration.crypto,
configuration.flags); configuration.flags);
TraceUtil.endSection(); TraceUtil.endSection();
if (configuration.createInputSurface) {
if (Util.SDK_INT >= 18) {
inputSurface = Api18.createCodecInputSurface(codec);
} else {
throw new IllegalStateException(
"Encoding from a surface is only supported on API 18 and up.");
}
}
TraceUtil.beginSection("startCodec"); TraceUtil.beginSection("startCodec");
codec.start(); codec.start();
TraceUtil.endSection(); TraceUtil.endSection();
return new SynchronousMediaCodecAdapter(codec, inputSurface); return new SynchronousMediaCodecAdapter(codec);
} catch (IOException | RuntimeException e) { } catch (IOException | RuntimeException e) {
if (inputSurface != null) {
inputSurface.release();
}
if (codec != null) { if (codec != null) {
codec.release(); codec.release();
} }
@ -93,13 +78,11 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
} }
private final MediaCodec codec; private final MediaCodec codec;
@Nullable private final Surface inputSurface;
@Nullable private ByteBuffer[] inputByteBuffers; @Nullable private ByteBuffer[] inputByteBuffers;
@Nullable private ByteBuffer[] outputByteBuffers; @Nullable private ByteBuffer[] outputByteBuffers;
private SynchronousMediaCodecAdapter(MediaCodec mediaCodec, @Nullable Surface inputSurface) { private SynchronousMediaCodecAdapter(MediaCodec mediaCodec) {
this.codec = mediaCodec; this.codec = mediaCodec;
this.inputSurface = inputSurface;
if (Util.SDK_INT < 21) { if (Util.SDK_INT < 21) {
inputByteBuffers = codec.getInputBuffers(); inputByteBuffers = codec.getInputBuffers();
outputByteBuffers = codec.getOutputBuffers(); outputByteBuffers = codec.getOutputBuffers();
@ -144,12 +127,6 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
} }
} }
@Override
@Nullable
public Surface getInputSurface() {
return inputSurface;
}
@Override @Override
@Nullable @Nullable
public ByteBuffer getOutputBuffer(int index) { public ByteBuffer getOutputBuffer(int index) {
@ -193,18 +170,9 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
public void release() { public void release() {
inputByteBuffers = null; inputByteBuffers = null;
outputByteBuffers = null; outputByteBuffers = null;
if (inputSurface != null) {
inputSurface.release();
}
codec.release(); codec.release();
} }
@Override
@RequiresApi(18)
public void signalEndOfInputStream() {
Api18.signalEndOfInputStream(codec);
}
@Override @Override
@RequiresApi(23) @RequiresApi(23)
public void setOnFrameRenderedListener(OnFrameRenderedListener listener, Handler handler) { public void setOnFrameRenderedListener(OnFrameRenderedListener listener, Handler handler) {
@ -237,19 +205,4 @@ public final class SynchronousMediaCodecAdapter implements MediaCodecAdapter {
public PersistableBundle getMetrics() { public PersistableBundle getMetrics() {
return codec.getMetrics(); return codec.getMetrics();
} }
@RequiresApi(18)
private static final class Api18 {
private Api18() {}
@DoNotInline
public static Surface createCodecInputSurface(MediaCodec codec) {
return codec.createInputSurface();
}
@DoNotInline
public static void signalEndOfInputStream(MediaCodec codec) {
codec.signalEndOfInputStream();
}
}
} }

View File

@ -195,12 +195,6 @@ public class CapturingRenderersFactory implements RenderersFactory, Dumper.Dumpa
return inputBuffer; return inputBuffer;
} }
@Nullable
@Override
public Surface getInputSurface() {
return delegate.getInputSurface();
}
@Nullable @Nullable
@Override @Override
public ByteBuffer getOutputBuffer(int index) { public ByteBuffer getOutputBuffer(int index) {
@ -270,12 +264,6 @@ public class CapturingRenderersFactory implements RenderersFactory, Dumper.Dumpa
delegate.setVideoScalingMode(scalingMode); delegate.setVideoScalingMode(scalingMode);
} }
@RequiresApi(18)
@Override
public void signalEndOfInputStream() {
delegate.signalEndOfInputStream();
}
@RequiresApi(26) @RequiresApi(26)
@Override @Override
public PersistableBundle getMetrics() { public PersistableBundle getMetrics() {