I have following code where I call same function twice with delay of 2 seconds.
But all those log prints inside isPermanentDisconnect()
are printed at the end of checkStatePermanent
after log('After secondflag check')
is printed in the console. Can you please guide me as what is wrong with this code.
function customdelay(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {
}
}
function checkStatePermanent(iceState) {
videoReceivedBytetCount = 0;
audioReceivedByteCount = 0;
log('before First call')
let firstFlag = isPermanentDisconnect();
log('after First call')
log('Disconnect Flag is: ' + firstFlag)
customdelay(2000);
log('after 2 secs')
let secondFlag = isPermanentDisconnect(); //Call this func again after 2 seconds to check whether data is still coming in.
log('after second call')
log('Disconnect Flag is: ' + secondFlag)
if(secondFlag){ //If permanent disconnect then we hangup i.e no audio/video is fllowing
log('Disconnect Flag = Permanent')
if (iceState == 'disconnected'){
hangUpCall(); //Hangup instead of closevideo() because we want to record call end in db
}
}
log('After secondflag check')
...
}
function isPermanentDisconnect(){
var isPermanentDisconnectFlag = false;
var videoIsAlive = false;
var audioIsAlive = false;
myPeerConnection.getStats(null).then(stats => {
stats.forEach(report => {
log('Report Type: '+report.type+ ' Report Kind :'+ report.kind)
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;
}
log('Previous Audio count: ' +audioReceivedByteCount + ' Now audio count: ' + report.bytesReceived + ' audio is alive: '+ audioIsAlive)
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;
}
log('Previous video count: ' +videoReceivedBytetCount + ' Now video count: ' + report.bytesReceived + ' video is alive: ' + videoIsAlive)
videoReceivedBytetCount = report.bytesReceived;
}
if(audioIsAlive || videoIsAlive){ //either audio or video is being recieved.
log('Either video or audio is alive')
isPermanentDisconnectFlag = false; //Disconnected is temp
} else {
isPermanentDisconnectFlag = true;
}
}
})
});
return isPermanentDisconnectFlag;
}