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

RuntimeException when stopStream #1460

Open
LiPengfei0106 opened this issue Apr 19, 2024 · 7 comments
Open

RuntimeException when stopStream #1460

LiPengfei0106 opened this issue Apr 19, 2024 · 7 comments

Comments

@LiPengfei0106
Copy link

Hello:

The demo run on a rk3399 chip device (Android 7.1.2) throw RuntimeException when stopStream sometimes.

here is the error logs

13:20:11.199 19203-19401 MessageQueue                         W  Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {f4772c3} sending message to a Handler on a dead thread
                                                                 java.lang.IllegalStateException: Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {f4772c3} sending message to a Handler on a dead thread
                                                                 	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                                                                 	at android.os.Handler.enqueueMessage(Handler.java:647)
                                                                 	at android.os.Handler.sendMessageAtTime(Handler.java:616)
                                                                 	at android.os.Handler.sendMessageDelayed(Handler.java:586)
                                                                 	at android.os.Handler.sendEmptyMessageDelayed(Handler.java:550)
                                                                 	at android.os.Handler.sendEmptyMessage(Handler.java:535)
                                                                 	at android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallback.onStopped(DisplayManagerGlobal.java:497)
                                                                 	at android.hardware.display.IVirtualDisplayCallback$Stub.onTransact(IVirtualDisplayCallback.java:61)
                                                                 	at android.os.Binder.execTransact(Binder.java:565)
13:20:11.211 19203-19203 MicrophoneManager                    I  Microphone stopped
13:20:11.212 19203-19203 VideoEncoder                         I  stopped
13:20:11.247 19203-19391 mali_so                              E  encounter the first mali_error : 0x0002 : failed to allocate CPU memory (gles_fb_first_drawcall_cb at hardware/rockchip/mali_so/driver/product/gles/src/fb/mali_gles_fb_module_api.c:961)
--------- beginning of crash
13:20:11.249 19203-19391 AndroidRuntime                       E  FATAL EXCEPTION: pool-2-thread-1
                                                                 Process: com.pedro.streamer, PID: 19203
                                                                 java.lang.RuntimeException: drawScreen end. GL error: 1285
                                                                 	at com.pedro.encoder.utils.gl.GlUtil.checkGlError(GlUtil.java:144)
                                                                 	at com.pedro.encoder.input.gl.render.ScreenRender.draw(ScreenRender.java:156)
                                                                 	at com.pedro.encoder.input.gl.render.ScreenRender.drawEncoder(ScreenRender.java:108)
                                                                 	at com.pedro.encoder.input.gl.render.MainRender.drawScreenEncoder(MainRender.kt:71)
                                                                 	at com.pedro.library.view.GlStreamInterface.draw(GlStreamInterface.kt:202)
                                                                 	at com.pedro.library.view.GlStreamInterface.access$draw(GlStreamInterface.kt:47)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1.invoke$lambda$0(GlStreamInterface.kt:152)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1.$r8$lambda$2tY9gRad84Iwfv49R2gYTsLrLZs(GlStreamInterface.kt)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
                                                                 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                 	at java.lang.Thread.run(Thread.java:761)
13:20:11.254 19203-19390 CommandsManager                      I  send Command(name='closeStream', transactionId=6, timeStamp=3, streamId=1, data=[AmfString value: closeStream, AmfNumber value: 6.0, AmfNull], bodySize=24)
13:20:11.263 19203-19203 AudioEncoder                         I  stopped
13:20:11.278 19203-19407 SurfaceManager                       I  GL released
13:20:11.281 19203-19407 SurfaceManager                       I  GL released
13:20:11.282 19203-19407 SurfaceManager                       E  GL already released
13:20:11.283 19203-19407 SurfaceManager                       I  GL released
@pedroSG94
Copy link
Owner

Hello,
Can you tell me the library version used?

@LiPengfei0106
Copy link
Author

I pulled the latest code from the master branch.

I tried to remove the throw RuntimeException in GLUtil, This GL error does not appear fatal

public static void checkGlError(String op) {
    int error = GLES20.glGetError();
    if (error != GLES20.GL_NO_ERROR) {
      throw new RuntimeException(op + ". GL error: " + error);
    }
  }

