Commit ac413a14 by christosts Committed by tonihei

Add experimental method to turn-off async flush

When operating the MediaCodec in asynchronous mode, after a
MediaCodec.flush(), we start MediaCodec in the callback thread,
which might trigger errors in some platforms. This change adds an
experimental flag to move the call to MediaCodec.start() back to the
playback thread.

PiperOrigin-RevId: 407801013
parent 84c24fb6
...@@ -192,6 +192,25 @@ public class DefaultRenderersFactory implements RenderersFactory { ...@@ -192,6 +192,25 @@ public class DefaultRenderersFactory implements RenderersFactory {
} }
/** /**
* Enable calling {@link MediaCodec#start} immediately after {@link MediaCodec#flush} on the
* playback thread, when operating the codec in asynchronous mode. If disabled, {@link
* MediaCodec#start} will be called by the callback thread after pending callbacks are handled.
*
* <p>By default, this feature is disabled.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* @param enabled Whether {@link MediaCodec#start} will be called on the playback thread
* immediately after {@link MediaCodec#flush}.
* @return This factory, for convenience.
*/
public DefaultRenderersFactory experimentalSetImmediateCodecStartAfterFlushEnabled(
boolean enabled) {
codecAdapterFactory.experimentalSetImmediateCodecStartAfterFlushEnabled(enabled);
return this;
}
/**
* Sets whether to enable fallback to lower-priority decoders if decoder initialization fails. * Sets whether to enable fallback to lower-priority decoders if decoder initialization fails.
* This may result in using a decoder that is less efficient or slower than the primary decoder. * This may result in using a decoder that is less efficient or slower than the primary decoder.
* *
......
...@@ -50,11 +50,7 @@ import java.nio.ByteBuffer; ...@@ -50,11 +50,7 @@ import java.nio.ByteBuffer;
private final Supplier<HandlerThread> callbackThreadSupplier; private final Supplier<HandlerThread> callbackThreadSupplier;
private final Supplier<HandlerThread> queueingThreadSupplier; private final Supplier<HandlerThread> queueingThreadSupplier;
private final boolean synchronizeCodecInteractionsWithQueueing; private final boolean synchronizeCodecInteractionsWithQueueing;
private final boolean enableImmediateCodecStartAfterFlush;
/** Creates a factory for codecs handling the specified {@link C.TrackType track type}. */
public Factory(@C.TrackType int trackType) {
this(trackType, /* synchronizeCodecInteractionsWithQueueing= */ false);
}
/** /**
* Creates an factory for {@link AsynchronousMediaCodecAdapter} instances. * Creates an factory for {@link AsynchronousMediaCodecAdapter} instances.
...@@ -66,23 +62,29 @@ import java.nio.ByteBuffer; ...@@ -66,23 +62,29 @@ import java.nio.ByteBuffer;
* interactions will wait until all input buffers pending queueing wil be submitted to the * interactions will wait until all input buffers pending queueing wil be submitted to the
* {@link MediaCodec}. * {@link MediaCodec}.
*/ */
public Factory(@C.TrackType int trackType, boolean synchronizeCodecInteractionsWithQueueing) { public Factory(
@C.TrackType int trackType,
boolean synchronizeCodecInteractionsWithQueueing,
boolean enableImmediateCodecStartAfterFlush) {
this( this(
/* callbackThreadSupplier= */ () -> /* callbackThreadSupplier= */ () ->
new HandlerThread(createCallbackThreadLabel(trackType)), new HandlerThread(createCallbackThreadLabel(trackType)),
/* queueingThreadSupplier= */ () -> /* queueingThreadSupplier= */ () ->
new HandlerThread(createQueueingThreadLabel(trackType)), new HandlerThread(createQueueingThreadLabel(trackType)),
synchronizeCodecInteractionsWithQueueing); synchronizeCodecInteractionsWithQueueing,
enableImmediateCodecStartAfterFlush);
} }
@VisibleForTesting @VisibleForTesting
/* package */ Factory( /* package */ Factory(
Supplier<HandlerThread> callbackThreadSupplier, Supplier<HandlerThread> callbackThreadSupplier,
Supplier<HandlerThread> queueingThreadSupplier, Supplier<HandlerThread> queueingThreadSupplier,
boolean synchronizeCodecInteractionsWithQueueing) { boolean synchronizeCodecInteractionsWithQueueing,
boolean enableImmediateCodecStartAfterFlush) {
this.callbackThreadSupplier = callbackThreadSupplier; this.callbackThreadSupplier = callbackThreadSupplier;
this.queueingThreadSupplier = queueingThreadSupplier; this.queueingThreadSupplier = queueingThreadSupplier;
this.synchronizeCodecInteractionsWithQueueing = synchronizeCodecInteractionsWithQueueing; this.synchronizeCodecInteractionsWithQueueing = synchronizeCodecInteractionsWithQueueing;
this.enableImmediateCodecStartAfterFlush = enableImmediateCodecStartAfterFlush;
} }
@Override @Override
...@@ -99,7 +101,8 @@ import java.nio.ByteBuffer; ...@@ -99,7 +101,8 @@ import java.nio.ByteBuffer;
codec, codec,
callbackThreadSupplier.get(), callbackThreadSupplier.get(),
queueingThreadSupplier.get(), queueingThreadSupplier.get(),
synchronizeCodecInteractionsWithQueueing); synchronizeCodecInteractionsWithQueueing,
enableImmediateCodecStartAfterFlush);
TraceUtil.endSection(); TraceUtil.endSection();
codecAdapter.initialize( codecAdapter.initialize(
configuration.mediaFormat, configuration.mediaFormat,
...@@ -132,6 +135,7 @@ import java.nio.ByteBuffer; ...@@ -132,6 +135,7 @@ import java.nio.ByteBuffer;
private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback; private final AsynchronousMediaCodecCallback asynchronousMediaCodecCallback;
private final AsynchronousMediaCodecBufferEnqueuer bufferEnqueuer; private final AsynchronousMediaCodecBufferEnqueuer bufferEnqueuer;
private final boolean synchronizeCodecInteractionsWithQueueing; private final boolean synchronizeCodecInteractionsWithQueueing;
private final boolean enableImmediateCodecStartAfterFlush;
private boolean codecReleased; private boolean codecReleased;
@State private int state; @State private int state;
@Nullable private Surface inputSurface; @Nullable private Surface inputSurface;
...@@ -140,11 +144,13 @@ import java.nio.ByteBuffer; ...@@ -140,11 +144,13 @@ import java.nio.ByteBuffer;
MediaCodec codec, MediaCodec codec,
HandlerThread callbackThread, HandlerThread callbackThread,
HandlerThread enqueueingThread, HandlerThread enqueueingThread,
boolean synchronizeCodecInteractionsWithQueueing) { boolean synchronizeCodecInteractionsWithQueueing,
boolean enableImmediateCodecStartAfterFlush) {
this.codec = codec; this.codec = codec;
this.asynchronousMediaCodecCallback = new AsynchronousMediaCodecCallback(callbackThread); this.asynchronousMediaCodecCallback = new AsynchronousMediaCodecCallback(callbackThread);
this.bufferEnqueuer = new AsynchronousMediaCodecBufferEnqueuer(codec, enqueueingThread); this.bufferEnqueuer = new AsynchronousMediaCodecBufferEnqueuer(codec, enqueueingThread);
this.synchronizeCodecInteractionsWithQueueing = synchronizeCodecInteractionsWithQueueing; this.synchronizeCodecInteractionsWithQueueing = synchronizeCodecInteractionsWithQueueing;
this.enableImmediateCodecStartAfterFlush = enableImmediateCodecStartAfterFlush;
this.state = STATE_CREATED; this.state = STATE_CREATED;
} }
...@@ -231,13 +237,20 @@ import java.nio.ByteBuffer; ...@@ -231,13 +237,20 @@ import java.nio.ByteBuffer;
@Override @Override
public void flush() { public void flush() {
// The order of calls is important: // The order of calls is important:
// First, flush the bufferEnqueuer to stop queueing input buffers. // 1. Flush the bufferEnqueuer to stop queueing input buffers.
// Second, flush the codec to stop producing available input/output buffers. // 2. Flush the codec to stop producing available input/output buffers.
// Third, flush the callback after flushing the codec so that in-flight callbacks are discarded. // 3. Flush the callback after flushing the codec so that in-flight callbacks are discarded.
bufferEnqueuer.flush(); bufferEnqueuer.flush();
codec.flush(); codec.flush();
// When flushAsync() is completed, start the codec again. if (enableImmediateCodecStartAfterFlush) {
asynchronousMediaCodecCallback.flushAsync(/* onFlushCompleted= */ codec::start); // The asynchronous callback will drop pending callbacks but we can start the codec now.
asynchronousMediaCodecCallback.flush(/* codec= */ null);
codec.start();
} else {
// Let the asynchronous callback start the codec in the callback thread after pending
// callbacks are handled.
asynchronousMediaCodecCallback.flush(codec);
}
} }
@Override @Override
......
...@@ -34,8 +34,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -34,8 +34,8 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
@RequiresApi(23) @RequiresApi(23)
/* package */ final class AsynchronousMediaCodecCallback extends MediaCodec.Callback { /* package */ final class AsynchronousMediaCodecCallback extends MediaCodec.Callback {
private final Object lock; private final Object lock;
private final HandlerThread callbackThread; private final HandlerThread callbackThread;
private @MonotonicNonNull Handler handler; private @MonotonicNonNull Handler handler;
@GuardedBy("lock") @GuardedBy("lock")
...@@ -192,14 +192,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -192,14 +192,13 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
* Initiates a flush asynchronously, which will be completed on the callback thread. When the * Initiates a flush asynchronously, which will be completed on the callback thread. When the
* flush is complete, it will trigger {@code onFlushCompleted} from the callback thread. * flush is complete, it will trigger {@code onFlushCompleted} from the callback thread.
* *
* @param onFlushCompleted A {@link Runnable} that will be called when flush is completed. {@code * @param codec A {@link MediaCodec} to {@link MediaCodec#start start} after all pending callbacks
* onFlushCompleted} will be called from the scallback thread, therefore it should execute * are handled, or {@code null} if starting the {@link MediaCodec} is performed elsewhere.
* synchronized and thread-safe code.
*/ */
public void flushAsync(Runnable onFlushCompleted) { public void flush(@Nullable MediaCodec codec) {
synchronized (lock) { synchronized (lock) {
++pendingFlushCount; ++pendingFlushCount;
Util.castNonNull(handler).post(() -> this.onFlushCompleted(onFlushCompleted)); Util.castNonNull(handler).post(() -> this.onFlushCompleted(codec));
} }
} }
...@@ -239,34 +238,31 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -239,34 +238,31 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
} }
} }
private void onFlushCompleted(Runnable onFlushCompleted) { private void onFlushCompleted(@Nullable MediaCodec codec) {
synchronized (lock) { synchronized (lock) {
onFlushCompletedSynchronized(onFlushCompleted); if (shutDown) {
} return;
} }
@GuardedBy("lock")
private void onFlushCompletedSynchronized(Runnable onFlushCompleted) {
if (shutDown) {
return;
}
--pendingFlushCount; --pendingFlushCount;
if (pendingFlushCount > 0) { if (pendingFlushCount > 0) {
// Another flush() has been called. // Another flush() has been called.
return; return;
} else if (pendingFlushCount < 0) { } else if (pendingFlushCount < 0) {
// This should never happen. // This should never happen.
setInternalException(new IllegalStateException()); setInternalException(new IllegalStateException());
return; return;
} }
flushInternal(); flushInternal();
try { if (codec != null) {
onFlushCompleted.run(); try {
} catch (IllegalStateException e) { codec.start();
setInternalException(e); } catch (IllegalStateException e) {
} catch (Exception e) { setInternalException(e);
setInternalException(new IllegalStateException(e)); } catch (Exception e) {
setInternalException(new IllegalStateException(e));
}
}
} }
} }
...@@ -275,10 +271,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; ...@@ -275,10 +271,10 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
private void flushInternal() { private void flushInternal() {
if (!formats.isEmpty()) { if (!formats.isEmpty()) {
pendingOutputFormat = formats.getLast(); pendingOutputFormat = formats.getLast();
} else {
// pendingOutputFormat may already be non-null following a previous flush, and remains set in
// this case.
} }
// else, pendingOutputFormat may already be non-null following a previous flush, and remains
// set in this case.
availableInputBuffers.clear(); availableInputBuffers.clear();
availableOutputBuffers.clear(); availableOutputBuffers.clear();
bufferInfos.clear(); bufferInfos.clear();
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
*/ */
package com.google.android.exoplayer2.mediacodec; package com.google.android.exoplayer2.mediacodec;
import android.media.MediaCodec;
import androidx.annotation.IntDef; import androidx.annotation.IntDef;
import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Log;
import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.MimeTypes;
...@@ -46,6 +47,7 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter. ...@@ -46,6 +47,7 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter.
@Mode private int asynchronousMode; @Mode private int asynchronousMode;
private boolean enableSynchronizeCodecInteractionsWithQueueing; private boolean enableSynchronizeCodecInteractionsWithQueueing;
private boolean enableImmediateCodecStartAfterFlush;
public DefaultMediaCodecAdapterFactory() { public DefaultMediaCodecAdapterFactory() {
asynchronousMode = MODE_DEFAULT; asynchronousMode = MODE_DEFAULT;
...@@ -85,6 +87,22 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter. ...@@ -85,6 +87,22 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter.
enableSynchronizeCodecInteractionsWithQueueing = enabled; enableSynchronizeCodecInteractionsWithQueueing = enabled;
} }
/**
* Enable calling {@link MediaCodec#start} immediately after {@link MediaCodec#flush} on the
* playback thread, when operating the codec in asynchronous mode. If disabled, {@link
* MediaCodec#start} will be called by the callback thread after pending callbacks are handled.
*
* <p>By default, this feature is disabled.
*
* <p>This method is experimental, and will be renamed or removed in a future release.
*
* @param enabled Whether {@link MediaCodec#start()} will be called on the playback thread
* immediately after {@link MediaCodec#flush}.
*/
public void experimentalSetImmediateCodecStartAfterFlushEnabled(boolean enabled) {
enableImmediateCodecStartAfterFlush = enabled;
}
@Override @Override
public MediaCodecAdapter createAdapter(MediaCodecAdapter.Configuration configuration) public MediaCodecAdapter createAdapter(MediaCodecAdapter.Configuration configuration)
throws IOException { throws IOException {
...@@ -97,7 +115,9 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter. ...@@ -97,7 +115,9 @@ public final class DefaultMediaCodecAdapterFactory implements MediaCodecAdapter.
+ Util.getTrackTypeString(trackType)); + Util.getTrackTypeString(trackType));
AsynchronousMediaCodecAdapter.Factory factory = AsynchronousMediaCodecAdapter.Factory factory =
new AsynchronousMediaCodecAdapter.Factory( new AsynchronousMediaCodecAdapter.Factory(
trackType, enableSynchronizeCodecInteractionsWithQueueing); trackType,
enableSynchronizeCodecInteractionsWithQueueing,
enableImmediateCodecStartAfterFlush);
return factory.createAdapter(configuration); return factory.createAdapter(configuration);
} }
return new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration); return new SynchronousMediaCodecAdapter.Factory().createAdapter(configuration);
......
...@@ -54,7 +54,8 @@ public class AsynchronousMediaCodecAdapterTest { ...@@ -54,7 +54,8 @@ public class AsynchronousMediaCodecAdapterTest {
new AsynchronousMediaCodecAdapter.Factory( new AsynchronousMediaCodecAdapter.Factory(
/* callbackThreadSupplier= */ () -> callbackThread, /* callbackThreadSupplier= */ () -> callbackThread,
/* queueingThreadSupplier= */ () -> queueingThread, /* queueingThreadSupplier= */ () -> queueingThread,
/* synchronizeCodecInteractionsWithQueueing= */ false) /* synchronizeCodecInteractionsWithQueueing= */ false,
/* enableImmediateCodecStartAfterFlush= */ false)
.createAdapter(configuration); .createAdapter(configuration);
bufferInfo = new MediaCodec.BufferInfo(); bufferInfo = new MediaCodec.BufferInfo();
// After starting the MediaCodec, the ShadowMediaCodec offers input buffer 0. We advance the // After starting the MediaCodec, the ShadowMediaCodec offers input buffer 0. We advance the
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment