WebRTC丢包监控
利用RTCPeerConnection
对象的getStats
方法拿到WebRTC两端之间的接收发出数据统计报告进行分析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
|
let lastPacketsReceived = 0
let lastPacketsLost = 0
let lastPacketsSent = 0
let lastRemotePacketsLost = 0
async function getRTCStats() { try { const pc = cur_call.rtc.peer.peer let packetsSent = 0 let packetsReceived = 0 let packetsLost = 0 pc.getStats(null).then(stats => { stats.forEach(report => { if (report.type === 'inbound-rtp') { packetsReceived = report.packetsReceived - lastPacketsReceived lastPacketsReceived = report.packetsReceived lastPacketsLost = report.packetsLost } if (report.type === 'outbound-rtp') { packetsSent = report.packetsSent - lastPacketsSent lastPacketsSent = report.packetsSent } if (report.type === 'remote-inbound-rtp') { packetsLost = report.packetsLost - lastRemotePacketsLost lastRemotePacketsLost = report.packetsLost } }) const fractionLost = (packetsSent - packetsReceived) / packetsSent const outboundLost = packetsLost / packetsSent if (fractionLost > 0.3 || outboundLost > 0.3) { } }) } catch (e) { console.log(e) } }
setInterval(() => { getRTCStats() },5 * 1000)
|
参考文章1
参考文章2
参考文章3