我们正在使用WebRTC开发从移动设备到计算机的视频流。移动设备可能完全失去连接,计算机应该能够检测到它。现在,视频就冻结了。但在这种情况下,EventHandler
s of RTCPeerConnection
都没有被调用。
答案 0 :(得分:8)
iceconnectionstatechange处理程序应该在大约5-10秒之后不再从对等方接收数据时触发(在Chrome中; Firefox目前正在进行此操作)。有关示例,请参阅https://webrtc.github.io/samples/src/content/peerconnection/states/。
答案 1 :(得分:8)
作为Firefox中的解决方法,您可以使用getStats
来检测数据包是否停止进入:
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);
}));