From 904f86152ea7693f481e29cdfd8327ac58eb8c34 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Sun, 10 Nov 2024 09:00:32 +0100 Subject: [PATCH] Move mediaCodec.stop() to finally block This will allow stopping MediaCodec only after the cleanup of other components which must be performed beforehand. PR #5455 --- .../com/genymobile/scrcpy/video/SurfaceEncoder.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java index a00a8236..dcb5d648 100644 --- a/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/video/SurfaceEncoder.java @@ -86,6 +86,7 @@ public class SurfaceEncoder implements AsyncProcessor { format.setInteger(MediaFormat.KEY_HEIGHT, size.getHeight()); Surface surface = null; + boolean mediaCodecStarted = false; try { mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); surface = mediaCodec.createInputSurface(); @@ -93,6 +94,7 @@ public class SurfaceEncoder implements AsyncProcessor { capture.start(surface); mediaCodec.start(); + mediaCodecStarted = true; // Set the MediaCodec instance to "interrupt" (by signaling an EOS) on reset reset.setRunningMediaCodec(mediaCodec); @@ -108,9 +110,6 @@ public class SurfaceEncoder implements AsyncProcessor { // The capture might have been closed internally (for example if the camera is disconnected) alive = !stopped.get() && !capture.isClosed(); } - - // do not call stop() on exception, it would trigger an IllegalStateException - mediaCodec.stop(); } catch (IllegalStateException | IllegalArgumentException e) { Ln.e("Encoding error: " + e.getClass().getName() + ": " + e.getMessage()); if (!prepareRetry(size)) { @@ -119,6 +118,13 @@ public class SurfaceEncoder implements AsyncProcessor { alive = true; } finally { reset.setRunningMediaCodec(null); + if (mediaCodecStarted) { + try { + mediaCodec.stop(); + } catch (IllegalStateException e) { + // ignore (just in case) + } + } mediaCodec.reset(); if (surface != null) { surface.release();