Skip to content

Commit

Permalink
Adding codecProfile and setCodecProfile to the ScreenEncoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Tzah Mazuz committed Mar 19, 2020
1 parent ef5d72c commit 2505242
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,21 @@ public class ScreenEncoder implements Device.RotationListener {
private int maxFps;
private int lockedVideoOrientation;
private int iFrameInterval;
private int codecProfile;
private boolean sendFrameMeta;
private long ptsOrigin;

public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation, int iFrameInterval) {
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation, int codecProfile, int iFrameInterval) {
this.sendFrameMeta = sendFrameMeta;
this.bitRate = bitRate;
this.maxFps = maxFps;
this.lockedVideoOrientation = lockedVideoOrientation;
this.codecProfile = codecProfile;
this.iFrameInterval = iFrameInterval;
}

public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation) {
this(sendFrameMeta, bitRate, maxFps, lockedVideoOrientation, DEFAULT_I_FRAME_INTERVAL);
public ScreenEncoder(boolean sendFrameMeta, int bitRate, int maxFps, int lockedVideoOrientation, int codecProfile) {
this(sendFrameMeta, bitRate, maxFps, lockedVideoOrientation, codecProfile, DEFAULT_I_FRAME_INTERVAL);
}

@Override
Expand All @@ -64,6 +66,7 @@ public void streamScreen(Device device, FileDescriptor fd) throws IOException {
try {
do {
MediaCodec codec = createCodec();
setCodecProfile(codec, format);
IBinder display = createDisplay();
ScreenInfo screenInfo = device.getScreenInfo();
Rect contentRect = screenInfo.getContentRect();
Expand Down Expand Up @@ -139,6 +142,22 @@ private void writeFrameMeta(FileDescriptor fd, MediaCodec.BufferInfo bufferInfo,
IO.writeFully(fd, headerBuffer);
}

private void setCodecProfile(MediaCodec codec, MediaFormat format) throws IOException {
if(codecProfile == 0) return;
int level = 0;
for (MediaCodecInfo.CodecProfileLevel profileLevel : codec.getCodecInfo().getCapabilitiesForType("video/avc").profileLevels) {
if(profileLevel.profile == codecProfile) {
level = Math.max(level, profileLevel.level);
}
}
if(level == 0) throw new IOException("Device doesn't support the requested codec profile.");
// Profile (SDK Level 21) and Level (SDK Level 23).
format.setInteger(MediaFormat.KEY_PROFILE, codecProfile);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
format.setInteger(MediaFormat.KEY_LEVEL, level);
}
}

private static MediaCodec createCodec() throws IOException {
return MediaCodec.createEncoderByType("video/avc");
}
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/com/genymobile/scrcpy/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private static void scrcpy(Options options) throws IOException {
boolean tunnelForward = options.isTunnelForward();
try (DesktopConnection connection = DesktopConnection.open(device, tunnelForward)) {
ScreenEncoder screenEncoder = new ScreenEncoder(options.getSendFrameMeta(), options.getBitRate(), options.getMaxFps(),
options.getLockedVideoOrientation());
options.getLockedVideoOrientation(), options.getCodecProfile());

if (options.getControl()) {
Controller controller = new Controller(device, connection);
Expand Down

0 comments on commit 2505242

Please sign in to comment.