Skip to content

Commit 3c670dc

Browse files
committed
Make streamer independent of codec type
Rename VideoStreamer to Streamer, and extract a Codec interface which will also support audio codecs. PR #3757 <#3757>
1 parent af1f00b commit 3c670dc

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.genymobile.scrcpy;
2+
3+
public interface Codec {
4+
5+
enum Type {
6+
VIDEO,
7+
}
8+
9+
Type getType();
10+
11+
int getId();
12+
13+
String getName();
14+
15+
String getMimeType();
16+
}

server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class ScreenEncoder implements Device.RotationListener {
3232
private final AtomicBoolean rotationChanged = new AtomicBoolean();
3333

3434
private final Device device;
35-
private final VideoStreamer streamer;
35+
private final Streamer streamer;
3636
private final String encoderName;
3737
private final List<CodecOption> codecOptions;
3838
private final int bitRate;
@@ -42,7 +42,7 @@ public class ScreenEncoder implements Device.RotationListener {
4242
private boolean firstFrameSent;
4343
private int consecutiveErrors;
4444

45-
public ScreenEncoder(Device device, VideoStreamer streamer, int bitRate, int maxFps, List<CodecOption> codecOptions, String encoderName,
45+
public ScreenEncoder(Device device, Streamer streamer, int bitRate, int maxFps, List<CodecOption> codecOptions, String encoderName,
4646
boolean downsizeOnError) {
4747
this.device = device;
4848
this.streamer = streamer;
@@ -164,7 +164,7 @@ private static int chooseMaxSizeFallback(Size failedSize) {
164164
return 0;
165165
}
166166

167-
private boolean encode(MediaCodec codec, VideoStreamer streamer) throws IOException {
167+
private boolean encode(MediaCodec codec, Streamer streamer) throws IOException {
168168
boolean eof = false;
169169
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
170170

server/src/main/java/com/genymobile/scrcpy/Server.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@ private static void scrcpy(Options options) throws IOException, ConfigurationExc
100100
device.setClipboardListener(text -> controllerRef.getSender().pushClipboardText(text));
101101
}
102102

103-
VideoStreamer videoStreamer = new VideoStreamer(connection.getVideoFd(), codec, options.getSendCodecId(), options.getSendFrameMeta());
104-
ScreenEncoder screenEncoder = new ScreenEncoder(device, videoStreamer, options.getBitRate(), options.getMaxFps(), codecOptions,
105-
options.getEncoderName(), options.getDownsizeOnError());
103+
Streamer videoStreamer = new Streamer(connection.getVideoFd(), codec, options.getSendCodecId(), options.getSendFrameMeta());
104+
ScreenEncoder screenEncoder = new ScreenEncoder(device, videoStreamer, options.getBitRate(), options.getMaxFps(),
105+
codecOptions, options.getEncoderName(), options.getDownsizeOnError());
106106
try {
107107
// synchronous
108108
screenEncoder.streamScreen();

server/src/main/java/com/genymobile/scrcpy/VideoStreamer.java renamed to server/src/main/java/com/genymobile/scrcpy/Streamer.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,26 @@
66
import java.io.IOException;
77
import java.nio.ByteBuffer;
88

9-
public final class VideoStreamer {
9+
public final class Streamer {
1010

1111
private static final long PACKET_FLAG_CONFIG = 1L << 63;
1212
private static final long PACKET_FLAG_KEY_FRAME = 1L << 62;
1313

1414
private final FileDescriptor fd;
15-
private final VideoCodec codec;
15+
private final Codec codec;
1616
private final boolean sendCodecId;
1717
private final boolean sendFrameMeta;
1818

1919
private final ByteBuffer headerBuffer = ByteBuffer.allocate(12);
2020

21-
public VideoStreamer(FileDescriptor fd, VideoCodec codec, boolean sendCodecId, boolean sendFrameMeta) {
21+
public Streamer(FileDescriptor fd, Codec codec, boolean sendCodecId, boolean sendFrameMeta) {
2222
this.fd = fd;
2323
this.codec = codec;
2424
this.sendCodecId = sendCodecId;
2525
this.sendFrameMeta = sendFrameMeta;
2626
}
2727

28-
public VideoCodec getCodec() {
28+
public Codec getCodec() {
2929
return codec;
3030
}
3131

server/src/main/java/com/genymobile/scrcpy/VideoCodec.java

+13-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import android.annotation.SuppressLint;
44
import android.media.MediaFormat;
55

6-
public enum VideoCodec {
6+
public enum VideoCodec implements Codec {
77
H264(0x68_32_36_34, "h264", MediaFormat.MIMETYPE_VIDEO_AVC),
88
H265(0x68_32_36_35, "h265", MediaFormat.MIMETYPE_VIDEO_HEVC),
99
@SuppressLint("InlinedApi") // introduced in API 21
@@ -19,10 +19,22 @@ public enum VideoCodec {
1919
this.mimeType = mimeType;
2020
}
2121

22+
@Override
23+
public Type getType() {
24+
return Type.VIDEO;
25+
}
26+
27+
@Override
2228
public int getId() {
2329
return id;
2430
}
2531

32+
@Override
33+
public String getName() {
34+
return name;
35+
}
36+
37+
@Override
2638
public String getMimeType() {
2739
return mimeType;
2840
}

0 commit comments

Comments
 (0)