From 08b477e698264671f284f8d35aba9c4311003ea0 Mon Sep 17 00:00:00 2001 From: kiwimchen Date: Sat, 2 Apr 2022 14:52:54 +0800 Subject: [PATCH] support udt timeout codec option Non-negative timeout had been specified in the call to dequeueOutputBuffer, indicates that the call timed out, this will fix idr req not return frame bug --- .../com/genymobile/scrcpy/ScreenEncoder.java | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index c5aef439..8df43159 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -145,7 +145,7 @@ public class ScreenEncoder implements Device.RotationListener { MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); while (!consumeRotationChange() && !eof) { - int outputBufferId = codec.dequeueOutputBuffer(bufferInfo, -1); + int outputBufferId = codec.dequeueOutputBuffer(bufferInfo, timeoutUS); eof = (bufferInfo.flags & MediaCodec.BUFFER_FLAG_END_OF_STREAM) != 0; try { if (consumeRotationChange()) { @@ -165,6 +165,9 @@ public class ScreenEncoder implements Device.RotationListener { firstFrameSent = true; } } + if (outputBufferId == MediaCodec.INFO_TRY_AGAIN_LATER) { + Ln.d("wait dequeueOutputBuffer timeout, retry again later"); + } } finally { if (outputBufferId >= 0) { codec.releaseOutputBuffer(outputBufferId, false); @@ -256,6 +259,9 @@ public class ScreenEncoder implements Device.RotationListener { if (codecOptions != null) { for (CodecOption option : codecOptions) { + if (setUdtCodecOption(format, option)) { + continue; + } setCodecOption(format, option); } } @@ -298,6 +304,9 @@ public class ScreenEncoder implements Device.RotationListener { private MediaCodec codec; private Device.CodecChangeLister codecChangeLister; + private static final String KEY_TIMEOUT = "timeout"; + private static long timeoutUS = -1; + private void initCodec(MediaCodec codec) { this.codec = codec; } @@ -325,4 +334,17 @@ public class ScreenEncoder implements Device.RotationListener { } return codecChangeLister; } + + private static boolean setUdtCodecOption(MediaFormat format, CodecOption codecOption) { + String key = codecOption.getKey(); + if (KEY_TIMEOUT.equals(key)) { + Object value = codecOption.getValue(); + if (value instanceof Integer) { + timeoutUS = (Integer) value * 1000 * 1000; + Ln.d("Udt Codec option set: " + key + " (" + value.getClass().getSimpleName() + ") = " + value); + return true; + } + } + return false; + } }