WebRTC for Android : VideoRendererGUI take a scree

2019-09-16 16:14发布

问题:

The demand is that saving a video frame in the video call. I have made a demo that take a screenshot through the GLSurfaceView's method "onDrawFrame". But when I use the webrtc, it have its own renderer "VideoRendererGUI" .And then when I want to override it, I find it can't be overrided. the main part code :

   vsv = (GLSurfaceView) findViewById(R.id.glviewchild_call);
    vsv.setPreserveEGLContextOnPause(true);
    vsv.setKeepScreenOn(true);

    VideoRendererGui.setView(vsv, new Runnable() {
        @Override
        public void run() {
            init();
        }
    });

And If you have another way to take a screenshot, you can also share with me. Thanks a lot!

回答1:

If you use a SurfaceViewRenderer to display the stream, you can use the solution in this answer to capture a frame on the call.

Basically, using surfaceViewRenderer.AddFrameListener with a class that implements EGLRenderer.FrameListener



回答2:

I will assume that the SurfaceViewRenderer of the peer you want to take a screenshot of is called remotePeerSurfaceViewRenderer, also I will asume that the button that you will use to take a screenshot is called btnScreenshot

So all what you need to do is as "Webo80" said in the answer above use the FrameListener, the issue is the FrameListener implementation of the webrtc takes only the first frame available once the Listener is attached to the SurfaceViewRenderer, so my approach to do that is by attaching the webrtc implementation of the FrameListsner at the second I need to take a screenshot and remove it once it is taken:

btnScreenshot.setOnClickListener((view)-> {
    remotePeerSurfaceViewRenderer.addFrameListsner(new EglRenderer.FrameListener() {
            @Override
            public void onFrame(Bitmap bitmap) {
                runOnUiThread(() -> {
                    /*
                    do what ever you want with the bitmap for example
                    imgView.setImageBitmap(bitmap);
                    */
                    localVideoView.removeFrameListener(this);
                });
            }
        }, 1);
})

Important note:

1. Please don't forget to runOnUiThread as Iam doing
2. Please don't forget to remove the listener inside the button onClick() method

I have tried this solution and it is working more than fine, if you want to make your custom solution you have to completely implement the interface "EglRenderer.FrameListener"



回答3:

I am sorry to say due to unavailability of canvas and other facilities in Android It's not possible to capture screenshot programatically using WebRTC. One can dodge this situation by animating the app's UI and capturing the screenshot manually , store it at configured location and exchange it with other party.