WebRTC getStats() logging
by Andreas Pehrson
HTML
<script src="https://cdn.rawgit.com/webrtc/adapter/master/adapter.js"></script>
<div id="log" style="white-space:pre;font-family:Courier New;">
<p><b>Log</b></p>
</div>
JavaScript
var logs = document.getElementById("log");
function log() {
// Make an array copy of arguments to have access to array
// methods like map, join.
var args = new Array(arguments.length);
for(var i = 0; i < args.length; ++i) {
args[i] = arguments[i];
}
var p = document.createElement('p');
var repr = arg => {
if (typeof(arg) == "object") {
return JSON.stringify(arg, null, 2);
} else {
return arg;
}
};
p.innerHTML = args.map(a => repr(a))
.join(" ");
logs.appendChild(p);
}
var stream;
var pcSend = new window.RTCPeerConnection();
var pcRecv = new window.RTCPeerConnection();
window.pcSend = pcSend;
window.pcRecv = pcRecv;
pcSend.name = "pcSend";
pcRecv.name = "pcRecv";
function setupIceGathering(from, to) {
var success = () => log("Added an ice candidate from", from.name, "to", to.name);
var failure = err => log("Failed to add ice candidate from", from.name, "to", to.name);
from.onicecandidate = event => {
if (!event.candidate) {
log("Finished gathering ice candidates from", from.name);
return;
}
to.addIceCandidate(event.candidate, success, failure);
}
}
function waitForConnected(pc) {
return new Promise(resolve => pc.oniceconnectionstatechange = () => {
log("State changed on", pc.name, "to", pc.iceConnectionState);
if (pc.iceConnectionState === "connected") {
resolve();
}
});
}
var pcSendConnected = waitForConnected(pcSend);
var pcRecvConnected = waitForConnected(pcRecv);
setupIceGathering(pcSend, pcRecv);
setupIceGathering(pcRecv, pcSend);
var sendOffer;
var recvAnswer;
var stream;
var remoteStream;
navigator.mediaDevices.getUserMedia({audio: true, video: true})
.then(s => stream = s)
.then(() => log("gUM OK", stream))
.then(() => pcSend.addStream(stream))
.then(() => new Promise((y, n) => pcSend.createOffer(y, n)))
.then(offer => sendOffer = offer)
.then(() => log("sendOffer OK",...