Async WebRTC failed
by jib1
HTML
<div id="div"></div>
JavaScript
const console = { log: msg => div.innerHTML += msg + "<br>" };
const logger = f => console.log(`${f.toString().slice(6)} = ${f()}`);
const wait = ms => new Promise(r => setTimeout(r, ms));
const pc1 = new RTCPeerConnection(), pc2 = new RTCPeerConnection();
(async () => {
try {
const tc1 = pc1.addTransceiver("video");
await new Promise(r => pc1.onsignalingstatechange = r);
const ice1 = tc1.sender.transport?.iceTransport;
if (ice1) {
logger(() => ice1.state);
logger(() => ice1.gatheringState);
logger(() => ice1.getSelectedCandidatePair());
ice1.onstatechange = () => logger(() => ice1.state);
ice1.ongatheringstatechange = () => logger(() => ice1.gatheringState);
ice1.onselectedcandidatepairchange = () => logger(() => ice1.getSelectedCandidatePair());
}
await new Promise(r => pc2.onsignalingstatechange = r);
await new Promise(r => pc2.onsignalingstatechange = r);
const [tc2] = pc2.getTransceivers();
const ice2 = tc2.sender.transport?.iceTransport;
if (ice2) {
logger(() => ice2.state);
logger(() => ice2.gatheringState);
logger(() => ice2.getSelectedCandidatePair());
ice2.onstatechange = () => logger(() => ice2.state);
ice2.ongatheringstatechange = () => logger(() => ice2.gatheringState);
ice2.onselectedcandidatepairchange = () => logger(() => ice2.getSelectedCandidatePair());
}
} catch (e) {
console.log(e);
}
})();
async function addIce(pc, candidate) {
try {
if (candidate) return; // comment out this line to unblock all candidates
await pc.addIceCandidate(candidate);
} catch (e) {
console.log(e);
}
}
pc1.onicecandidate = e => addIce(pc2, e.candidate);
pc2.onicecandidate = e => addIce(pc1, e.candidate);
pc1.onicecandidateerror = e => console.log("ERROR");
pc2.onicecandidateerror = e => console.log("ERROR");
pc1.onconnectionstatechange = () => logger(() => pc1.connectionState);
pc1.oniceconnectionstatechange = () =>...