Async WebRTC two-way tab demo
by jib1
HTML
<video id="v1" height="120" width="160" autoplay></video>
<video id="v2" height="120" width="160" autoplay></video><br>
<button onclick="call()">Call!</button><br><div id="div"></div>
<script src="https://webrtchacks.github.io/adapter/adapter-latest.js"></script>
<script src="https://jan-ivar.github.io/localSocket/localSocket.js"></script>
JavaScript
let pc = new RTCPeerConnection();
let gUM = c => navigator.mediaDevices.getUserMedia(c);
let call = async () => pc.addStream(v1.srcObject = await gUM({video: true, audio: true}));
pc.onaddstream = e => v2.srcObject = e.stream;
pc.oniceconnectionstatechange = e => div.innerHTML += pc.iceConnectionState + "<br>";
pc.onicecandidate = e => sc.send({ice: e.candidate});
pc.onnegotiationneeded = async e => {
await pc.setLocalDescription(await pc.createOffer());
sc.send({sdp: pc.localDescription});
}
var sc = new localSocket();
sc.onmessage = async ({data: {sdp, ice}}) => {
if (sdp) {
await pc.setRemoteDescription(sdp);
if (sdp.type == "offer") {
pc.addStream(v1.srcObject = await gUM({video: true, audio: true}));
await pc.setLocalDescription(await pc.createAnswer());
sc.send({sdp: pc.localDescription});
}
} else if (ice) await pc.addIceCandidate(ice);
}