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.
This commit is contained in:
Romain Vimont 2023-02-06 13:46:19 +01:00
parent ae9b08b905
commit c226797991
3 changed files with 14 additions and 6 deletions

View file

@ -31,7 +31,8 @@ public class ScreenEncoder implements Device.RotationListener {
private final AtomicBoolean rotationChanged = new AtomicBoolean(); private final AtomicBoolean rotationChanged = new AtomicBoolean();
private final String videoMimeType; private final Device device;
private final VideoStreamer streamer;
private final String encoderName; private final String encoderName;
private final List<CodecOption> codecOptions; private final List<CodecOption> codecOptions;
private final int bitRate; private final int bitRate;
@ -41,8 +42,10 @@ public class ScreenEncoder implements Device.RotationListener {
private boolean firstFrameSent; private boolean firstFrameSent;
private int consecutiveErrors; private int consecutiveErrors;
public ScreenEncoder(String videoMimeType, int bitRate, int maxFps, List<CodecOption> codecOptions, String encoderName, boolean downsizeOnError) { public ScreenEncoder(Device device, VideoStreamer streamer, int bitRate, int maxFps, List<CodecOption> codecOptions, String encoderName,
this.videoMimeType = videoMimeType; boolean downsizeOnError) {
this.device = device;
this.streamer = streamer;
this.bitRate = bitRate; this.bitRate = bitRate;
this.maxFps = maxFps; this.maxFps = maxFps;
this.codecOptions = codecOptions; this.codecOptions = codecOptions;
@ -59,7 +62,8 @@ public class ScreenEncoder implements Device.RotationListener {
return rotationChanged.getAndSet(false); 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); MediaCodec codec = createCodec(videoMimeType, encoderName);
MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions); MediaFormat format = createFormat(videoMimeType, bitRate, maxFps, codecOptions);
IBinder display = createDisplay(); IBinder display = createDisplay();

View file

@ -101,11 +101,11 @@ public final class Server {
} }
VideoStreamer videoStreamer = new VideoStreamer(connection.getVideoFd(), codec, options.getSendCodecId(), options.getSendFrameMeta()); 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()); options.getEncoderName(), options.getDownsizeOnError());
try { try {
// synchronous // synchronous
screenEncoder.streamScreen(device, videoStreamer); screenEncoder.streamScreen();
} catch (IOException e) { } catch (IOException e) {
// Broken pipe is expected on close, because the socket is closed by the client // Broken pipe is expected on close, because the socket is closed by the client
if (!IO.isBrokenPipe(e)) { if (!IO.isBrokenPipe(e)) {

View file

@ -25,6 +25,10 @@ public final class VideoStreamer {
this.sendFrameMeta = sendFrameMeta; this.sendFrameMeta = sendFrameMeta;
} }
public VideoCodec getCodec() {
return codec;
}
public void writeHeader() throws IOException { public void writeHeader() throws IOException {
if (sendCodecId) { if (sendCodecId) {
ByteBuffer buffer = ByteBuffer.allocate(4); ByteBuffer buffer = ByteBuffer.allocate(4);