Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect offline peer in WebRTC connection

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?
like image 863
str Avatar asked Feb 23 '16 12:02

str


1 Answers

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/

like image 178
jib Avatar answered Sep 18 '22 15:09

jib