@pedroSG94
Copy link
Owner

pedroSG94 commented Apr 22, 2024

Hello,

Ideally it should be solved instead of ignored.
Can you test this?:

  • Try to use the library always with the same thread. For example always with the main thread.
  • If you have a way to reproduce it you can debug a bit the case. Add this line on start of draw method in GlStreamInterface to debug the thread used in each call. The thread must be always the same so if you get a different thread in a call when the app crash, it is the reason:
Log.e("Pedro", "id: ${Thread.currentThread().id}, name: ${Thread.currentThread().name}")
  • As last test. You can change that code to this (I want avoid it so test the others ways first):
  public static void checkGlError(String op) {
    int error = GLES20.glGetError();
    if (error != GLES20.GL_NO_ERROR && error != GLES20.GL_OUT_OF_MEMORY) {
      throw new RuntimeException(op + ". GL error: " + error);
    }
  }

This way you only exclude this error instead of all gl errors

Please, test it all and let me know the result. I will try to add a proper fix depend of your tests.

@LiPengfei0106
Copy link
Author

Hello,

  • I'm testing with the app from the project, it's using the library on the main thread.
  • I add the log, the thread is same.
    There is the log:
11:23:35.668 26459-26562 Pedro                                E  id: 485, name: pool-2-thread-1
11:23:35.708 26459-26563 RtmpSender                           I  wrote Video packet, size 702
11:23:35.735 26459-26562 Pedro                                E  id: 485, name: pool-2-thread-1
11:23:35.755 26459-26563 RtmpSender                           I  wrote Video packet, size 216
11:23:35.792 26459-26459 MicrophoneManager                    I  Microphone stopped
11:23:35.792 26459-26459 VideoEncoder                         I  stopped
11:23:35.797 26459-26472 MessageQueue                         W  Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {8392389} sending message to a Handler on a dead thread
                                                                 java.lang.IllegalStateException: Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {8392389} sending message to a Handler on a dead thread
                                                                 	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                                                                 	at android.os.Handler.enqueueMessage(Handler.java:647)
                                                                 	at android.os.Handler.sendMessageAtTime(Handler.java:616)
                                                                 	at android.os.Handler.sendMessageDelayed(Handler.java:586)
                                                                 	at android.os.Handler.sendEmptyMessageDelayed(Handler.java:550)
                                                                 	at android.os.Handler.sendEmptyMessage(Handler.java:535)
                                                                 	at android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallback.onStopped(DisplayManagerGlobal.java:497)
                                                                 	at android.hardware.display.IVirtualDisplayCallback$Stub.onTransact(IVirtualDisplayCallback.java:61)
                                                                 	at android.os.Binder.execTransact(Binder.java:565)
11:23:35.805 26459-26562 Pedro                                E  id: 485, name: pool-2-thread-1
11:23:35.810 26459-26562 mali_so                              E  encounter the first mali_error : 0x0002 : failed to allocate CPU memory (gles_fb_first_drawcall_cb at hardware/rockchip/mali_so/driver/product/gles/src/fb/mali_gles_fb_module_api.c:961)
--------- beginning of crash
11:23:35.813 26459-26562 AndroidRuntime                       E  FATAL EXCEPTION: pool-2-thread-1
                                                                 Process: com.pedro.streamer, PID: 26459
                                                                 java.lang.RuntimeException: drawScreen end. GL error: 1285
                                                                 	at com.pedro.encoder.utils.gl.GlUtil.checkGlError(GlUtil.java:145)
                                                                 	at com.pedro.encoder.input.gl.render.ScreenRender.draw(ScreenRender.java:156)
                                                                 	at com.pedro.encoder.input.gl.render.ScreenRender.drawEncoder(ScreenRender.java:108)
                                                                 	at com.pedro.encoder.input.gl.render.MainRender.drawScreenEncoder(MainRender.kt:70)
                                                                 	at com.pedro.library.view.GlStreamInterface.draw(GlStreamInterface.kt:203)
                                                                 	at com.pedro.library.view.GlStreamInterface.access$draw(GlStreamInterface.kt:47)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1.invoke$lambda$0(GlStreamInterface.kt:152)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1.$r8$lambda$2tY9gRad84Iwfv49R2gYTsLrLZs(GlStreamInterface.kt)
                                                                 	at com.pedro.library.view.GlStreamInterface$start$1$1$$ExternalSyntheticLambda0.run(D8$$SyntheticClass:0)
                                                                 	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
                                                                 	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
                                                                 	at java.lang.Thread.run(Thread.java:761)
