Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
111.36k stars 10.67k forks source link

Not work audio streaming on android 11 #3796

Closed bropines closed 1 year ago

bropines commented 1 year ago

Environment

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 commented 1 year ago

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

bropines commented 1 year ago

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 commented 1 year ago

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

It disables the workaround for Android 11:

diff ```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 commented 1 year ago

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 commented 1 year ago

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

bropines commented 1 year ago

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 commented 1 year ago

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?

rom1v commented 1 year ago

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

bropines commented 1 year ago

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

I'm kind of dumb. image image

bropines commented 1 year ago

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 commented 1 year ago

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

rom1v commented 1 year ago

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

bropines commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

I haven't tried changing that delay yet

Could you please try? :heart:

drobotk commented 1 year ago

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 commented 1 year ago

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.

bropines commented 1 year ago

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 commented 1 year ago

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.

rom1v commented 1 year ago

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

bropines commented 1 year ago

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

I just saw all these branches. What a horror)

rom1v commented 1 year ago

:see_no_evil:

rom1v commented 1 year ago

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 commented 1 year ago

02f4ff7534649153d6f87b05a0757431a2d0ee5f and 3626d90004c9946320152564a375e56f9c5030f4 merged into dev.