From 52f85fd6f150907818ccd1213719c1d21aa4684d Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 27 Jan 2023 22:31:09 +0100 Subject: [PATCH] Keep the same MediaCodec instance across sessions Calling codec.reset() is sufficient. --- .../src/main/java/com/genymobile/scrcpy/ScreenEncoder.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index a63886e9..270751f3 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -75,13 +75,13 @@ public class ScreenEncoder implements Device.RotationListener { } private void internalStreamScreen(Device device, FileDescriptor fd) throws IOException { + MediaCodec codec = createCodec(encoderName); MediaFormat format = createFormat(bitRate, maxFps, codecOptions); IBinder display = createDisplay(); device.setRotationListener(this); boolean alive; try { do { - MediaCodec codec = createCodec(encoderName); ScreenInfo screenInfo = device.getScreenInfo(); Rect contentRect = screenInfo.getContentRect(); // include the locked video orientation @@ -120,13 +120,14 @@ public class ScreenEncoder implements Device.RotationListener { device.setMaxSize(newMaxSize); alive = true; } finally { - codec.release(); + codec.reset(); if (surface != null) { surface.release(); } } } while (alive); } finally { + codec.release(); device.setRotationListener(null); SurfaceControl.destroyDisplay(display); }