Genymobile / scrcpy

Display and control your Android device
Apache License 2.0
106.5k stars 10.31k forks source link

CompositionEngine: ANativeWindow::dequeueBuffer failed for display [scrcpy] with error: -32 #2963

Closed cameralis closed 2 years ago

cameralis commented 2 years ago

Environment

Describe the bug scrcpy crashes after a few frames. I've tried different resolutions and encoders, but none of them helped. This problem might be specific to my phone but it's worth a try. Also i couldn't find this exact CompositionEngine error in the issues. Also there is a weird thing. This is specific to the OMX.google.h264.encoder and c2.android.avc.encoder encoders. They don't crash immediately they just start spamming the above mentioned CompositeEngine lines in the logcat.

Terminal output:

$ scrcpy
scrcpy 1.21 <https://github.com/Genymobile/scrcpy>
/usr/share/scrcpy/scrcpy-server: 1 file pushed, 0 skipped. 488.1 MB/s (40067 bytes in 0.000s)
[server] INFO: Device: Xiaomi M2101K6G (Android 12)
INFO: Renderer: opengl
INFO: OpenGL version: 4.6 (Compatibility Profile) Mesa 21.3.4
INFO: Trilinear filtering enabled
INFO: Initial texture: 1080x2400
WARN: Device disconnected
[server] ERROR: Exception on thread Thread[main,5,main]
java.lang.IllegalStateException
    at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
    at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3535)
    at com.genymobile.scrcpy.ScreenEncoder.encode(ScreenEncoder.java:110)
    at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:91)
    at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:65)
    at com.genymobile.scrcpy.Server.scrcpy(Server.java:93)
    at com.genymobile.scrcpy.Server.main(Server.java:309)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:357)

Logcat:

01-23 20:36:17.443 18781 18781 D AndroidRuntime: Calling main entry com.genymobile.scrcpy.Server
01-23 20:36:17.445 18781 18781 I scrcpy  : Device: Xiaomi M2101K6G (Android 12)
01-23 20:36:17.670 18796 18796 D AndroidRuntime: Calling main entry com.genymobile.scrcpy.CleanUp
01-23 20:36:18.090   640   640 E CompositionEngine: Error when queueing buffer for display [scrcpy]: -32
01-23 20:36:18.103   640   640 E CompositionEngine: ANativeWindow::dequeueBuffer failed for display [scrcpy] with error: -32
01-23 20:36:18.103   640   640 W CompositionEngine: Dequeuing buffer for display [scrcpy] failed, bailing out of client composition for this frame
01-23 20:36:18.117 18781 18781 E AndroidRuntime:    at com.genymobile.scrcpy.ScreenEncoder.encode(ScreenEncoder.java:110)
01-23 20:36:18.117 18781 18781 E AndroidRuntime:    at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:91)
01-23 20:36:18.117 18781 18781 E AndroidRuntime:    at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:65)
01-23 20:36:18.117 18781 18781 E AndroidRuntime:    at com.genymobile.scrcpy.Server.scrcpy(Server.java:93)
01-23 20:36:18.117 18781 18781 E AndroidRuntime:    at com.genymobile.scrcpy.Server.main(Server.java:309)
01-23 20:36:18.117 18781 18781 E scrcpy  : Exception on thread Thread[main,5,main]
01-23 20:36:18.117 18781 18781 E scrcpy  : java.lang.IllegalStateException
01-23 20:36:18.117 18781 18781 E scrcpy  :  at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3535)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.genymobile.scrcpy.ScreenEncoder.encode(ScreenEncoder.java:110)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:91)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:65)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.genymobile.scrcpy.Server.scrcpy(Server.java:93)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.genymobile.scrcpy.Server.main(Server.java:309)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
01-23 20:36:18.117 18781 18781 E scrcpy  :  at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:357)
01-23 20:36:19.017 18796 18796 I scrcpy  : Cleaning up
01-23 20:36:19.024 18796 18796 I scrcpy  : Restoring normal power mode
rom1v commented 2 years ago

Please test #2947.

cameralis commented 2 years ago

I builded the dev branch but it doesn't seem to work. (Same issue still)

rom1v commented 2 years ago

I builded the dev branch but it doesn't seem to work. (Same issue still)

How do you run what you built from the dev branch? Did you also build the server?

cameralis commented 2 years ago

Found some additional logs that might be related:

