diff --git a/server/src/main/java/com/genymobile/scrcpy/Server.java b/server/src/main/java/com/genymobile/scrcpy/Server.java index 1380227586..2e6e1d4a99 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Server.java +++ b/server/src/main/java/com/genymobile/scrcpy/Server.java @@ -99,26 +99,7 @@ private static void scrcpy(Options options) throws IOException, ConfigurationExc boolean audio = options.getAudio(); boolean sendDummyByte = options.getSendDummyByte(); - Workarounds.prepareMainLooper(); - - // Workarounds must be applied for Meizu phones: - // - - // - - // - - // - // But only apply when strictly necessary, since workarounds can cause other issues: - // - - // - - if (Build.BRAND.equalsIgnoreCase("meizu")) { - Workarounds.fillAppInfo(); - } - - // Before Android 11, audio is not supported. - // Since Android 12, we can properly set a context on the AudioRecord. - // Only on Android 11 we must fill the application context for the AudioRecord to work. - if (audio && Build.VERSION.SDK_INT == Build.VERSION_CODES.R) { - Workarounds.fillAppContext(); - } + Workarounds.apply(audio); List asyncProcessors = new ArrayList<>(); diff --git a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java index 9ae7983f16..ded1d9cb34 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Workarounds.java +++ b/server/src/main/java/com/genymobile/scrcpy/Workarounds.java @@ -27,8 +27,31 @@ private Workarounds() { // not instantiable } + public static void apply(boolean audio) { + Workarounds.prepareMainLooper(); + + // Workarounds must be applied for Meizu phones: + // - + // - + // - + // + // But only apply when strictly necessary, since workarounds can cause other issues: + // - + // - + if (Build.BRAND.equalsIgnoreCase("meizu")) { + Workarounds.fillAppInfo(); + } + + // Before Android 11, audio is not supported. + // Since Android 12, we can properly set a context on the AudioRecord. + // Only on Android 11 we must fill the application context for the AudioRecord to work. + if (audio && Build.VERSION.SDK_INT == Build.VERSION_CODES.R) { + Workarounds.fillAppContext(); + } + } + @SuppressWarnings("deprecation") - public static void prepareMainLooper() { + private static void prepareMainLooper() { // Some devices internally create a Handler when creating an input Surface, causing an exception: // "Can't create handler inside thread that has not called Looper.prepare()" // @@ -57,7 +80,7 @@ private static void fillActivityThread() throws Exception { } @SuppressLint("PrivateApi,DiscouragedPrivateApi") - public static void fillAppInfo() { + private static void fillAppInfo() { try { fillActivityThread(); @@ -86,7 +109,7 @@ public static void fillAppInfo() { } @SuppressLint("PrivateApi,DiscouragedPrivateApi") - public static void fillAppContext() { + private static void fillAppContext() { try { fillActivityThread();