最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Detect offline peer in WebRTC connection - Stack Overflow

programmeradmin7浏览0评论

We are developing a video stream from a mobile device to a computer using 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 EventHandlers of RTCPeerConnection are called in such a situation.

  • 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?

We are developing a video stream from a mobile device to a computer using 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 EventHandlers of RTCPeerConnection are called in such a situation.

  • 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?
Share Improve this question edited Sep 4, 2017 at 18:41 jib 42.5k17 gold badges108 silver badges165 bronze badges asked Feb 23, 2016 at 12:31 strstr 45k18 gold badges114 silver badges134 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 10

As a workaround in Firefox, you could use getStats to detect if packets stop coming in:

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/

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). See https://webrtc.github.io/samples/src/content/peerconnection/states/ for an example.

发布评论

评论列表(0)

  1. 暂无评论