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

Not work audio streaming on android 11 #3796

Closed
2 tasks done
bropines opened this issue Mar 12, 2023 · 24 comments
Closed
2 tasks done

Not work audio streaming on android 11 #3796

bropines opened this issue Mar 12, 2023 · 24 comments

Comments

@bropines
Copy link

bropines commented Mar 12, 2023

  • I have read the FAQ.
  • I have searched in existing issues.

Environment

  • OS: Windows 11
  • scrcpy version: scrcpy 2.0
  • installation method: Windows release...
  • device model: Redmi Note 8T
  • Android version: 11

Describe the bug
The sound just doesn't work. When you try to make it forcibly this happens

PS Programs\scrcpy-win64-v2.0> .\scrcpy-console.bat -d --require-audio
scrcpy 2.0 <https://github.com/Genymobile/scrcpy>
C:\Users\bropi\Programs\scrcpy-win64-v2.0\scrcpy-server: 1 file pushed, 0 skipped. 62.6 MB/s (52867 bytes in 0.001s)
[server] INFO: Device: Xiaomi Redmi Note 8T (Android 11)
INFO: Renderer: direct3d
INFO: Initial texture: 1080x2336
[server] ERROR: Failed to start audio capture
[server] ERROR: On Android 11, it is only possible to capture in foreground, make sure that the device is unlocked when starting scrcpy.
WARN: Demuxer 'audio': stream explicitly disabled by the device
ERROR: Demuxer error
WARN: Killing the server... ```


Please do not post screenshots of your terminal, just post the content as text instead.
@rom1v
Copy link
Collaborator

rom1v commented Mar 12, 2023

Dis you follow the instructions specific to Android 11? https://github.com/Genymobile/scrcpy/blob/master/doc/audio.md

@bropines
Copy link
Author

Do you follow the instructions specific to Android 11? https://github.com/Genymobile/scrcpy/blob/master/doc/audio.md

Yep. The screen is on, the developer settings do not block access to the sound. I also tried it with the --require-audio parameter the result is zero. It just won't start.

@rom1v
Copy link
Collaborator

rom1v commented Mar 12, 2023

For testing, could you please replace the server in the scrcpy 2.0 release folder:

  • scrcpy-server SHA-256: 68f9c478177e543a180ee9460eb63b6969c0501ef22280fe00fe51d4310fe80f

It disables the workaround for Android 11:

diff
diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java
index 9228e3d71..6d336ac66 100644
--- a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java
+++ b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java
@@ -85,7 +85,7 @@ public final class AudioCapture {
     }
 
     public void start() throws AudioCaptureForegroundException {
-        startWorkaroundAndroid11();
+        //startWorkaroundAndroid11();
         try {
             recorder = createAudioRecord();
             recorder.startRecording();
@@ -97,7 +97,7 @@ public final class AudioCapture {
             }
             throw e;
         } finally {
-            stopWorkaroundAndroid11();
+            //stopWorkaroundAndroid11();
         }
     }
 

Then, unlock your device, and execute:

adb shell am start -n com.android.shell/.HeapDumpActivity

It should open a popup. Keep it open and visible (your device must be kept unlocked and powered on).

Then run scrcpy. Does audio capture work in this case?

@bropines
Copy link
Author

bropines commented Mar 12, 2023

For testing, could you please replace the server in the scrcpy 2.0 release folder:

  • scrcpy-server SHA-256: 68f9c478177e543a180ee9460eb63b6969c0501ef22280fe00fe51d4310fe80f

It disables the workaround for Android 11:

diff
Then, unlock your device, and execute:

adb shell am start -n com.android.shell/.HeapDumpActivity

It should open a popup. Keep it open and visible (your device must be kept unlocked and powered on).

Then run scrcpy. Does audio capture work in this case?

Yes. It works. Even if you press OK at the popup, the sound keeps streaming. And it only works the way you said it would.

This is the window that pops up (sorry for the Russian lang in system)
image

@rom1v
Copy link
Collaborator

rom1v commented Mar 12, 2023

OK, so the culprit is probably the delay here:

Are you comfortable to build the app from sources? You could try to adjust the delay until you get a value which works for your device. I'm interested in that value :)

@bropines
Copy link
Author

Are you comfortable to build the app from sources? You could try to adjust the delay until you get a value which works for your device. I'm interested in that value :)

It doesn't look hard, I'm going to try it now

@bropines
Copy link
Author

OK, so the culprit is probably the delay here:

Are you comfortable to build the app from sources? You could try to adjust the delay until you get a value which works for your device. I'm interested in that value :)

I would like to clarify. Which way should I change the delay? Higher or lower?

@rom1v
Copy link
Collaborator

rom1v commented Mar 12, 2023

It seems the delay is insufficient in your case, so you must use an higher delay.

@bropines
Copy link
Author

bropines commented Mar 12, 2023

It seems the delay is insufficient in your case, so you must use an higher delay.

I'm kind of dumb.
image
image

@bropines
Copy link
Author

It seems the delay is insufficient in your case, so you must use an higher delay.

I'm kind of dumb. image image

That's the error thing because I was using kali -_-

@bropines
Copy link
Author

After 5 hours of misunderstandings with Linux I finally installed it. Delay of 300 (units, seconds, whatever) is working

@rom1v
Copy link
Collaborator

rom1v commented Mar 12, 2023

Thank you. Does it 100% work with 300ms? I will probably increase the current value to 300 then.

@bropines
Copy link
Author

Thank you. Does it 100% work with 300ms? I will probably increase the current value to 300 then.

It worked at 200 every other time, I have not tested at 250. I can check later. But at 300 it definitely works.

@drobotk
Copy link

drobotk commented Mar 13, 2023

Hi

I have an issue that seems related. For some reason audio works fine for me on Windows, but fails on Linux, with the same error:

[server] ERROR: Failed to start audio capture
[server] ERROR: On Android 11, it is only possible to capture in foreground, make sure that the device is unlocked when starting scrcpy.

I haven't tried changing that delay yet, but it seems a bit weird that the Linux version would require a different delay than the Windows version.

@rom1v
Copy link
Collaborator

rom1v commented Mar 14, 2023

I haven't tried changing that delay yet

Could you please try? ❤️

@drobotk
Copy link

drobotk commented Mar 15, 2023

Alright, with a delay of 250 it seems to work fine every time. I haven't tried any other values, though, but I might later.

EDIT: After a bunch of restarts I did manage to get it to fail. 300 might be better then.

rom1v added a commit that referenced this issue Mar 16, 2023
On Android 11, a fake popup must be briefly opened to make the system
think that the shell app is in the foreground so that audio may be
recorded.

Making the shell app foreground may take some time depending on the
device, so make 3 attempts, waiting 100ms before each.

Fixes #3796 <#3796>
rom1v added a commit that referenced this issue Mar 16, 2023
On Android 11, a fake popup must be briefly opened to make the system
think that the shell app is in the foreground so that audio may be
recorded.

Making the shell app foreground may take some time depending on the
device, so make 3 attempts, waiting 100ms before each.

Fixes #3796 <#3796>
@rom1v
Copy link
Collaborator

rom1v commented Mar 16, 2023

I implemented 3 attempts with 100ms before each, so that on most devices, it will work within 100ms (so the popup is not visible for too long), but on others it will still work: bc82e34

Here is a full build for win64:

Please tell me if it works for you.

@bropines
Copy link
Author

bropines commented Mar 16, 2023

I implemented 3 attempts with 100ms before each, so that on most devices, it will work within 100ms (so the popup is not visible for too long), but on others it will still work: bc82e34

Here is a full build for win64:

Please tell me if it works for you.

Yep. Working on my phone. Thanks. I think we'll wait for feedback from other users, and close this issue

@yume-chan
Copy link
Contributor

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);
}
}
}

It checked if (Build.VERSION.SDK_INT == Build.VERSION_CODES.R) twice.

@rom1v
Copy link
Collaborator

rom1v commented Mar 17, 2023

Yes, I noticed that yesterday 👍 It will be fixed by aa6846c (not merged yet).

@bropines
Copy link
Author

Yes, I noticed that yesterday 👍 It will be fixed by aa6846c (not merged yet).

I just saw all these branches. What a horror)

@rom1v
Copy link
Collaborator

rom1v commented Mar 17, 2023

🙈

@rom1v
Copy link
Collaborator

rom1v commented Mar 17, 2023

Locally:

$ git branch | wc -l
1062

I should probably do some cleanup. 😄

However, I want to keep many of them somewhere, to be able to (manually) "bisect" across branches for debugging.

For example, if a bug is reported, and I observe that it works on audio.107 (102 commits) but not on audio.145 (119 commits), what changes has been made between the two patchsets?

git range-diff audio.107~102..audio.107 audio.145~119..audio.145

rom1v added a commit that referenced this issue Mar 17, 2023
On Android 11, a fake popup must be briefly opened to make the system
think that the shell app is in the foreground so that audio may be
recorded.

Making the shell app foreground may take some time depending on the
device, so make 3 attempts, waiting 100ms before each.

Fixes #3796 <#3796>
@rom1v
Copy link
Collaborator

rom1v commented Mar 17, 2023

02f4ff7 and 3626d90 merged into dev.

@rom1v rom1v closed this as completed Mar 17, 2023
This was referenced Mar 27, 2023
rom1v added a commit that referenced this issue Jul 7, 2023
Making the shell app foreground (specific for Android 11) may take more
than 300ms on some devices, so increase the number of attempts from 3 to
5 (separated by 100ms).

Fixes #4147 <#4147>
Refs #3796 <#3796>
Refs 02f4ff7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants