From a52053421abee7c96b1071a06cb68bbeac2fd464 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 27 Jan 2023 23:03:41 +0100 Subject: [PATCH] Extract retry handling Move the code to downscale and retry on error out of the catch-block. Refs 26b4104844fb9516be13ff1f2be34e2945090e79 --- .../com/genymobile/scrcpy/ScreenEncoder.java | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index 37467937..a695f0db 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -108,20 +108,10 @@ public class ScreenEncoder implements Device.RotationListener { codec.stop(); } catch (IllegalStateException | IllegalArgumentException e) { Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage()); - if (!downsizeOnError || firstFrameSent) { - // Fail immediately + if (!prepareRetry(device, screenInfo)) { throw e; } - - int newMaxSize = chooseMaxSizeFallback(screenInfo.getVideoSize()); - if (newMaxSize == 0) { - // Definitively fail - throw e; - } - - // Retry with a smaller device size - Ln.i("Retrying with -m" + newMaxSize + "..."); - device.setMaxSize(newMaxSize); + Ln.i("Retrying..."); alive = true; } finally { codec.reset(); @@ -137,6 +127,25 @@ public class ScreenEncoder implements Device.RotationListener { } } + private boolean prepareRetry(Device device, ScreenInfo screenInfo) { + if (!downsizeOnError || firstFrameSent) { + // Must fail immediately + return false; + } + + int newMaxSize = chooseMaxSizeFallback(screenInfo.getVideoSize()); + Ln.i("newMaxSize = " + newMaxSize); + if (newMaxSize == 0) { + // Must definitively fail + return false; + } + + // Retry with a smaller device size + Ln.i("Retrying with -m" + newMaxSize + "..."); + device.setMaxSize(newMaxSize); + return true; + } + private static int chooseMaxSizeFallback(Size failedSize) { int currentMaxSize = Math.max(failedSize.getWidth(), failedSize.getHeight()); for (int value : MAX_SIZE_FALLBACK) {