3

I am using WebRTC Library in a service and an activity. In "onDestroy" of both components, I am trying to fully disconnect so that with the service/activity running again, the device can join room with the same procedure.

Firstly, I have tried these old code snippet here Combined with here:

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();
}

But the activity/service crashes with this error (There was no leaking in my code):

Channel is unrecoverably broken and will be disposed!

Then I learned that disposing connections and factory makes up for this error. So I just set them to null. The onDestroy callback turned out to be like this:

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();
    }

Now everything is fine except that only sometimes I get a crash with this error:

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

Is there a better way to end WebRTC connection? If I want to stay in the room but stop sending and or receiving streams, what is the optimal way?

Ali Has
  • 598
  • 1
  • 8
  • 24

0 Answers0