Skip to content Skip to sidebar Skip to footer

WebRTC - Differentiate Between Temporary Disconnect Or Failure And Permanant

UPDATE It seems I can do myPeerConnection.getStats() as described here I can measure the bytes sent or recieved. If they increase that means we are connected and the disconnected I

Solution 1:

from MDN I got this

inbound-rtp: An RTCInboundRtpStreamStats object providing statistics about inbound data being received from remote peers. Since this only provides statistics related to inbound data, without considering the local peer's state, any values that require knowledge of both, such as round-trip time, is not included. This report isn't available if there are no connected peers

I am now going to use that as shown below, in case anyone else wants it in future.

function handleICEConnectionStateChangeEvent(event) {
  log("*** ICE connection state changed to " + myPeerConnection.iceConnectionState);

  switch(myPeerConnection.iceConnectionState) {
    case "closed": // This means connection is shut down and no longer handling requests.
        hangUpCall(); //Hangup instead of closevideo() because we want to record call end in db
        break;
    case "failed":
        checkStatePermanent('failed');
        break;
    case "disconnected":
        checkStatePermanent('disconnected');
        break;
  }
}


 const customdelay = ms => new Promise(res => setTimeout(res, ms));


async function checkStatePermanent (iceState) {
    videoReceivedBytetCount = 0;
    audioReceivedByteCount = 0;

    let firstFlag = await isPermanentDisconnect();

    await customdelay(2000);

    let secondFlag = await isPermanentDisconnect(); //Call this func again after 2 seconds to check whether data is still coming in.

    if(secondFlag){ //If permanent disconnect then we hangup i.e no audio/video is fllowing
        if (iceState == 'disconnected'){
            hangUpCall(); //Hangup instead of closevideo() because we want to record call end in db
        }
    }
    if(!secondFlag){//If temp failure then restart ice i.e audio/video is still flowing
         if(iceState == 'failed') {
            myPeerConnection.restartIce();
        }
    }
};

var videoReceivedBytetCount = 0;
var audioReceivedByteCount = 0; 


async function isPermanentDisconnect (){
    var isPermanentDisconnectFlag = false;
    var videoIsAlive = false;
    var audioIsAlive = false;

    await myPeerConnection.getStats(null).then(stats => {
        stats.forEach(report => {
            if(report.type === 'inbound-rtp' && (report.kind === 'audio' || report.kind  === 'video')){ //check for inbound data only
                if(report.kind  === 'audio'){
                    //Here we must compare previous data count with current
                    if(report.bytesReceived > audioReceivedByteCount){
                        // If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
                        audioIsAlive = true;
                    } else {
                        audioIsAlive = false;
                        
                    }
                    audioReceivedByteCount = report.bytesReceived;
                }
                if(report.kind  === 'video'){
                    if(report.bytesReceived > videoReceivedBytetCount){
                        // If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
                        videoIsAlive = true;
                    } else{
                        videoIsAlive = false;
                    }
                    videoReceivedBytetCount = report.bytesReceived;
                }
                if(audioIsAlive || videoIsAlive){ //either audio or video is being recieved.
                    isPermanentDisconnectFlag = false; //Disconnected is temp
                } else {
                    isPermanentDisconnectFlag = true;
                }
            }
        })
    });

    return isPermanentDisconnectFlag;
}

Post a Comment for "WebRTC - Differentiate Between Temporary Disconnect Or Failure And Permanant"