From 51628201b77c7f7c990e26471aa88c5ef930061c Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 6 Feb 2023 11:57:37 +0100 Subject: [PATCH] Write streamer header from ScreenEncoder The screen encoder is responsible for writing data to the video streamer. --- .../com/genymobile/scrcpy/ScreenEncoder.java | 3 +++ .../java/com/genymobile/scrcpy/Server.java | 5 +---- .../com/genymobile/scrcpy/VideoStreamer.java | 18 ++++++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index 3ca2e80c..c86bc9a5 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -64,6 +64,9 @@ public class ScreenEncoder implements Device.RotationListener { MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions); IBinder display = createDisplay(); device.setRotationListener(this); + + streamer.writeHeader(); + boolean alive; try { do { diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index 027050af..d75e8310 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -104,10 +104,7 @@ public final class Server { try { // synchronous - VideoStreamer videoStreamer = new VideoStreamer(connection.getVideoFd(), options.getSendFrameMeta()); - if (options.getSendCodecId()) { - videoStreamer.writeHeader(codec.getId()); - } + VideoStreamer videoStreamer = new VideoStreamer(connection.getVideoFd(), codec, options.getSendCodecId(), options.getSendFrameMeta()); screenEncoder.streamScreen(device, videoStreamer); } catch (IOException e) { // Broken pipe is expected on close, because the socket is closed by the client diff --git a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java index cbde141e..5858d7d8 100644 --- a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java +++ b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java @@ -12,20 +12,26 @@ public final class VideoStreamer { private static final long PACKET_FLAG_KEY_FRAME = 1L << 62; private final FileDescriptor fd; + private final VideoCodec codec; + private final boolean sendCodecId; private final boolean sendFrameMeta; private final ByteBuffer headerBuffer = ByteBuffer.allocate(12); - public VideoStreamer(FileDescriptor fd, boolean sendFrameMeta) { + public VideoStreamer(FileDescriptor fd, VideoCodec codec, boolean sendCodecId, boolean sendFrameMeta) { this.fd = fd; + this.codec = codec; + this.sendCodecId = sendCodecId; this.sendFrameMeta = sendFrameMeta; } - public void writeHeader(int codecId) throws IOException { - ByteBuffer buffer = ByteBuffer.allocate(4); - buffer.putInt(codecId); - buffer.flip(); - IO.writeFully(fd, buffer); + public void writeHeader() throws IOException { + if (sendCodecId) { + ByteBuffer buffer = ByteBuffer.allocate(4); + buffer.putInt(codec.getId()); + buffer.flip(); + IO.writeFully(fd, buffer); + } } public void writePacket(ByteBuffer codecBuffer, MediaCodec.BufferInfo bufferInfo) throws IOException {