WebRTC - RTCPeerConnection.onconnectionstatechange fires with RTCPeerConnection.connectionState = 'disconnected' without any reason

Viewed 28

I am working on the WebRTC application for video chatting. On my local network everything works well. But when I try it to test through internet RTCPeerConnection.onconnectionstatechange fires with RTCPeerConnection.connectionState = 'disconnected' without any reason after some 20-30 seconds of communication. Another very confusing thing is that for example I have peer2 and peer3 started in the same browser in different tabs connected to peer1 and peer1 videostreaming to them. And after 20-30 seconds RTCPeerConnection.connectionState = 'disconnected' can fire on peer2 and at the same time peer3 continues to receive video stream from peer1. I have googled a bit and found this solution (which doesnt work in my case):

this.myRTCMediaMediatorConnections[id][hash].onconnectionstatechange=async function(e){
    log('onSignalingServerMediaMediatorOfferFunc.myRTCMediaMediatorConnections['+id+']['+hash+'].onconnectionstatechange('+This.myRTCMediaMediatorConnections[id][hash].connectionState+')',10,true)
    switch(This.myRTCMediaMediatorConnections[id][hash].connectionState){
        case "failed":
            This.disconnectMeFromMediatorConnection(targetId,logicGroupName,streamerId,streamerHash,id,hash)
            break
        case "closed":
            This.disconnectMeFromMediatorConnection(targetId,logicGroupName,streamerId,streamerHash,id,hash)
            break
        case "disconnected":
            if(await This.confirmPeerDisconnection(This.myRTCMediaMediatorConnections[id][hash]))This.disconnectMeFromConnection(targetId,logicGroupName,streamerId,streamerHash,id,hash)
            break
    }
    log('onSignalingServerMediaMediatorOfferFunc.myRTCMediaMediatorConnections['+id+']['+hash+'].onconnectionstatechange',10,false)
}    

this.confirmPeerDisconnection=async function(connectionObject){
    log('confirmPeerDisconnection',10,true)
    var b1=await this.confirmPeerDisconnectionFunc(connectionObject);
    await new Promise(resolve=>setTimeout(resolve,2000));
    var b2=await this.confirmPeerDisconnectionFunc(connectionObject);
    log('confirmPeerDisconnection=>'+(b2-b1),10,false)
    if(b2-b1>0)return false
    return true;
}

this.confirmPeerDisconnectionFunc=async function(connectionObject){
    var b=0
    await connectionObject.getStats(null).then(function(stats){
        stats.forEach((report)=>{if(report.type=='transport')Object.keys(report).forEach((statName)=>{if(statName==='bytesReceived')b=parseInt(report[statName])})})
    })
    return b
}

b2-b1 always equals 0 or less than 0. Can anyone give me an advise why RTCPeerConnection.onconnectionstatechange fires and how I can get rid of this bug.

Any help appriciated!

0 Answers
Related