WebRTC getStats selector dump
by jib1
HTML
<div id="div"></div><br>
<video id="video1" height="120" width="160" autoplay muted></video>
<video id="video2" height="120" width="160" autoplay></video>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>
CSS
body { white-space: pre; font-family: monospace; }
JavaScript
var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();
var selector;
navigator.mediaDevices.getUserMedia({ video: true, audio: true })
.then(stream => {
pc1.addStream(video1.srcObject = stream);
selector = stream.getAudioTracks()[0];
})
.catch(e => log(e));
var add = (pc, can) => can && pc.addIceCandidate(can).catch(log);
pc1.onicecandidate = e => add(pc2, e.candidate);
pc2.onicecandidate = e => add(pc1, e.candidate);
pc2.onaddstream = e => video2.srcObject = e.stream;
pc1.onnegotiationneeded = e =>
pc1.createOffer().then(d => pc1.setLocalDescription(d))
.then(() => pc2.setRemoteDescription(pc1.localDescription))
.then(() => pc2.createAnswer()).then(d => pc2.setLocalDescription(d))
.then(() => pc1.setRemoteDescription(pc2.localDescription))
.then(() => new Promise(resolve => pc1.oniceconnectionstatechange = e =>
pc1.iceConnectionState == "connected" && resolve()))
.then(() => (log("wait 5 seconds for rtcp packets..."), wait(5000)))
.then(() => pc1.getStats(selector).then(stats =>
log([...stats.values()].map(s => JSON.stringify(s, null, 2)))))
.catch(log);
var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var log = msg => div.innerHTML += msg + "<br>";