Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update scrcpy server to support Android 15 #2

Open
wants to merge 1 commit into
base: feature/websocket-v1.19.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions server/.java-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
system
6 changes: 3 additions & 3 deletions server/build.gradle
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
compileSdkVersion 35
defaultConfig {
applicationId "com.genymobile.scrcpy"
minSdkVersion 21
targetSdkVersion 30
targetSdkVersion 35
versionCode 11900
versionName "1.19-ws5"
versionName "1.19-ws6"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
7 changes: 7 additions & 0 deletions server/src/main/java/com/genymobile/scrcpy/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public final class Device {
public static final int LOCK_VIDEO_ORIENTATION_INITIAL = -2;

private static final ServiceManager SERVICE_MANAGER = new ServiceManager();
public static ServiceManager getServiceManager() {
return SERVICE_MANAGER;
}

public interface RotationListener {
void onRotationChanged(int rotation);
Expand Down Expand Up @@ -136,6 +139,10 @@ public void applyNewVideoSetting(VideoSettings videoSettings) {
this.setScreenInfo(ScreenInfo.computeScreenInfo(Device.getDisplayInfo(displayId), videoSettings));
}

public int getDisplayId() {
return displayId;
}

public synchronized void setScreenInfo(ScreenInfo screenInfo) {
this.screenInfo = screenInfo;
}
Expand Down
32 changes: 28 additions & 4 deletions server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import android.hardware.display.VirtualDisplay;

public class ScreenEncoder implements Connection.StreamInvalidateListener, Runnable {

Expand Down Expand Up @@ -94,10 +95,13 @@ private void internalStreamScreen() throws IOException {
updateFormat();
connection.setStreamInvalidateListener(this);
boolean alive;
IBinder display = null;
VirtualDisplay virtualDisplay = null;

try {
do {
MediaCodec codec = createCodec(videoSettings.getEncoderName());
IBinder display = createDisplay();

ScreenInfo screenInfo = device.getScreenInfo();
Rect contentRect = screenInfo.getContentRect();
// include the locked video orientation
Expand All @@ -110,14 +114,34 @@ private void internalStreamScreen() throws IOException {
setSize(format, videoRect.width(), videoRect.height());
configure(codec, format);
Surface surface = codec.createInputSurface();
setDisplaySurface(display, surface, videoRotation, contentRect, unlockedVideoRect, layerStack);

try {
display = createDisplay();
setDisplaySurface(display, surface, videoRotation, contentRect, unlockedVideoRect, layerStack);
} catch (Exception e) {
try {
virtualDisplay = Device.getServiceManager().getDisplayManager()
.createVirtualDisplay("scrcpy", videoRect.width(), videoRect.height(), device.getDisplayId(), surface);
} catch(Exception x) {

}

}
codec.start();
try {
alive = encode(codec);
// do not call stop() on exception, it would trigger an IllegalStateException
codec.stop();
} finally {
destroyDisplay(display);
if (display != null) {
destroyDisplay(display);
display = null;
}
if (virtualDisplay != null) {
virtualDisplay.release();
virtualDisplay = null;
}

codec.release();
surface.release();
}
Expand Down Expand Up @@ -250,7 +274,7 @@ private static MediaFormat createFormat(VideoSettings videoSettings) {
return format;
}

private static IBinder createDisplay() {
private static IBinder createDisplay() throws Exception {
// Since Android 12 (preview), secure displays could not be created with shell permissions anymore.
// On Android 12 preview, SDK_INT is still R (not S), but CODENAME is "S".
boolean secure = Build.VERSION.SDK_INT < Build.VERSION_CODES.R || (Build.VERSION.SDK_INT == Build.VERSION_CODES.R && !"S"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
import com.genymobile.scrcpy.DisplayInfo;
import com.genymobile.scrcpy.Ln;
import com.genymobile.scrcpy.Size;
import android.hardware.display.VirtualDisplay;


import android.os.IInterface;
import android.view.Display;
import android.view.Surface;

import java.lang.reflect.Method;

public final class DisplayManager {
private final IInterface manager;
private Method createVirtualDisplayMethod;

public DisplayManager(IInterface manager) {
this.manager = manager;
Expand Down Expand Up @@ -49,4 +53,17 @@ public int[] getDisplayIds() {
throw new AssertionError(e);
}
}

private Method getCreateVirtualDisplayMethod() throws NoSuchMethodException {
if (createVirtualDisplayMethod == null) {
createVirtualDisplayMethod = android.hardware.display.DisplayManager.class
.getMethod("createVirtualDisplay", String.class, int.class, int.class, int.class, Surface.class);
}
return createVirtualDisplayMethod;
}

public VirtualDisplay createVirtualDisplay(String name, int width, int height, int displayIdToMirror, Surface surface) throws Exception {
Method method = getCreateVirtualDisplayMethod();
return (VirtualDisplay) method.invoke(null, name, width, height, displayIdToMirror, surface);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,8 @@ public static void setDisplaySurface(IBinder displayToken, Surface surface) {
}
}

public static IBinder createDisplay(String name, boolean secure) {
try {
return (IBinder) CLASS.getMethod("createDisplay", String.class, boolean.class).invoke(null, name, secure);
} catch (Exception e) {
throw new AssertionError(e);
}
public static IBinder createDisplay(String name, boolean secure) throws Exception {
return (IBinder) CLASS.getMethod("createDisplay", String.class, boolean.class).invoke(null, name, secure);
}

private static Method getGetBuiltInDisplayMethod() throws NoSuchMethodException {
Expand Down