scrcpy icon indicating copy to clipboard operation
scrcpy copied to clipboard

Not work audio streaming on android 11

Open bropines opened this issue 2 years ago • 23 comments
trafficstars

  • [x] I have read the FAQ.
  • [x] 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.

bropines avatar Mar 12 '23 12:03 bropines

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

rom1v avatar Mar 12 '23 12:03 rom1v

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.

bropines avatar Mar 12 '23 12:03 bropines

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?

rom1v avatar Mar 12 '23 14:03 rom1v

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

bropines avatar Mar 12 '23 14:03 bropines

OK, so the culprit is probably the delay here:

https://github.com/Genymobile/scrcpy/blob/cbca79b95baa2b2cf39b72d0a0b2b3de0725959a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java#L76

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 :)

rom1v avatar Mar 12 '23 14:03 rom1v

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 avatar Mar 12 '23 14:03 bropines

OK, so the culprit is probably the delay here:

https://github.com/Genymobile/scrcpy/blob/cbca79b95baa2b2cf39b72d0a0b2b3de0725959a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java#L76

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?

bropines avatar Mar 12 '23 14:03 bropines

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

rom1v avatar Mar 12 '23 14:03 rom1v

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

I'm kind of dumb. image image

bropines avatar Mar 12 '23 15:03 bropines

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 avatar Mar 12 '23 17:03 bropines

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

bropines avatar Mar 12 '23 21:03 bropines

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

rom1v avatar Mar 12 '23 22:03 rom1v

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.

bropines avatar Mar 12 '23 22:03 bropines

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.

drobotk avatar Mar 13 '23 18:03 drobotk

I haven't tried changing that delay yet

Could you please try? :heart:

rom1v avatar Mar 14 '23 18:03 rom1v

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.

drobotk avatar Mar 15 '23 20:03 drobotk

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: bc82e340d751da8fd1903f93ee00b455a7e6a904

Here is a full build for win64:

Please tell me if it works for you.

rom1v avatar Mar 16 '23 21:03 rom1v

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

bropines avatar Mar 16 '23 23:03 bropines

https://github.com/Genymobile/scrcpy/blob/2ef14307eb84601036f200e0acc57952a010c1e2/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java#L61-L79

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

yume-chan avatar Mar 17 '23 05:03 yume-chan

Yes, I noticed that yesterday :+1: It will be fixed by https://github.com/Genymobile/scrcpy/commit/aa6846c989377cf6c77c57291f71b5cdda2e3026 (not merged yet).

rom1v avatar Mar 17 '23 06:03 rom1v

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

I just saw all these branches. What a horror)

bropines avatar Mar 17 '23 10:03 bropines

:see_no_evil:

rom1v avatar Mar 17 '23 10:03 rom1v

Locally:

$ git branch | wc -l
1062

I should probably do some cleanup. :smile:

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 avatar Mar 17 '23 11:03 rom1v

02f4ff7534649153d6f87b05a0757431a2d0ee5f and 3626d90004c9946320152564a375e56f9c5030f4 merged into dev.

rom1v avatar Mar 17 '23 20:03 rom1v