如何正确关闭android webrtc连接?

5kgi1eie  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(601)

我在服务和活动中使用webrtc库。在这两个组件的“ondestroy”中,我尝试完全断开连接,以便在服务/活动再次运行时,设备可以使用相同的过程加入room。
首先,我尝试了以下这些旧代码片段:

public void onDestroy() {
    for (Peer peer : peers.values()) {
        peer.pc.dispose();
    }
    videoSource.dispose();
    factory.dispose();
    client.off();<---- You need to turn OFF and then disconnect  and then close it.
    client.disconnect();
    client.close();
}

但活动/服务因此错误而崩溃(我的代码中没有泄漏):

Channel is unrecoverably broken and will be disposed!

然后我了解到处理连接和工厂可以弥补这个错误。所以我把它们设为空。ondestroy回调结果如下:

if(videoTrackFromCamera != null) videoTrackFromCamera.dispose();
    //if(rootEglBase != null) rootEglBase.release(); //Causes Crash:: # Fatal error in ../../webrtc/sdk/android/src/jni/peerconnection_jni.cc, line 1031
    //    # last system error: 0
    //    # Check failed: 0 == (reinterpret_cast<MediaStreamInterface*>(j_p))->Release() (0 vs. 2)
    //    # Unexpected refcount.
    if(audioSource != null) audioSource.dispose();
    if(localAudioTrack != null) localAudioTrack.dispose();

    if(localMediaStream != null) localMediaStream.dispose();
    if(mRemoteMediaStream != null) mRemoteMediaStream.dispose();
    audioConstraints = null;

    if (factory != null) {
        factory.stopAecDump();
    }
    if (peerConnection != null) {
        //peerConnection.dispose();
        //peerConnection.close();
        peerConnection = null;
    }
    if (factory != null) {
        //factory.dispose(); //causes error  Channel is unrecoverably broken and will be disposed
        factory = null;
    }
    if (socket != null) {
        sendMessage("finished");
        socket.disconnect();
        socket.close();
    }

现在一切都很好,只是有时我会因为这个错误而崩溃:

Channel {0} was not shutdown properly!!! ~*~*~*
        Make sure to call shutdown()/shutdownNow() and wait until awaitTermination() returns true.
    java.lang.RuntimeException: ManagedChannel allocation site

有没有更好的方法结束webrtc连接?如果我想呆在房间里,但停止发送和/或接收流,最佳的方式是什么?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题