android webrtc 资源释放的问题

  最近的音视频研究的有点多额。。。。。。

 今天来说下我遇到的问题

1.从A页面点击按钮跳转到扫描二维码页面,首次可以成功,进入音视频通话页面后挂断视频,再次扫描就会出现,failed to connect camera service 这个。。真的是很糟心。原来,webrtc里面有一个VideoSource 里面封装了camera资源,而我。。没有释放掉。soga 下面,来看看俺写的那个webrtc资源释放方法吧。。

 /***
     * 处理挂断视频
     * */
    public void handleHangUp() {
        if (null != videoChatCallback) {
            videoChatCallback.onCloseWithUserId(GetGlassInfo.getInstance().getAppId());
            videoChatCallback.onCloseLocalStream(localMediaStream, GetGlassInfo.getInstance().getAppId());
        }
        mWebconnectManager.closePeerConnection(GetGlassInfo.getInstance().getWebId());
        if (null != localMediaStream) {
            localMediaStream = null;
        }
    }

这个主要有两个会方法

onCloseLocalStream 这个呢,主要是一个activity的回调 这里面主要做的就是一移除画面

           if (null != localRender) {
                    VideoRendererGui.remove(localRender);
                    localRender = null;
                }
                finish();

最重要的是 closePeerConnection 这个方法 来看下

 public void closePeerConnection(String connectionId) {
        Log.e("sssss", "peers==" + peers.toString() + "---connectionId==" + connectionId);
        if (null != peers) {
            //关闭peerconnecttion连接
            Peer peer = peers.get(connectionId);
            if (peer != null) {
                PeerConnection connection = peer.pc;
                if (connection != null) {
                    connection.close();
                    connection.dispose();
                }
                peers.remove(connectionId);
            } else {
                Log.e("ssssss", "peerc===null");
            }
            if (null != audioSource) {//释放音频资源
                audioSource.dispose();
                audioSource = null;
            }
            if (null != videoSource) {//释放视频资源
                videoSource.dispose();
                videoSource = null;
            }

            if (capture != null) {释放画面
                capture.dispose();
                capture = null;
            }
            if (null != factory) {//释放掉PeerConnecttionFactroy
                factory.dispose();
                factory = null;
            }
        }
    }

这里面主要做的就是资源释放的东西啦。。。

再然后。。。

在视频画面的activity中 


    @Override
    protected void onPause() {
        super.onPause();
        Log.e("sssss", "onPause==" + isConnect);
        if (glSurfaceView != null) {
            glSurfaceView.onPause();
        }
        mWebSocketManager.stopVideoSource();

    }

    @Override
    protected void onResume() {
        super.onResume();
        if (glSurfaceView != null) {
            glSurfaceView.onResume();
        }
        mWebSocketManager.restartVideoSource();
        
    }

附上 stop 和 resume的方法

    private boolean isStop = false;  
public void restartVideoSource() {
        if (null != videoSource && isStop) {
            videoSource.restart();
        }
    }

    public void stopVideoSource() {
        if (null != videoSource) {
            Log.e("ssssss", "videoSource stop");
            videoSource.stop();
            isStop = true;
        }
    }

以上,是目前为止测试过的webrtc资源释放的东西,希望自己以后能记住,啊哈 ,关于webrtc android 更多内容,待我慢慢道来 哈哈

猜你喜欢

转载自blog.csdn.net/zww986736788/article/details/88398853
今日推荐