WebRTC disconnects after reconnecting (Android)

594 views Asked by At

Everything works fine but when I turn off my wifi and turn it on again, the WebRTC session is reconnected but after 6-7 seconds it goes to disconnected state and then failed state.

Logs

2020-09-19 19:34:27.112 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: CHECKING
2020-09-19 19:34:28.338 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: CONNECTED
2020-09-19 19:35:06.133 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: DISCONNECTED
2020-09-19 19:35:06.473 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: CONNECTED
2020-09-19 19:35:21.363 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: DISCONNECTED
2020-09-19 19:35:31.379 5941-6059/com.thirdeyegen.mr_workspacee D/NEW_WEBRTC_STATE==>: FAILED

Event 1, 2 initial session (Works fine) Event 3, 4, 5, 6 Series of events when I turn off wifi and connect again. On event 4 everything is working but 5, 6 happens after 6-7 seconds.

My code

public PeerConnection createLocalPeerConnection() {
    final List<PeerConnection.IceServer> iceServers = new ArrayList<>();
    PeerConnection.IceServer iceServer = PeerConnection.IceServer
            .builder("turn:" + turnUrl)
            .setUsername(turnUsername)
            .setPassword(turnPassword)
            .createIceServer();
    iceServers.add(iceServer);

    PeerConnection.RTCConfiguration rtcConfig = new PeerConnection.RTCConfiguration(iceServers);
    rtcConfig.tcpCandidatePolicy = PeerConnection.TcpCandidatePolicy.ENABLED;
    rtcConfig.bundlePolicy = PeerConnection.BundlePolicy.MAXBUNDLE;
    rtcConfig.rtcpMuxPolicy = PeerConnection.RtcpMuxPolicy.REQUIRE;
    rtcConfig.continualGatheringPolicy = PeerConnection.ContinualGatheringPolicy.GATHER_CONTINUALLY;
    rtcConfig.keyType = PeerConnection.KeyType.ECDSA;
    rtcConfig.enableDtlsSrtp = true;
    rtcConfig.enableRtpDataChannel = true;
    rtcConfig.sdpSemantics = PeerConnection.SdpSemantics.UNIFIED_PLAN;

    return peerConnectionFactory.createPeerConnection(rtcConfig, new CustomPeerConnectionObserver("local") {
        @Override
        public void onIceCandidate(IceCandidate iceCandidate) {
            super.onIceCandidate(iceCandidate);
            websocket.onIceCandidate(iceCandidate, localParticipant.getConnectionId());
        }

        @Override
        public void onSignalingChange(PeerConnection.SignalingState signalingState) {
            super.onSignalingChange(signalingState);
        }

        @Override
        public void onIceConnectionChange(PeerConnection.IceConnectionState iceConnectionState) {
            if (iceConnectionState.name().toLowerCase().equals("connected")) {
                isLocalConnected = true;
            }
            Log.d("NEW_WEBRTC_STATE==>", iceConnectionState.name());

            if (iceConnectionState.equals(PeerConnection.IceConnectionState.FAILED)) {
               checkForInternetAndReconnect();
            } else if (iceConnectionState.equals(PeerConnection.IceConnectionState.DISCONNECTED)) {
                activity.showConnectingAnimation(View.VISIBLE);
            } else {
                activity.showConnectingAnimation(View.GONE);
            }
            super.onIceConnectionChange(iceConnectionState);
        }

    });
}

P.S. I'm using code from OpenVidu android repository.

0

There are 0 answers