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.
This commit is contained in:
Romain Vimont 2018-02-07 16:22:56 +01:00
parent d5acc8adc5
commit 5f51d605c0

View file

@ -53,12 +53,12 @@ public class ScreenEncoder implements Device.RotationListener {
public void streamScreen(Device device, OutputStream outputStream) throws IOException { public void streamScreen(Device device, OutputStream outputStream) throws IOException {
MediaFormat format = createFormat(bitRate, frameRate, iFrameInterval); MediaFormat format = createFormat(bitRate, frameRate, iFrameInterval);
MediaCodec codec = createCodec();
IBinder display = createDisplay();
device.setRotationListener(this); device.setRotationListener(this);
boolean alive; boolean alive;
try { try {
do { do {
MediaCodec codec = createCodec();
IBinder display = createDisplay();
Rect deviceRect = device.getScreenInfo().getDeviceSize().toRect(); Rect deviceRect = device.getScreenInfo().getDeviceSize().toRect();
Rect videoRect = device.getScreenInfo().getVideoSize().toRect(); Rect videoRect = device.getScreenInfo().getVideoSize().toRect();
setSize(format, videoRect.width(), videoRect.height()); setSize(format, videoRect.width(), videoRect.height());
@ -70,13 +70,13 @@ public class ScreenEncoder implements Device.RotationListener {
alive = encode(codec, outputStream); alive = encode(codec, outputStream);
} finally { } finally {
codec.stop(); codec.stop();
destroyDisplay(display);
codec.release();
surface.release(); surface.release();
} }
} while (alive); } while (alive);
} finally { } finally {
device.setRotationListener(null); device.setRotationListener(null);
destroyDisplay(display);
codec.release();
} }
} }