Detect offline peer in WebRTC connection

后端 未结 2 1049
北海茫月
北海茫月 2020-12-19 07:51

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 d

相关标签:
2条回答
  • 2020-12-19 08:07

    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/

    0 讨论(0)
  • 2020-12-19 08:09

    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 讨论(0)
提交回复
热议问题