Autonomous peer discovery in a WebRTC connection

We are developing a video stream from a mobile device to a computer using WebRTC. A mobile device may completely lose the connection and the computer will be able to detect it. Right now the video just freezes. But none of the EventHandler of RTCPeerConnection is called in this situation.

  • So, how can you detect such a connection failure on another node?
  • How can I identify connection problems when establishing a connection in the first place?
+5
source share
2 answers

the iceconnectionstatechange handler should go away ~ 5-10 seconds after it no longer receives data from the partner (in Chrome, Firefox is currently working on it). For an example, see https://webrtc.imtqy.com/samples/src/content/peerconnection/states/ .

+8
source

As a workaround in Firefox, you can use getStats to determine if packages are stopping:

 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 is a demo: https://jsfiddle.net/4rzhe7n8/

+8
source

Source: https://habr.com/ru/post/1243659/


All Articles