JSFiddle - React, Tailwind, and code Playground

by fippo

HTML

<div id='codec'></div>

JavaScript

const pc1 = new RTCPeerConnection();
const pc2 = new RTCPeerConnection();
pc1.onicecandidate = e => pc2.addIceCandidate(e.candidate);
pc2.onicecandidate = e => pc1.addIceCandidate(e.candidate);
async function run() {
  const stream = await navigator.mediaDevices.getUserMedia({video: true});
  pc1.addTrack(stream.getTracks()[0], stream);
  pc1.getTransceivers()[0].setCodecPreferences([{clockRate: 90000, mimeType: 'video/VP8'}]); // Only receive VP8
  await pc1.setLocalDescription();
  const sdp = pc1.localDescription.sdp
  	.replace('m=video 9 UDP/TLS/RTP/SAVPF 96', 'm=video 9 UDP/TLS/RTP/SAVPF 97 96')
    .replace('a=rtpmap:96 VP8/90000', 'a=rtpmap:96 VP8/90000\r\na=rtpmap:97 VP9/90000');
  await pc2.setRemoteDescription({type: 'offer', sdp});
  await pc2.setLocalDescription();
  await pc1.setRemoteDescription(pc2.localDescription);
}
run();
setInterval(async () => {
  const stats = await pc1.getStats();
  const rtp = [...stats.values()].find(({type}) => type === 'outbound-rtp');
  if (rtp) {
  	document.getElementById('codec').innerText = stats.get(rtp.codecId).mimeType;
  }
}, 1000);