Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

javascript - 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?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...