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);
pc2.ontrack = e => {
e.transceiver.setCodecPreferences([{clockRate: 90000, mimeType: 'video/VP9', sdpFmtpLine: 'profile-id=0'}]); // Only receive VP9
};
async function run() {
const stream = await navigator.mediaDevices.getUserMedia({video: true});
pc1.addTrack(stream.getTracks()[0], stream);
pc2.addTrack(stream.getTracks()[0], stream);
await pc1.setLocalDescription();
await pc2.setRemoteDescription(pc1.localDescription);
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 = '1:' + stats.get(rtp.codecId).mimeType;
}
const stats2 = await pc2.getStats();
const rtp2 = [...stats2.values()].find(({type}) => type === 'outbound-rtp');
if (rtp2) {
document.getElementById('codec').innerText += '\n2:' + stats2.get(rtp2.codecId).mimeType;
}
}, 1000);