问题描述
更新
看来我可以按照 myPeerConnection.getStats()="nofollow noreferrer">这里 我可以测量发送或接收的字节数.如果它们增加,则意味着我们已连接并且 disconnected
ICE 状态将被视为临时状态.否则,它是永久性的.但是现在我对应该测量哪个字节感到困惑.有 inbound-rtp
、outbound-rtp
、remote-inbound-rtp
和 remote-outbound-rtp
.
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
ICE state will be treated as temporary. otherwise, it is permanent.But now I am confused about which byte I should measure. There inbound-rtp
, outbound-rtp
, remote-inbound-rtp
and remote-outbound-rtp
.
我想确保双方实际上都在接收彼此的数据.那么我应该从以上四个方面衡量什么?
I want to make sure that both sides are actually receiving data from each other. So what should I measure from the above four?
原创
有时在不稳定的网络上,ICE 状态可能会更改为已断开连接",并且通常会尝试自行恢复.失败"状态将需要重新协商 ICE.但是在某些情况下,其他对等方刚刚失去连接或死亡,在这种情况下,我将断开连接",然后在一段时间后出现失败"状态.我需要知道对等连接什么时候还活着,什么时候死了,以便我可以采取适当的行动.
Sometimes on unstable networks ICE state can change to 'Disconnected' and will normally try to recover on its own. 'Failed' state will need ICE renegotiated. But there will be cases when the other peer has just lost connection or died and in that case I will get 'Disconnected' and then after sometime 'Failed' states. I need to know when the peer connection is still alive and when it is dead so that I can take appropriate action.
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": // This will not restart ICE negotiation on its own and must be restarted/
myPeerConnection.restartIce();
break;
case "disconnected":
//This will resolve on its own. No need to close connection.
//But in case the other peer connection is dead we want to call the below function.
//hangUpCall(); //Hangup instead of closevideo() because we want to record call end in db
//break;
}
}
我想要类似的东西
case "disconnected":
if(!otherPeerConnected){
hangUpCall();
}
有没有办法做到这一点?
Is there anyway to do this?
谢谢
推荐答案
来自 MDN 我明白了
入站 rtp:一个 RTCInboundRtpStreamStats 对象,提供有关从远程对等方接收的入站数据的统计信息.由于这仅提供与入站数据相关的统计信息,而未考虑本地对等方的状态,因此不包括需要了解两者的任何值,例如往返时间.如果没有连接的对等点,则此报告不可用
我现在将使用它,如下所示,以防其他人将来需要它.
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;
}
这篇关于webRTC - 区分临时断开连接或故障与永久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!