From c2267979917215415befb67115bce5cbca1e5006 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 6 Feb 2023 13:46:19 +0100 Subject: [PATCH] Pass all args to ScreenEncoder constructor There is no good reason to pass some of them in the constructor and some others as parameters of the streamScreen() method. --- .../java/com/genymobile/scrcpy/ScreenEncoder.java | 12 ++++++++---- .../src/main/java/com/genymobile/scrcpy/Server.java | 4 ++-- .../java/com/genymobile/scrcpy/VideoStreamer.java | 4 ++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index c86bc9a5..6f47c7f0 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -31,7 +31,8 @@ public class ScreenEncoder implements Device.RotationListener { private final AtomicBoolean rotationChanged = new AtomicBoolean(); - private final String videoMimeType; + private final Device device; + private final VideoStreamer streamer; private final String encoderName; private final List codecOptions; private final int bitRate; @@ -41,8 +42,10 @@ public class ScreenEncoder implements Device.RotationListener { private boolean firstFrameSent; private int consecutiveErrors; - public ScreenEncoder(String videoMimeType, int bitRate, int maxFps, List codecOptions, String encoderName, boolean downsizeOnError) { - this.videoMimeType = videoMimeType; + public ScreenEncoder(Device device, VideoStreamer streamer, int bitRate, int maxFps, List codecOptions, String encoderName, + boolean downsizeOnError) { + this.device = device; + this.streamer = streamer; this.bitRate = bitRate; this.maxFps = maxFps; this.codecOptions = codecOptions; @@ -59,7 +62,8 @@ public class ScreenEncoder implements Device.RotationListener { return rotationChanged.getAndSet(false); } - public void streamScreen(Device device, VideoStreamer streamer) throws IOException, ConfigurationException { + public void streamScreen() throws IOException, ConfigurationException { + String videoMimeType = streamer.getCodec().getMimeType(); MediaCodec codec = createCodec(videoMimeType, encoderName); MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions); IBinder display = createDisplay(); diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index ed1dc1bb..85234c4e 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -101,11 +101,11 @@ public final class Server { } VideoStreamer videoStreamer = new VideoStreamer(connection.getVideoFd(), codec, options.getSendCodecId(), options.getSendFrameMeta()); - ScreenEncoder screenEncoder = new ScreenEncoder(codec.getMimeType(), options.getBitRate(), options.getMaxFps(), codecOptions, + ScreenEncoder screenEncoder = new ScreenEncoder(device, videoStreamer, options.getBitRate(), options.getMaxFps(), codecOptions, options.getEncoderName(), options.getDownsizeOnError()); try { // synchronous - screenEncoder.streamScreen(device, videoStreamer); + screenEncoder.streamScreen(); } catch (IOException e) { // Broken pipe is expected on close, because the socket is closed by the client if (!IO.isBrokenPipe(e)) { diff --git a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java index 5858d7d8..24f48082 100644 --- a/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java +++ b/server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java @@ -25,6 +25,10 @@ public final class VideoStreamer { this.sendFrameMeta = sendFrameMeta; } + public VideoCodec getCodec() { + return codec; + } + public void writeHeader() throws IOException { if (sendCodecId) { ByteBuffer buffer = ByteBuffer.allocate(4);