From ae29e5b56200035a81c7321c1617fe05928fc5b0 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 6 Feb 2023 11:44:18 +0100 Subject: [PATCH] Use VideoStreamer directly from ScreenEncoder The Callbacks interface notifies new packets. But in addition, the screen encoder will need to write headers on start. We could add a function onStart(), but for simplicity, just remove the interface, which brings no value, and call the streamer directly. Refs 87972e2022686b1176bfaf0c678e703856c2b027 --- .../java/com/genymobile/scrcpy/ScreenEncoder.java | 12 ++++-------- .../java/com/genymobile/scrcpy/VideoStreamer.java | 5 ++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index 44ba75d1..3ca2e80c 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -21,10 +21,6 @@ import java.util.concurrent.atomic.AtomicBoolean; public class ScreenEncoder implements Device.RotationListener { - public interface Callbacks { - void onPacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException; - } - private static final int DEFAULT_I_FRAME_INTERVAL = 10; // seconds private static final int REPEAT_FRAME_DELAY_US = 100_000; // repeat after 100ms private static final String KEY_MAX_FPS_TO_ENCODER = "max-fps-to-encoder"; @@ -63,7 +59,7 @@ public class ScreenEncoder implements Device.RotationListener { return rotationChanged.getAndSet(false); } - public void streamScreen(Device device, Callbacks callbacks) throws IOException, ConfigurationException { + public void streamScreen(Device device, VideoStreamer streamer) throws IOException, ConfigurationException { MediaCodec codec = createCodec(videoMimeType, encoderName); MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions); IBinder display = createDisplay(); @@ -92,7 +88,7 @@ public class ScreenEncoder implements Device.RotationListener { codec.start(); - alive = encode(codec, callbacks); + alive = encode(codec, streamer); // do not call stop() on exception, it would trigger an IllegalStateException codec.stop(); } catch (IllegalStateException | IllegalArgumentException e) { @@ -161,7 +157,7 @@ public class ScreenEncoder implements Device.RotationListener { return 0; } - private boolean encode(MediaCodec codec, Callbacks callbacks) throws IOException { + private boolean encode(MediaCodec codec, VideoStreamer streamer) throws IOException { boolean eof = false; MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); @@ -184,7 +180,7 @@ public class ScreenEncoder implements Device.RotationListener { consecutiveErrors = 0; } - callbacks.onPacket(codecBuffer, bufferInfo); + streamer.writePacket(codecBuffer, bufferInfo); } } finally { if (outputBufferId >= 0) { diff --git a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java index 943c641d..cbde141e 100644 --- a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java +++ b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java @@ -6,7 +6,7 @@ import java.io.FileDescriptor; import java.io.IOException; import java.nio.ByteBuffer; -public final class VideoStreamer implements ScreenEncoder.Callbacks { +public final class VideoStreamer { private static final long PACKET_FLAG_CONFIG = 1L << 63; private static final long PACKET_FLAG_KEY_FRAME = 1L << 62; @@ -28,8 +28,7 @@ public final class VideoStreamer implements ScreenEncoder.Callbacks { IO.writeFully(fd, buffer); } - @Override - public void onPacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException { + public void writePacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException { if (sendFrameMeta) { writeFrameMeta(fd, bufferInfo, codecBuffer.remaining()); }