Commit 4c0ba12c by christosts Committed by kim-vde

Synchronize calls to queueSecureInpuffer()

Parallel asynchronous calls to MediaCodec.queueSecureInputBuffer() may
produce garbled video on some platforms. This workaround synchronizes
calls to MediaCodec.queueSecureInputBuffer() so that only one call is
in flight.

PiperOrigin-RevId: 297601037
parent 88e356f2
...@@ -49,11 +49,14 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque ...@@ -49,11 +49,14 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque
@GuardedBy("MESSAGE_PARAMS_INSTANCE_POOL") @GuardedBy("MESSAGE_PARAMS_INSTANCE_POOL")
private static final ArrayDeque<MessageParams> MESSAGE_PARAMS_INSTANCE_POOL = new ArrayDeque<>(); private static final ArrayDeque<MessageParams> MESSAGE_PARAMS_INSTANCE_POOL = new ArrayDeque<>();
private static final Object QUEUE_SECURE_LOCK = new Object();
private final MediaCodec codec; private final MediaCodec codec;
private final HandlerThread handlerThread; private final HandlerThread handlerThread;
private @MonotonicNonNull Handler handler; private @MonotonicNonNull Handler handler;
private final AtomicReference<@NullableType RuntimeException> pendingRuntimeException; private final AtomicReference<@NullableType RuntimeException> pendingRuntimeException;
private final ConditionVariable conditionVariable; private final ConditionVariable conditionVariable;
private final boolean needsSynchronizationWorkaround;
private boolean started; private boolean started;
/** /**
...@@ -76,6 +79,7 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque ...@@ -76,6 +79,7 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque
this.handlerThread = handlerThread; this.handlerThread = handlerThread;
this.conditionVariable = conditionVariable; this.conditionVariable = conditionVariable;
pendingRuntimeException = new AtomicReference<>(); pendingRuntimeException = new AtomicReference<>();
needsSynchronizationWorkaround = needsSynchronizationWorkaround();
} }
@Override @Override
...@@ -207,7 +211,13 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque ...@@ -207,7 +211,13 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque
private void doQueueSecureInputBuffer( private void doQueueSecureInputBuffer(
int index, int offset, MediaCodec.CryptoInfo info, long presentationTimeUs, int flags) { int index, int offset, MediaCodec.CryptoInfo info, long presentationTimeUs, int flags) {
try { try {
codec.queueSecureInputBuffer(index, offset, info, presentationTimeUs, flags); if (needsSynchronizationWorkaround) {
synchronized (QUEUE_SECURE_LOCK) {
codec.queueSecureInputBuffer(index, offset, info, presentationTimeUs, flags);
}
} else {
codec.queueSecureInputBuffer(index, offset, info, presentationTimeUs, flags);
}
} catch (RuntimeException e) { } catch (RuntimeException e) {
setPendingRuntimeException(e); setPendingRuntimeException(e);
} }
...@@ -260,6 +270,15 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque ...@@ -260,6 +270,15 @@ class AsynchronousMediaCodecBufferEnqueuer implements MediaCodecInputBufferEnque
} }
} }
/**
* Returns whether this device needs the synchronization workaround when queueing secure input
* buffers (see [Internal: b/149908061]).
*/
private static boolean needsSynchronizationWorkaround() {
String manufacturer = Util.toLowerInvariant(Util.MANUFACTURER);
return manufacturer.contains("samsung") || manufacturer.contains("motorola");
}
private static String createThreadLabel(int trackType) { private static String createThreadLabel(int trackType) {
StringBuilder labelBuilder = new StringBuilder("MediaCodecInputBufferEnqueuer:"); StringBuilder labelBuilder = new StringBuilder("MediaCodecInputBufferEnqueuer:");
if (trackType == C.TRACK_TYPE_AUDIO) { if (trackType == C.TRACK_TYPE_AUDIO) {
......
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