Compare commits

...

3 Commits

1 changed files with 18 additions and 20 deletions

View File

@ -204,6 +204,15 @@ form label {
var pc = undefined; var pc = undefined;
var dc = undefined; var dc = undefined;
function sendOffer() {
if (pc.iceGatheringState == "complete") {
sendJson({
description: pc.localDescription
});
}
}
function createRTCPeerConnection() { function createRTCPeerConnection() {
const pc = new RTCPeerConnection(); const pc = new RTCPeerConnection();
log("Created RTCPeerConnection."); log("Created RTCPeerConnection.");
@ -214,19 +223,17 @@ form label {
pc.oniceconnectionstatechange = e => { pc.oniceconnectionstatechange = e => {
log("pc.oniceconnectionstatechange: " + pc.iceConnectionState); log("pc.oniceconnectionstatechange: " + pc.iceConnectionState);
} }
pc.onicegatheringstatechange = e => { pc.onicegatheringstatechange = async function(e) {
log("pc.onicegatheringstatechange: " + pc.iceGatheringState); log("pc.onicegatheringstatechange: " + pc.iceGatheringState);
sendOffer();
} }
pc.onicecandidate = ({candidate}) => sendJson({candidate});
// let the "negotiationneeded" event trigger offer generation // let the "negotiationneeded" event trigger offer generation
// ... but only once icegathering is complete.
pc.onnegotiationneeded = async function () { pc.onnegotiationneeded = async function () {
log("In pc.onnegotiationneeded..."); log("In pc.onnegotiationneeded...");
await pc.setLocalDescription(await pc.createOffer()); await pc.setLocalDescription(await pc.createOffer());
sendJson({ sendOffer();
description: pc.localDescription
});
} }
pc.ontrack = ({streams: [stream]}) => { pc.ontrack = ({streams: [stream]}) => {
@ -262,7 +269,7 @@ form label {
log("Data channel open, sending settings...") log("Data channel open, sending settings...")
settings = readSettingsForm(true); settings = readSettingsForm(true);
sendJson({settings}); sendJson({settings});
startStartingWithErorrHandling(false); startStreamingWithErorrHandling(false);
} }
} }
@ -321,7 +328,7 @@ form label {
pc.addTrack(track, stream); pc.addTrack(track, stream);
} }
} }
function startStartingWithErorrHandling(fromButton) { function startStreamingWithErorrHandling(fromButton) {
startStreaming(fromButton) startStreaming(fromButton)
.then(() => { .then(() => {
log("startStreaming() finished."); log("startStreaming() finished.");
@ -332,7 +339,7 @@ form label {
} }
start.addEventListener("click", _ => { start.addEventListener("click", _ => {
startStartingWithErorrHandling(true) startStreamingWithErorrHandling(true)
}); });
async function receiveMessage(e) { async function receiveMessage(e) {
@ -343,26 +350,17 @@ form label {
if (data.ready) { if (data.ready) {
// Ready message means client is open and ready for connection. // Ready message means client is open and ready for connection.
pc = createRTCPeerConnection(); pc = createRTCPeerConnection();
} else if (data.requestSettings) {
settings = readSettingsForm(true);
startStartingWithErorrHandling(false);
} else if (data.settings) { } else if (data.settings) {
settings = data.settings; settings = data.settings;
startStartingWithErorrHandling(false); startStreamingWithErorrHandling(false);
} else if (data.description) { } else if (data.description) {
if (pc == undefined) pc = createRTCPeerConnection(); if (pc == undefined) pc = createRTCPeerConnection();
await pc.setRemoteDescription(data.description); await pc.setRemoteDescription(data.description);
if (data.description.type == "offer") { if (data.description.type == "offer") {
log("Got an offer..."); log("Got an offer...");
await pc.setLocalDescription(await pc.createAnswer()); await pc.setLocalDescription(await pc.createAnswer());
sendJson({ sendOffer();
description: pc.localDescription
});
} }
} else if (data.candidate) {
if (pc == undefined) pc = createRTCPeerConnection();
log("Adding ice candidate...");
await pc.addIceCandidate(data.candidate);
} }
}; };