Skip to content

Display and control your Android device

Add workaround to capture audio on Android 11

Simon Chande40cac6ad92
diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioCaptureForegroundException.java b/server/src/main/java/com/genymobile/scrcpy/AudioCaptureForegroundException.java
new file mode 100644
index 00000000..baa7d846
--- /dev/null
+++ b/server/src/main/java/com/genymobile/scrcpy/AudioCaptureForegroundException.java
@@ -0,0 +1,7 @@
+package com.genymobile.scrcpy;
+
+/**
+ * Exception thrown if audio capture failed on Android 11 specifically because the running App (shell) was not in foreground.
+ */
+public class AudioCaptureForegroundException extends Exception {
+}
diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java b/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java
index 1ce4107f..cc786bdb 100644
--- a/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java
+++ b/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java
@@ -1,7 +1,11 @@
package com.genymobile.scrcpy;
+import com.genymobile.scrcpy.wrappers.ServiceManager;
+
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
+import android.content.ComponentName;
+import android.content.Intent;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.AudioTimestamp;
@@ -12,6 +16,7 @@ import android.os.Build;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
+import android.os.SystemClock;
import java.io.IOException;
import java.nio.ByteBuffer;
@@ -179,7 +184,7 @@ public final class AudioEncoder {
thread = new Thread(() -> {
try {
encode();
- } catch (ConfigurationException e) {
+ } catch (ConfigurationException | AudioCaptureForegroundException e) {
// Do not print stack trace, a user-friendly error-message has already been logged
} catch (IOException e) {
Ln.e("Audio encoding error", e);
@@ -218,8 +223,34 @@ public final class AudioEncoder {
}
}
+ private static void startWorkaroundAndroid11() {
+ if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
+ // Android 11 requires Apps to be at foreground to record audio.
+ // Normally, each App has its own user ID, so Android checks whether the requesting App has the user ID that's at the foreground.
+ // But scrcpy server is NOT an App, it's a Java application started from Android shell, so it has the same user ID (2000) with Android
+ // shell ("com.android.shell").
+ // If there is an Activity from Android shell running at foreground, then the permission system will believe scrcpy is also in the
+ // foreground.
+ if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
+ Intent intent = new Intent(Intent.ACTION_MAIN);
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ intent.addCategory(Intent.CATEGORY_LAUNCHER);
+ intent.setComponent(new ComponentName(FakeContext.PACKAGE_NAME, "com.android.shell.HeapDumpActivity"));
+ ServiceManager.getActivityManager().startActivityAsUserWithFeature(intent);
+ // Wait for activity to start
+ SystemClock.sleep(150);
+ }
+ }
+ }
+
+ private static void stopWorkaroundAndroid11() {
+ if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
+ ServiceManager.getActivityManager().forceStopPackage(FakeContext.PACKAGE_NAME);
+ }
+ }
+
@TargetApi(Build.VERSION_CODES.M)
- public void encode() throws IOException, ConfigurationException {
+ public void encode() throws IOException, ConfigurationException, AudioCaptureForegroundException {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
Ln.w("Audio disabled: it is not supported before Android 11");
streamer.writeDisableStream(false);
@@ -242,8 +273,20 @@ public final class AudioEncoder {
mediaCodec.setCallback(new EncoderCallback(), new Handler(mediaCodecThread.getLooper()));
mediaCodec.configure(format, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE);
- recorder = createAudioRecord();
- recorder.startRecording();
+ startWorkaroundAndroid11();
+ try {
+ recorder = createAudioRecord();
+ recorder.startRecording();
+ } catch (UnsupportedOperationException e) {
+ if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) {
+ Ln.e("Failed to start audio capture");
+ Ln.e("On Android 11, it is only possible to capture in foreground, make sure that the device is unlocked when starting scrcpy.");
+ throw new AudioCaptureForegroundException();
+ }
+ throw e;
+ } finally {
+ stopWorkaroundAndroid11();
+ }
recorderStarted = true;
final MediaCodec mediaCodecRef = mediaCodec;
diff --git a/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java b/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java
index 76aab5f1..aaf83d66 100644
--- a/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java
+++ b/server/src/main/java/com/genymobile/scrcpy/wrappers/ActivityManager.java
@@ -3,7 +3,12 @@ package com.genymobile.scrcpy.wrappers;
import com.genymobile.scrcpy.FakeContext;
import com.genymobile.scrcpy.Ln;
+import android.annotation.SuppressLint;
+import android.annotation.TargetApi;
+import android.content.Intent;
import android.os.Binder;
+import android.os.Build;
+import android.os.Bundle;
import android.os.IBinder;
import android.os.IInterface;
@@ -11,12 +16,15 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
+@SuppressLint("PrivateApi,DiscouragedPrivateApi")
public class ActivityManager {
private final IInterface manager;
private Method getContentProviderExternalMethod;
private boolean getContentProviderExternalMethodNewVersion = true;
private Method removeContentProviderExternalMethod;
+ private Method startActivityAsUserWithFeatureMethod;
+ private Method forceStopPackageMethod;
public ActivityManager(IInterface manager) {
this.manager = manager;
@@ -43,6 +51,7 @@ public class ActivityManager {
return removeContentProviderExternalMethod;
}
+ @TargetApi(Build.VERSION_CODES.Q)
private ContentProvider getContentProviderExternal(String name, IBinder token) {
try {
Method method = getGetContentProviderExternalMethod();
@@ -85,4 +94,55 @@ public class ActivityManager {
public ContentProvider createSettingsProvider() {
return getContentProviderExternal("settings", new Binder());
}
+
+ private Method getStartActivityAsUserWithFeatureMethod() throws NoSuchMethodException, ClassNotFoundException {
+ if (startActivityAsUserWithFeatureMethod == null) {
+ Class<?> iApplicationThreadClass = Class.forName("android.app.IApplicationThread");
+ Class<?> profilerInfo = Class.forName("android.app.ProfilerInfo");
+ startActivityAsUserWithFeatureMethod = manager.getClass()
+ .getMethod("startActivityAsUserWithFeature", iApplicationThreadClass, String.class, String.class, Intent.class, String.class,
+ IBinder.class, String.class, int.class, int.class, profilerInfo, Bundle.class, int.class);
+ }
+ return startActivityAsUserWithFeatureMethod;
+ }
+
+ @SuppressWarnings("ConstantConditions")
+ public int startActivityAsUserWithFeature(Intent intent) {
+ try {
+ Method method = getStartActivityAsUserWithFeatureMethod();
+ return (int) method.invoke(
+ /* this */ manager,
+ /* caller */ null,
+ /* callingPackage */ FakeContext.PACKAGE_NAME,
+ /* callingFeatureId */ null,
+ /* intent */ intent,
+ /* resolvedType */ null,
+ /* resultTo */ null,
+ /* resultWho */ null,
+ /* requestCode */ 0,
+ /* startFlags */ 0,
+ /* profilerInfo */ null,
+ /* bOptions */ null,
+ /* userId */ /* UserHandle.USER_CURRENT */ -2);
+ } catch (Throwable e) {
+ Ln.e("Could not invoke method", e);
+ return 0;
+ }
+ }
+
+ private Method getForceStopPackageMethod() throws NoSuchMethodException {
+ if (forceStopPackageMethod == null) {
+ forceStopPackageMethod = manager.getClass().getMethod("forceStopPackage", String.class, int.class);
+ }
+ return forceStopPackageMethod;
+ }
+
+ public void forceStopPackage(String packageName) {
+ try {
+ Method method = getForceStopPackageMethod();
+ method.invoke(manager, packageName, /* userId */ /* UserHandle.USER_CURRENT */ -2);
+ } catch (Throwable e) {
+ Ln.e("Could not invoke method", e);
+ }
+ }
}