简体   繁体   English

在 WebRTC 连接中检测离线对等点

[英]Detect offline peer in WebRTC connection

We are developing a video stream from a mobile device to a computer using WebRTC.我们正在使用 WebRTC 开发从移动设备到计算机的视频流。 The mobile device might lose its connection completely and the computer should be able to detect that.移动设备可能会完全失去连接,而计算机应该能够检测到。 Right now, the video just freezes.现在,视频只是冻结。 But neither of the EventHandler s of RTCPeerConnection are called in such a situation.但是在这种情况下RTCPeerConnectionEventHandlerRTCPeerConnection被调用。

  • So how can such a connection failure be detected on the other peer?那么如何在另一个对等点上检测到这样的连接失败呢?
  • How can a peer detect connection problems on connection establishment in the first place?首先,对等方如何检测连接建立时的连接问题?

As a workaround in Firefox, you could use getStats to detect if packets stop coming in:作为 Firefox 中的解决方法,您可以使用getStats来检测数据包是否停止进入:

var findStat = (m, type) => [...m.values()].find(s => s.type == type && !s.isRemote);

var hasConnected = new Promise(resolve => pc.oniceconnectionstatechange =
  e => pc.iceConnectionState == "connected" && resolve());

var hasDropped = hasConnected.then(() => new Promise(resolve => {
  var lastPackets = countdown = 0, timeout = 3; // seconds

  var iv = setInterval(() => pc.getStats().then(stats => {
    var packets = findStat(stats, "inbound-rtp").packetsReceived;
    countdown = (packets - lastPackets)? timeout : countdown - 1;
    if (!countdown) resolve(clearInterval(iv)); 
    lastPackets = packets;
  }), 1000);
}));

Here's a demo: https://jsfiddle.net/4rzhe7n8/这是一个演示: https : //jsfiddle.net/4rzhe7n8/

the iceconnectionstatechange handler should fire after ~5-10 seconds of not receiving data from the peer anymore (in Chrome; Firefox is working on that currently). iceconnectionstatechange 处理程序应该在大约 5-10 秒后不再从对等方接收数据后触发(在 Chrome 中;Firefox 目前正在处理)。 See https://webrtc.github.io/samples/src/content/peerconnection/states/ for an example.有关示例,请参见https://webrtc.github.io/samples/src/content/peerconnection/states/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM