Extract retry handling

Move the code to downscale and retry on error out of the catch-block.

Refs 26b4104844
This commit is contained in:
Romain Vimont 2023-01-27 23:03:41 +01:00
parent a9b2697f3e
commit a52053421a

View file

@ -108,20 +108,10 @@ public class ScreenEncoder implements Device.RotationListener {
codec.stop(); codec.stop();
} catch (IllegalStateException | IllegalArgumentException e) { } catch (IllegalStateException | IllegalArgumentException e) {
Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage()); Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage());
if (!downsizeOnError || firstFrameSent) { if (!prepareRetry(device, screenInfo)) {
// Fail immediately
throw e; throw e;
} }
Ln.i("Retrying...");
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);
alive = true; alive = true;
} finally { } finally {
codec.reset(); 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) { private static int chooseMaxSizeFallback(Size failedSize) {
int currentMaxSize = Math.max(failedSize.getWidth(), failedSize.getHeight()); int currentMaxSize = Math.max(failedSize.getWidth(), failedSize.getHeight());
for (int value : MAX_SIZE_FALLBACK) { for (int value : MAX_SIZE_FALLBACK) {