11:23:35.816 26459-26574 CommandsManager                      I  send Command(name='closeStream', transactionId=6, timeStamp=1, streamId=1, data=[AmfString value: closeStream, AmfNumber value: 6.0, AmfNull], bodySize=24)
11:23:35.836 26459-26580 SurfaceManager                       I  GL released
11:23:35.840 26459-26580 SurfaceManager                       I  GL released
11:23:35.840 26459-26580 SurfaceManager                       E  GL already released
11:23:35.843 26459-26580 SurfaceManager                       I  GL released
11:23:35.846 26459-26459 VideoEncoder                         I  1 encoders found
11:23:35.846 26459-26459 VideoEncoder                         I  Encoder OMX.rk.video_encoder.avc
11:23:35.847 26459-26459 VideoEncoder                         I  Color supported: 2135033992
11:23:35.847 26459-26459 VideoEncoder                         I  Color supported: 19
11:23:35.847 26459-26459 VideoEncoder                         I  Color supported: 21
11:23:35.848 26459-26459 VideoEncoder                         I  Color supported: 2130708361
11:23:35.848 26459-26459 VideoEncoder                         I  Encoder selected OMX.rk.video_encoder.avc
11:23:35.850 26459-26459 MediaCodec                           I  callingProcessName:com.pedro.streamer
11:23:35.853   448-501   ActivityManager                      E  Found activity ActivityRecord{6618211 u0 com.pedro.streamer/.MainActivity t2102 f} in proc activity list using null instead of expected ProcessRecord{dc3896a 26459:com.pedro.streamer/u0a47}
11:23:35.854 26459-26584 OMXClient                            I  MuxOMX ctor
11:23:35.854 26459-26584 ACodec                               I  onAllocateComponent:6752 mSoftCodecPref:0 componentName:OMX.rk.video_encoder.avc
11:23:35.855 26459-26584 ACodec                               I  onAllocateComponent:6783 mSoftCodecPref:0 componentName:OMX.rk.video_encoder.avc
11:23:35.858 26459-26459 VideoEncoder                         I  Prepare video info: SURFACE, 640x480
11:23:35.860 26459-26459 VideoEncoder                         I  bitrate mode CBR not supported using default mode
11:23:35.861 26459-26583 MediaCodec                           I  MediaCodec will operate in async mode
11:23:35.865 26459-26584 ACodec                               W  do not know color format 0x7f000789 = 2130708361
11:23:35.867 26459-26584 ACodec                               I  setupAVCEncoderParameters with [profile: Baseline] [level: Level31]
11:23:35.868 26459-26584 ACodec                               I  [OMX.rk.video_encoder.avc] cannot encode color aspects. Ignoring.
11:23:35.868 26459-26584 ACodec                               I  [OMX.rk.video_encoder.avc] cannot encode HDR static metadata. Ignoring.
11:23:35.868 26459-26584 ACodec                               I  setupVideoEncoder succeeded
11:23:35.869 26459-26584 ACodec                               W  do not know color format 0x7f000789 = 2130708361

@pedroSG94
Copy link
Owner

pedroSG94 commented Apr 23, 2024

Hello,

Thank you for the logs. I suppose that you are using Rotation example. If not, let me know the example used. I did the fix for Rotation example and I will add it to all if it is working fine.
Try with this branch:
#1462
This should fix the problem. If not, please, share me a logcat

@LiPengfei0106
Copy link
Author

hello,

Thank you for your reply.

Rotation is worked. Just setCameraOrientation(0) not very friendly, maybe use VideoSource rotation control the stream rotation?

