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

SCRCPY Crashing Sometimes When the Screen is Rotated #3693

Closed
eeriemyxi opened this issue Jan 26, 2023 · 12 comments
Closed

SCRCPY Crashing Sometimes When the Screen is Rotated #3693

eeriemyxi opened this issue Jan 26, 2023 · 12 comments

Comments

@eeriemyxi
Copy link

eeriemyxi commented Jan 26, 2023

  • [☑️] I have read the FAQ.
  • [☑️] I have searched in existing issues.

Environment

  • OS: Microsoft Windows [Version 10.0.19045.2546]
  • scrcpy version: 1.25
  • installation method: Windows release.
  • device model: RMX1971.
  • Android version: 11

Describing the Bug
Sometimes when I try to rotate the screen using MOD + R, SCRCPY crashes with an exception but when I restart the server the screen rotation is as I'd expect after triggering the hotkey.

The following terminal output is the result of me rotating the screen several times on the same app and on the same app activity screen. I did find an open issue (#1700) also addressing server crashes sometimes when the screen is rotated, but that error is slightly different, the observer was emulating some device virtually (which may have contained unfinished encoders and decoders to enhance the performance), and collaborator's reply was that the device does not support quick encoding or something which causes the errors but in my case it happens even when I wouldn't rotate screen for several minutes before the error-causing attempt to rotate the screen.

If this isn't something that could be prevented by configurations or modifications of the code, is there some way to auto reconnect the device on error?

scrcpy -b 2500k -m 1080 -S -w --power-off-on-close
scrcpy 1.25 <https://github.com/Genymobile/scrcpy>
C:\Users\Myxi\Documents\scrcpy\scrcpy-server: 1 fi...ushed, 0 skipped. 0.6 MB/s (42151 bytes in 0.065s)
[server] INFO: Device: realme RMX1971 (Android 11)
INFO: Renderer: direct3d
INFO: Initial texture: 496x1080
[server] INFO: Device screen turned off
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
[server] ERROR: Encoding error: android.media.MediaCodec$CodecException: Error 0x80001009
WARN: Device disconnected
[server] ERROR: Exception on thread Thread[main,5,main]
android.media.MediaCodec$CodecException: Error 0x80001009
        at android.media.MediaCodec.native_stop(Native Method)
        at android.media.MediaCodec.stop(MediaCodec.java:2250)
        at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:104)
        at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:74)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:100)
        at com.genymobile.scrcpy.Server.main(Server.java:334)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:419)
@rom1v
Copy link
Collaborator

rom1v commented Jan 26, 2023

Thank you for your report.

This is a codec error. When this happens, scrcpy should try to recover (it is not implemented yet, I added it to my TODOlist):

Internal codec errors result in a CodecException, which may be due to media content corruption, hardware failure, resource exhaustion, and so forth, even when the application is correctly using the API. The recommended action when receiving a CodecException can be determined by calling CodecException#isRecoverable and CodecException#isTransient

@rom1v
Copy link
Collaborator

rom1v commented Jan 27, 2023

I refactored some code and attempted to recover on non-fatal errors: branch refactor-encoder.

Here are binaries (to replace in your 1.25 release):

  • scrcpy.exe SHA-256: 9559d04e0db24a8d8537cb9fc5710a719a9bec152cedccdde4e2e2481e4327ac
  • scrcpy-server SHA-256: 22a2ccf24f048f4d313b09ce9db2ff26c07c5abfba0ad8afcdfc589370f494d7

@eeriemyxi
Copy link
Author

eeriemyxi commented Jan 28, 2023

I refactored some code and attempted to recover on non-fatal errors: branch refactor-encoder.

Here are binaries (to replace in your 1.25 release):

  • scrcpy.exe SHA-256: 9559d04e0db24a8d8537cb9fc5710a719a9bec152cedccdde4e2e2481e4327ac
  • scrcpy-server SHA-256: 22a2ccf24f048f4d313b09ce9db2ff26c07c5abfba0ad8afcdfc589370f494d7

Thank you. I have not gotten the error on general use yet so I feel it is working.

@eeriemyxi eeriemyxi changed the title SCRCPY Crashing Sometimes When The Screen is Rotated SCRCPY Crashing Sometimes When the Screen is Rotated Jan 28, 2023
@rom1v
Copy link
Collaborator

rom1v commented Jan 28, 2023

When there is a recovered error, there should be a line Codec error: ... in the console. Do you see it?

@eeriemyxi
Copy link
Author

eeriemyxi commented Jan 28, 2023