01-23 21:00:24.937 22042 22042 D MediaCodecsXmlParser: parsing /vendor/etc/media_codecs.xml...
01-23 21:00:24.937 22042 22042 D MediaCodecsXmlParser: parsing /vendor/etc/media_codecs_google_audio.xml...
01-23 21:00:24.937 22042 22042 D MediaCodecsXmlParser: parsing /vendor/etc/media_codecs_google_telephony.xml...
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: parsing /vendor/etc/media_codecs_google_video.xml...
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: parsing /vendor/etc/media_codecs_performance.xml...
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.qti.avc.encoder at line 53 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.avc.encoder at line 77 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.hevc.encoder at line 83 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.h263.encoder at line 86 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.mpeg4.encoder at line 89 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.vp8.encoder at line 92 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.qti.avc.encoder at line 98 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.938 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.qti.avc.decoder at line 141 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.avc.decoder at line 182 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.hevc.decoder at line 188 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.h263.decoder at line 195 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.mpeg4.decoder at line 199 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.vp8.decoder at line 202 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 D MediaCodecsXmlParser: MediaCodec: cannot update non-existing codec: c2.android.vp9.decoder at line 207 of /vendor/etc/media_codecs_performance.xml
01-23 21:00:24.939 22042 22042 I MediaCodecsXmlParser: Did not find media_codecs_shaping.xml in search path
01-23 21:00:24.939 22042 22042 E MediaCodecsXmlParser: Cannot find the role for a decoder of type video/divx4
01-23 21:00:24.939 22042 22042 E MediaCodecsXmlParser: Cannot find the role for a decoder of type video/divx
01-23 21:00:24.939 22042 22042 E MediaCodecsXmlParser: Cannot find the role for a decoder of type video/mp4v-esdp
cameralis commented 2 years ago

How do you run what you built from the dev branch? Did you also build the server?

$ git clone https://github.com/Genymobile/scrcpy
$ git checkout dev
$ ./install_release.sh
rom1v commented 2 years ago

install_release is just for building the official release (it downloads the prebuilt server).

Anyway, with the official release, just execute scrcpy -m1024: https://github.com/Genymobile/scrcpy/blob/master/FAQ.md#exception

cameralis commented 2 years ago

Okay I'm sorry, I just realized this doesn't build the server. I'll try again with building the server.

cameralis commented 2 years ago

Anyway, with the official release, just execute scrcpy -m1024:

In the describe this bug section I wrote:

I've tried different resolutions and encoders, but none of them helped.

So yeah, it didn't help unfortunately.

cameralis commented 2 years ago

Here's the output with scrcpy-server built from the dev branch:

$ build/app/scrcpy -m1024
scrcpy 1.21 <https://github.com/Genymobile/scrcpy>
DEBUG: Using server: /usr/local/share/scrcpy/scrcpy-server
/usr/local/share/scrcpy/scrcpy-server: 1 file pus...ed, 0 skipped. 509.8 MB/s (60376 bytes in 0.000s
DEBUG: Screensaver enabled
[server] INFO: Device: Xiaomi M2101K6G (Android 12)
DEBUG: Server connected
DEBUG: Starting controller thread
DEBUG: Starting receiver thread
INFO: Renderer: opengl
INFO: OpenGL version: 4.6 (Compatibility Profile) Mesa 21.3.4
INFO: Trilinear filtering enabled
DEBUG: Using icon: /usr/local/share/icons/hicolor/256x256/apps/scrcpy.png
INFO: Initial texture: 464x1024
DEBUG: Starting stream thread
[server] DEBUG: Using encoder: 'OMX.qcom.video.encoder.avc'
[server] ERROR: Encoding error: java.lang.IllegalStateException: null
DEBUG: End of frames
WARN: Device disconnected
DEBUG: quit...
DEBUG: Receiver stopped
[server] DEBUG: Device message sender stopped
[server] DEBUG: Controller stopped
[server] ERROR: Exception on thread Thread[main,5,main]
java.lang.IllegalStateException
    at android.media.MediaCodec.native_dequeueOutputBuffer(Native Method)
    at android.media.MediaCodec.dequeueOutputBuffer(MediaCodec.java:3535)
    at com.genymobile.scrcpy.ScreenEncoder.encode(ScreenEncoder.java:147)
    at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:99)
    at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:73)
    at com.genymobile.scrcpy.Server.scrcpy(Server.java:93)
    at com.genymobile.scrcpy.Server.main(Server.java:303)
    at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
    at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:357)
DEBUG: Server disconnected
DEBUG: Server terminated
rom1v commented 2 years ago

INFO: Initial texture: 464x1024
DEBUG: Starting stream thread
[server] DEBUG: Using encoder: 'OMX.qcom.video.encoder.avc'
[server] ERROR: Encoding error: java.lang.IllegalStateException: null

Hmm, weird, it should retry with -m800 (that might not solve your problem, but this is still unexpected):

https://github.com/Genymobile/scrcpy/blob/17c97820b2030759e8b7e431e7d353c30bcff3dc/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java#L130-L140

Could you try with this additional diff, please?

diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
index e4e87c72..4c23dd92 100644
--- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
+++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java
@@ -159,7 +159,10 @@ public class ScreenEncoder implements Device.RotationListener {
                     }

                     IO.writeFully(fd, codecBuffer);
-                    firstFrameSent = true;
+                    if ((bufferInfo.flags & MediaCodec.BUFFER_FLAG_CODEC_CONFIG) == 0) {
+                        // If this is not a config packet, then it contains a frame
+                        firstFrameSent = true;
+                    }
                 }
             } finally {
                 if (outputBufferId >= 0) {
rom1v commented 2 years ago
$ build/app/scrcpy -m1024
scrcpy 1.21 <https://github.com/Genymobile/scrcpy>
DEBUG: Using server: /usr/local/share/scrcpy/scrcpy-server

Also, you should run ./run build -m1024 instead, to use the correct scrcpy-server :wink:

cameralis commented 2 years ago

Before running it I copied build/server/scrcpy-server to /usr/local/share/scrcpy/ so that should be fine but the run script is more convenient.

cameralis commented 2 years ago

The patch didn't help unfortunately, everything still looks the same. :/ (Although now I can see it retrying in the logs)

rom1v commented 2 years ago

(Although now I can see it retrying in the logs)

Thank you, that fixes the new retry feature in the next release (introduced by #2947): a9429efa34b3cbdc57847cb37d3621b889522c7f :tada:

everything still looks the same

This problem might be specific to my phone but it's worth a try.

Probably a device problem indeed.

Try with a tiny definition just to confim (scrcpy -m 200).

Also try to record with screenrecord:

adb shell screenrecord /sdcard/file.mp4
adb pull /sdcard/file.mp4

Does it work?

This is specific to the OMX.google.h264.encoder and c2.android.avc.encoder encoders.

Are there other encoders on the device? How do they behave?

cameralis commented 2 years ago

Some more logs that might be related (this appears before scrcpy crashes):

01-23 21:48:36.125 25213 25355 E Adreno-C2D: <c2d_surface_createYUV:1468>: Error Surface creation failed, error=1, one or more attributes are not aligned for this surface type: surf_bits=11, format=8388760, stride0=1152, stride1=1, stride2=1152, height0=2400, height1=1200, height2=0, phys0=0x00e00000, phys1=0x010a8000, phys2=0x01152000
01-23 21:48:36.125 25213 25355 E C2DColorConvert: convertC2D: Update dst surface def failed (1)
01-23 21:48:36.127 25213 25355 E OMX-VENC: Color Conversion failed
01-23 21:48:36.130 25213 25355 E OMX-VENC: ERROR: ETBProxy() failed!
01-23 21:48:36.130 25213 25355 E OMX-VENC: ERROR: send OMX_ErrorHardware to Client
01-23 21:48:36.131 25332 25353 E ACodec  : [OMX.qcom.video.encoder.avc] ERROR(0x80001009)
01-23 21:48:36.131 25332 25353 E ACodec  : signalError(omxError 0x80001009, internalError -2147483648)
01-23 21:48:36.131 25332 25352 E MediaCodec: Codec reported err 0x80001009, actionCode 0, while in state 6/STARTED
01-23 21:48:36.134 25332 25332 E scrcpy  : Encoding error: java.lang.IllegalStateException: null
01-23 21:48:36.138 25332 25351 D scrcpy  : Device message sender stopped
01-23 21:48:36.144 25332 25350 D scrcpy  : Controller stopped
01-23 21:48:36.145 25332 25332 D AndroidRuntime: Shutting down VM
01-23 21:48:36.146 25332 25353 E ACodec  : signalError(omxError 0x80001001, internalError -2147483646)

I'll test the things you've suggested in a minute.

cameralis commented 2 years ago
  1. -m200 (88x200) also crashes
  2. screenrecord does work, but the phone becomes slow and the recording is laggy.
  3. Of the 4 encoders scrcpy --encoder _ lists:
    • OMX.qcom.video.encoder.avc crashes
    • c2.android.avc.encoder draws a few frames then hangs
    • OMX.google.h264.encoder draws a few frames then hangs
    • c2.qti.avc.encoder draws a few frames then hangs
cameralis commented 2 years ago

closing because of inactivity and possibly a specific hardware problem

cameralis commented 2 years ago

at least I'm not the only one... #3051

cameralis commented 2 years ago

Updating to Android 12L solved this issue. Everything is working fine as it should.

AndroidDeveloperLB commented 1 month ago

@rom1v I've noticed recently that on v2.4, even though it's not crashing as written here, I get a similar error log, all the time, about 20 times every second, as such:

CompositionEngine surfaceflinger                               ANativeWindow::dequeueBuffer failed for display [scrcpy] with error: -32

Is it a known issue? Related to here? Why does it occur? It's spamming logcat...

This was tested on Pixel 6 with Android 14.