#1462
fixed the stop error, but there are still Handler warn:

18:50:43.819 10917-11248 Pedro                                E  id: 321, name: pool-13-thread-1
18:50:43.847 10917-11248 Pedro                                E  id: 321, name: pool-13-thread-1
18:50:43.849 10917-11030 RtmpSender                           I  wrote Video packet, size 194
18:50:43.860 10917-11030 RtmpSender                           I  wrote Audio packet, size 472
18:50:43.861 10917-11030 RtmpSender                           I  wrote Audio packet, size 565
18:50:43.881 10917-11030 RtmpSender                           I  wrote Video packet, size 118
18:50:43.884 10917-11248 Pedro                                E  id: 321, name: pool-13-thread-1
18:50:43.892 10917-11085 MessageQueue                         W  Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {24fd987} sending message to a Handler on a dead thread
                                                                 java.lang.IllegalStateException: Handler (android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallbackDelegate) {24fd987} sending message to a Handler on a dead thread
                                                                 	at android.os.MessageQueue.enqueueMessage(MessageQueue.java:543)
                                                                 	at android.os.Handler.enqueueMessage(Handler.java:647)
                                                                 	at android.os.Handler.sendMessageAtTime(Handler.java:616)
                                                                 	at android.os.Handler.sendMessageDelayed(Handler.java:586)
                                                                 	at android.os.Handler.sendEmptyMessageDelayed(Handler.java:550)
                                                                 	at android.os.Handler.sendEmptyMessage(Handler.java:535)
                                                                 	at android.hardware.display.DisplayManagerGlobal$VirtualDisplayCallback.onStopped(DisplayManagerGlobal.java:497)
                                                                 	at android.hardware.display.IVirtualDisplayCallback$Stub.onTransact(IVirtualDisplayCallback.java:61)
                                                                 	at android.os.Binder.execTransact(Binder.java:565)
18:50:43.905 10917-10917 MicrophoneManager                    I  Microphone stopped
18:50:43.905 10917-10917 Pedro                                E  removeMediaCodecSurface
18:50:43.912 10917-11248 SurfaceManager                       I  GL released
18:50:43.912 10917-10917 Pedro                                E  stop glInterface
18:50:43.913 10917-11030 CommandsManager                      I  send Command(name='closeStream', transactionId=6, timeStamp=0, streamId=1, data=[AmfString value: closeStream, AmfNumber value: 6.0, AmfNull], bodySize=24)
18:50:43.916 10917-11248 SurfaceManager                       I  GL released
18:50:43.916 10917-11248 SurfaceManager                       E  GL already released
18:50:43.918 10917-11248 SurfaceManager                       I  GL released
18:50:43.919 10917-11248 libEGL                               E  call to OpenGL ES API with no current context (logged once per thread)
18:50:43.922 10917-10917 VideoEncoder                         I  stopped
18:50:43.960 10917-10917 AudioEncoder                         I  stopped

@pedroSG94
Copy link
Owner

pedroSG94 commented Apr 23, 2024

Hello,

fixed the stop error, but there are still Handler warn:

Nice, I will remove logs and add the fix.
You can ignore it. This is an internal warning of the Executor class but it is save to ignore.

Rotation is worked. Just setCameraOrientation(0) not very friendly, maybe use VideoSource rotation control the stream rotation?

Which one? This?:
https://github.com/pedroSG94/RootEncoder/blob/master/app/src/main/java/com/pedro/streamer/screen/ScreenService.kt#L146

It is totally necessary to use always 0. You can't do it inside VideoSource because you haven't access to glInterface inside a VideoSource.

I'm not sure if add a setter or similar because I don't want make VideoSource depend of glInterface to allow use it without glInterface.
For example, you actually can use Camera2Source with a SurfaceView only with a Camera2Source instance but in case I add glInterface to VideoSource you will need handle with a glInterface.

Other option could be check if you are using a ScreenSource and set that value to 0 before call create method but add a conditional only for a VideoSource feels like a bad idea.

What do you think about it? do you have a better idea? which option do you like (current, glInterface setter or internal ScreenSource conditional)?

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