Access deprecated legacy stats

by jib1

HTML

<video id="video1" height="120" width="160" autoplay muted></video>
<video id="video2" height="120" width="160" autoplay></video><br>
<button id="legacy" disabled>Access legacy stats</button><br>
<div id="div"></div>
<script src="https://webrtc.github.io/adapter/adapter-latest.js"></script>

JavaScript

var pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();
var log = msg => div.innerHTML += "<br>" + msg;

navigator.mediaDevices.getUserMedia({video: true})
  .then(stream => pc1.addStream(video1.srcObject = stream))
  .catch(log);

pc2.ontrack = e => {
  video2.srcObject = e.streams[0];
  pc1.getStats().then(stats => {
    log(JSON.stringify([...stats.values()][0]));
    legacy.onclick = () => log(JSON.stringify(stats[Object.keys(stats)[0]]));
    legacy.disabled = false;
  }).catch(log);
};

pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
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))
  .catch(log);