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.

functionhandleICEConnectionStateChangeEvent(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 dbbreak;
    case"failed":
        checkStatePermanent('failed');
        break;
    case"disconnected":
        checkStatePermanent('disconnected');
        break;
  }
}


 constcustomdelay = ms => newPromise(res =>setTimeout(res, ms));


asyncfunctioncheckStatePermanent (iceState) {
    videoReceivedBytetCount = 0;
    audioReceivedByteCount = 0;

    let firstFlag = awaitisPermanentDisconnect();

    awaitcustomdelay(2000);

    let secondFlag = awaitisPermanentDisconnect(); //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 fllowingif (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 flowingif(iceState == 'failed') {
            myPeerConnection.restartIce();
        }
    }
};

var videoReceivedBytetCount = 0;
var audioReceivedByteCount = 0; 


asyncfunctionisPermanentDisconnect (){
    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 onlyif(report.kind  === 'audio'){
                    //Here we must compare previous data count with currentif(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"