When there is a recovered error, there should be a line Codec error: ... in the console. Do you see it?

Back then I didn't see such output, but now I do get the error and it terminates the server subsequently.

scrcpy -b 2500k -m 1080 -S -w --power-off-on-close
scrcpy 1.25 <https://github.com/Genymobile/scrcpy>
C:\Users\Myxi\Documents\scrcpy\scrcpy-server: 1 file pushed, 0 skipped. 16.7 MB/s (43943 bytes in 0.003s)
[server] INFO: Device: realme RMX1971 (Android 11)
INFO: Renderer: direct3d
INFO: Initial texture: 496x1080
[server] INFO: Device screen turned off
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
[server] INFO: Device rotation requested: landscape
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
[server] ERROR: Codec error: Error 0x80001009
[server] INFO: #1 true true
WARN: Device disconnected
[server] ERROR: Exception on thread Thread[main,5,main]
android.media.MediaCodec$CodecException: Error 0x80001009
        at android.media.MediaCodec.native_stop(Native Method)
        at android.media.MediaCodec.stop(MediaCodec.java:2250)
        at com.genymobile.scrcpy.ScreenEncoder.internalStreamScreen(ScreenEncoder.java:109)
        at com.genymobile.scrcpy.ScreenEncoder.streamScreen(ScreenEncoder.java:75)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:96)
        at com.genymobile.scrcpy.Server.main(Server.java:323)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:419)

Sometimes it would crash after I'd try to close an app that was in landscape mode (my launcher does not support landscape mode) and sometimes it would crash when I try to rotate an app; however if I had requested landscape mode or portrait mode and then it crashed, if I then restart the server, it retains the requested mode.

To update the binaries, I deleted the previous ones, moved the new ones and renamed scrcpy-server.bin to scrcpy-server.

@eeriemyxi
Copy link
Author

It seems that it almost only happens when it tries to switch from landscape to portrait. I have tried and received this several times now, and I think each time it was this case.

@rom1v
Copy link
Collaborator

rom1v commented Jan 28, 2023

[server] ERROR: Codec error: Error 0x80001009
…
WARN: Device disconnected
[server] ERROR: Exception on thread Thread[main,5,main]
android.media.MediaCodec$CodecException: Error 0x80001009

OK, so the error is neither "recoverable" nor "transient" 😕

In that case, I will need an additional retry mechanism even if the error is not recoverable. I'll let you know.

rom1v added a commit that referenced this issue Jan 29, 2023
MediaCodec may fail spuriously, typically when stopping an encoding and
starting a new one immediately (for example on device rotation).

In that case, retry (3 times), in many cases it should work.

Refs #3693 <#3693>
@rom1v
Copy link
Collaborator

rom1v commented Jan 29, 2023

Here is a new version: branch refactor-encoder.3.

Here are the binaries to replace in your 1.25 release:

  • scrcpy.exe SHA-256: 23d22f9fc72c9ba0a814eb293135c4805396bf8a68d2e5b05f79c57c3435e557
  • scrcpy-server SHA-256: d51156f615ea85bf7207f3666afb4025244909adb1e68d5c2135a4fa9c258e1a

@eeriemyxi
Copy link
Author

eeriemyxi commented Jan 30, 2023

Here is a new version: branch refactor-encoder.3.

Here are the binaries to replace in your 1.25 release:

* [`scrcpy.exe`](https://tmp.rom1v.com/scrcpy/3693/2/scrcpy.exe) `SHA-256: 23d22f9fc72c9ba0a814eb293135c4805396bf8a68d2e5b05f79c57c3435e557`

* [`scrcpy-server`](https://tmp.rom1v.com/scrcpy/3693/2/scrcpy-server) `SHA-256: d51156f615ea85bf7207f3666afb4025244909adb1e68d5c2135a4fa9c258e1a`

Cloning the branch then compiling it on Kubuntu is giving out these errors, but if I compile the master branch it works as expected.

I did git clone -b refactor-encoder.3 <repo_url> and then ran the install_release.sh script.

eeriemyxi@eeriemyxi:~/Documents/compiling/scrcpy$ adb devices
List of devices attached
319d2661        device

eeriemyxi@eeriemyxi:~/Documents/compiling/scrcpy$ scrcpy 
scrcpy 1.25 <https://github.com/Genymobile/scrcpy>
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 3.0 MB/s (42151 bytes in 0.013s)
[server] WARN: Unknown server option: uid
[server] INFO: Device: realme RMX1971 (Android 11)
[server] ERROR: Exception on thread Thread[main,5,main]
java.io.IOException: Connection refused
        at android.net.LocalSocketImpl.connectLocal(Native Method)
        at android.net.LocalSocketImpl.connect(LocalSocketImpl.java:262)
        at android.net.LocalSocket.connect(LocalSocket.java:148)
        at com.genymobile.scrcpy.DesktopConnection.connect(DesktopConnection.java:45)
        at com.genymobile.scrcpy.DesktopConnection.open(DesktopConnection.java:72)
        at com.genymobile.scrcpy.Server.scrcpy(Server.java:73)
        at com.genymobile.scrcpy.Server.main(Server.java:334)
        at com.android.internal.os.RuntimeInit.nativeFinishInit(Native Method)
        at com.android.internal.os.RuntimeInit.main(RuntimeInit.java:419)
ERROR: Server connection failed

This is the output with the current master branch (git clone -b master ...).

[...]
Found ninja-1.10.1 at /usr/bin/ninja
[54/54] Linking target app/scrcpy
[scrcpy] Installing (sudo)...
[0/1] Installing files.
Installing app/scrcpy to /usr/local/bin
Stripping target 'app/scrcpy' using strip.
Installing server/scrcpy-server to /usr/local/share/scrcpy
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/scrcpy.1 to /usr/local/share/man/man1
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/data/icon.png to /usr/local/share/icons/hicolor/256x256/apps
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/data/zsh-completion/_scrcpy to /usr/local/share/zsh/site-functions
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/data/bash-completion/scrcpy to /usr/local/share/bash-completion/completions
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/data/scrcpy.desktop to /usr/local/share/applications
Installing /home/eeriemyxi/Documents/compiling/scrcpy/app/data/scrcpy-console.desktop to /usr/local/share/applications
eeriemyxi@eeriemyxi:~/Documents/compiling/scrcpy$ scrcpy
scrcpy 1.25 <https://github.com/Genymobile/scrcpy>
/usr/local/share/scrcpy/scrcpy-server: 1 file pushed. 2.7 MB/s (42151 bytes in 0.015s)
[server] INFO: Device: realme RMX1971 (Android 11)
INFO: Renderer: opengl
INFO: OpenGL version: 4.5 (Compatibility Profile) Mesa 22.0.5
INFO: Trilinear filtering enabled
INFO: Initial texture: 1080x2336
^Ceeriemyxi@eeriemyxi:~/Documents/compiling/scrcpy$ 

is this branch only meant to be used on Windows? I am sorry but I moved to Linux yesterday.

@rom1v
Copy link
Collaborator

rom1v commented Jan 30, 2023

Cloning the branch then compiling it on Kubuntu is giving out these errors, but if I compile the master branch it works as expected.

I did git clone -b refactor-encoder.3 <repo_url> and then ran the install_release.sh script.

Because install_release.sh downloads the prebuilt server from the last release (v1.25), and compiles the client for your current branch. As a result, the client and server versions don't match (and the protocol between the client and the server has changed a bit since the latest release).

Either you need to compile the server too, or take the prebuilt scrcpy-server from my previous comment.

You can specify a prebuilt server by following these steps.

@eeriemyxi
Copy link
Author

eeriemyxi commented Jan 30, 2023

Cloning the branch then compiling it on Kubuntu is giving out these errors, but if I compile the master branch it works as expected.
I did git clone -b refactor-encoder.3 <repo_url> and then ran the install_release.sh script.

Because install_release.sh downloads the prebuilt server from the last release (v1.25), and compiles the client for your current branch. As a result, the client and server versions don't match (and the protocol between the client and the server has changed a bit since the latest release).

Either you need to compile the server too, or take the prebuilt scrcpy-server from my previous comment.

You can specify a prebuilt server by following these steps.

[...]
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
INFO: New texture: 496x1080
[server] INFO: Device rotation requested: landscape
INFO: New texture: 1080x496
[server] INFO: Device rotation requested: portrait
[server] ERROR: Encoding error: android.media.MediaCodec$CodecException: Error 0x80001009
[server] INFO: Retrying...
INFO: New texture: 496x1080

It works fine now. Thank you so much.

rom1v added a commit that referenced this issue Jan 30, 2023
MediaCodec may fail spuriously, typically when stopping an encoding and
starting a new one immediately (for example on device rotation).

In that case, retry a few times, in many cases it should work.

Refs #3693 <#3693>
@rom1v
Copy link
Collaborator

rom1v commented Jan 30, 2023

Thank you for your tests.

Merged into dev.

@rom1v rom1v closed this as completed Jan 30, 2023
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

2 participants