Compare commits
No commits in common. "c4cddaaefd188ed8e765bbb13e2b074b366029fd" and "5669593cb1d5327c7524e54ea5c104465db9d525" have entirely different histories.
c4cddaaefd
...
5669593cb1
|
@ -205,45 +205,25 @@ form label {
|
||||||
|
|
||||||
function sendJson(data) {
|
function sendJson(data) {
|
||||||
const toSend = JSON.stringify(data);
|
const toSend = JSON.stringify(data);
|
||||||
const dcReady = dc && dc.readyState == "open";
|
log("Sending message...");
|
||||||
const method = dcReady ? "dataConnection" : "webSocket";
|
|
||||||
log("Sending message via " + method + "...");
|
|
||||||
logPre(toSend);
|
logPre(toSend);
|
||||||
(dcReady ? dc : webSocket).send(toSend);
|
webSocket.send(toSend);
|
||||||
}
|
}
|
||||||
|
|
||||||
var pc = undefined;
|
var pc = 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.");
|
||||||
|
|
||||||
pc.onsignalingstatechange = e => {
|
pc.onicecandidate = ({candidate}) => sendJson({candidate});
|
||||||
log("pc.onsignalingstatechange: " + pc.signalingState);
|
|
||||||
}
|
|
||||||
pc.oniceconnectionstatechange = e => {
|
|
||||||
log("pc.oniceconnectionstatechange: " + pc.iceConnectionState);
|
|
||||||
}
|
|
||||||
pc.onicegatheringstatechange = async function(e) {
|
|
||||||
log("pc.onicegatheringstatechange: " + pc.iceGatheringState);
|
|
||||||
sendOffer();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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());
|
||||||
sendOffer();
|
sendJson({
|
||||||
|
description: pc.localDescription
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
pc.ontrack = ({streams: [stream]}) => {
|
pc.ontrack = ({streams: [stream]}) => {
|
||||||
|
@ -262,33 +242,11 @@ form label {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
pc.ondatachannel = e => {
|
|
||||||
dc = e.channel;
|
|
||||||
dc.onmessage = e => {
|
|
||||||
receiveMessage({source: "dataChannel", data: e.data});
|
|
||||||
}
|
|
||||||
log('Data channel initialized.');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isHost) {
|
|
||||||
dc = pc.createDataChannel('data');
|
|
||||||
dc.onmessage = e => {
|
|
||||||
receiveMessage({source: "dataChannel", data: e.data});
|
|
||||||
}
|
|
||||||
dc.onopen = e => {
|
|
||||||
log("Data channel open, sending settings...")
|
|
||||||
settings = readSettingsForm(true);
|
|
||||||
sendJson({settings});
|
|
||||||
startStreamingWithErorrHandling(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pc;
|
return pc;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get a local stream, show it in a self-view and add it to be sent
|
// get a local stream, show it in a self-view and add it to be sent
|
||||||
async function startStreaming(fromButton) {
|
async function startStreaming(fromButton) {
|
||||||
log('In startStreaming(fromButton=' + fromButton + ')...');
|
|
||||||
const otherAudioSettings = isHost
|
const otherAudioSettings = isHost
|
||||||
? settings['client-audio']
|
? settings['client-audio']
|
||||||
: settings['host-audio'];
|
: settings['host-audio'];
|
||||||
|
@ -311,6 +269,15 @@ form label {
|
||||||
}
|
}
|
||||||
start.style.display = 'none';
|
start.style.display = 'none';
|
||||||
|
|
||||||
|
if (isHost) {
|
||||||
|
sendJson({
|
||||||
|
settings: settings
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pc !== undefined) return;
|
||||||
|
pc = createRTCPeerConnection();
|
||||||
|
|
||||||
const videoConstraints = videoSettings == 'none'
|
const videoConstraints = videoSettings == 'none'
|
||||||
? false
|
? false
|
||||||
: videoSettings == 'true'
|
: videoSettings == 'true'
|
||||||
|
@ -354,23 +321,27 @@ form label {
|
||||||
|
|
||||||
async function receiveMessage(e) {
|
async function receiveMessage(e) {
|
||||||
qrcode.style.display = 'none';
|
qrcode.style.display = 'none';
|
||||||
log("In receiveMessage from " + e.source + "...");
|
log("In webSocket.onmessage...");
|
||||||
logPre(e.data);
|
logPre(e.data);
|
||||||
const data = JSON.parse(e.data);
|
const data = JSON.parse(e.data);
|
||||||
if (data.ready) {
|
if (data.requestSettings) {
|
||||||
// Ready message means client is open and ready for connection.
|
settings = readSettingsForm(true);
|
||||||
pc = createRTCPeerConnection();
|
startStreamingWithErorrHandling(false);
|
||||||
} else if (data.settings) {
|
} else if (data.settings) {
|
||||||
settings = data.settings;
|
settings = data.settings;
|
||||||
startStreamingWithErorrHandling(false);
|
startStreamingWithErorrHandling(false);
|
||||||
} else if (data.description) {
|
} else if (data.description) {
|
||||||
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());
|
||||||
sendOffer();
|
sendJson({
|
||||||
|
description: pc.localDescription
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
} else if (data.candidate) {
|
||||||
|
log("Adding ice candidate...");
|
||||||
|
await pc.addIceCandidate(data.candidate);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -391,9 +362,7 @@ form label {
|
||||||
log('WebSocket error: ' + e);
|
log('WebSocket error: ' + e);
|
||||||
};
|
};
|
||||||
|
|
||||||
webSocket.onmessage = e => {
|
webSocket.onmessage = receiveMessage;
|
||||||
receiveMessage({source: "webSocket", data: e.data});
|
|
||||||
}
|
|
||||||
|
|
||||||
return webSocket;
|
return webSocket;
|
||||||
}
|
}
|
||||||
|
@ -401,10 +370,7 @@ form label {
|
||||||
webSocket = createWebSocket();
|
webSocket = createWebSocket();
|
||||||
|
|
||||||
if (!isHost) {
|
if (!isHost) {
|
||||||
// To make serverless and server mode more similar,
|
webSocket.onopen = _ => sendJson({requestSettings: true});
|
||||||
// always make the first RTCPeerConnection offer from the host,
|
|
||||||
// so here just notify the host to start the process.
|
|
||||||
webSocket.onopen = _ => sendJson({ready: true});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log("Finished <script> block.");
|
log("Finished <script> block.");
|
||||||
|
|
Loading…
Reference in New Issue
Block a user