RootEncoder icon indicating copy to clipboard operation
RootEncoder copied to clipboard

[Camera1Base, Camera2Base] stopRecord() freezes

Open kenle opened this issue 3 years ago • 6 comments

Running this code causes the camera to freeze after stopRecord() https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/blob/812078d899d7d7491c81451164bf48d907dd6344/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java#L449

Update the code to below seems to fix the issue.

if (streaming) stopStream();

kenle avatar Dec 20 '21 07:12 kenle

Hello,

What do you means with freeze? Which view or example activity are you using?

Your suggestion produce a bug because you need stop encoders

pedroSG94 avatar Dec 20 '21 09:12 pedroSG94

Hi @pedroSG94 , freeze as in the camera preview stops moving, it freezes on the last frame when performing stopRecord() function.

Yes, we want to stop the encoders, but if I'm not mistaken the current code reads: "If NOT streaming, stop the stream".

I think it should read: "If IS streaming, stop the stream".

I believe you suggested a similar solution here: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/issues/468#issuecomment-568454771

(please note, I am currently recording to file only, I am not running an RTMP stream)

kenle avatar Dec 20 '21 16:12 kenle

Yes, we want to stop the encoders, but if I'm not mistaken the current code reads: "If NOT streaming, stop the stream".

Yes, but if you read stopStream method this means stop encoders because stopStreamRtp line is not executed: https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/blob/916d3a7f1bb9bd6e8868cbcdb0ebf611e3d5d7ae/rtplibrary/src/main/java/com/pedro/rtplibrary/base/Camera2Base.java#L672 I used stopStream method to avoid duplicated code but basically I'm doing this:

    if (!recordController.isRecording()) {
      onPreview = !isBackground;
      if (audioInitialized) microphoneManager.stop();
      if (glInterface != null) {
        glInterface.removeMediaCodecSurface();
        if (glInterface instanceof OffScreenGlThread) {
          glInterface.stop();
          cameraManager.closeCamera();
        }
      } else {
        if (isBackground) {
          cameraManager.closeCamera();
          onPreview = false;
        } else {
          cameraManager.stopRepeatingEncoder();
        }
      }
      videoEncoder.stop();
      if (audioInitialized) audioEncoder.stop();
      recordController.resetFormats();
    }

For this reason your solution is not valid and I need know view used in constructor (SurfaceView, OpenGlView, TextureView or Context). If you are using SurfaceView or TextureView, please try to use OpenGlView. Also, test it using my app example (to discard code errors and make easy to reproduce for me), let me know device used and try reproduce the problem with other device

pedroSG94 avatar Dec 20 '21 21:12 pedroSG94

Thanks for the above reference, I should have dug deeper. I believe I might understand why now.

I'm integrating your library into a custom Flutter plugin, so I extended some of your classes to render the preview onto a Flutter surface texture. I used the RTMPCamera2 constructor that instantiates an OffScreenGlThread (referenced below) with some minor edits to get the Flutter surface to be added to the surfaceList for camera preview. https://github.com/pedroSG94/rtmp-rtsp-stream-client-java/blob/916d3a7f1bb9bd6e8868cbcdb0ebf611e3d5d7ae/rtplibrary/src/main/java/com/pedro/rtplibrary/rtmp/RtmpCamera2.java#L81

For the case of OffScreenGlThread and my situation, perhaps these lines are causing my camera preview to freeze:

        if (glInterface instanceof OffScreenGlThread) {
          glInterface.stop();
          cameraManager.closeCamera();
        }

I will try some things and understand your library better to see if I can get things working. The problem here may be the way I use your library and not a problem with your code. Thanks.

kenle avatar Dec 20 '21 22:12 kenle

@pedroSG94 this issue has been resolved. For my use-case I do not wish to stop the glInterface and close the camera on stop record. I've extended the class for my purpose. Thanks again for your help.

kenle avatar Dec 30 '21 00:12 kenle

That is good to know. Closing issue since it is solved. You can reopen it or open other issue if you have any problems

pedroSG94 avatar Dec 30 '21 17:12 pedroSG94