From 5f51d605c037f75bc266b8d62c5048a803f639cf Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Wed, 7 Feb 2018 16:22:56 +0100 Subject: [PATCH] Recreate codec and display on rotation changed On some devices, we can reuse the same codec and display, but on some others (e.g. Nexus 5X with Android 7.1.2), it crashes on codec.stop() with an IllegalStateException. Therefore, always recreate the codec and display, so that it works on all devices. --- .../main/java/com/genymobile/scrcpy/ScreenEncoder.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index c2d61366..2d056351 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -53,12 +53,12 @@ public class ScreenEncoder implements Device.RotationListener { public void streamScreen(Device device, OutputStream outputStream) throws IOException { MediaFormat format = createFormat(bitRate, frameRate, iFrameInterval); - MediaCodec codec = createCodec(); - IBinder display = createDisplay(); device.setRotationListener(this); boolean alive; try { do { + MediaCodec codec = createCodec(); + IBinder display = createDisplay(); Rect deviceRect = device.getScreenInfo().getDeviceSize().toRect(); Rect videoRect = device.getScreenInfo().getVideoSize().toRect(); setSize(format, videoRect.width(), videoRect.height()); @@ -70,13 +70,13 @@ public class ScreenEncoder implements Device.RotationListener { alive = encode(codec, outputStream); } finally { codec.stop(); + destroyDisplay(display); + codec.release(); surface.release(); } } while (alive); } finally { device.setRotationListener(null); - destroyDisplay(display); - codec.release(); } }