Skip to content

Commit 6f0f471

Browse files
Fix safari ice candidate behavior
1 parent f5b0e54 commit 6f0f471

File tree

3 files changed

+38
-16
lines changed

3 files changed

+38
-16
lines changed

src/public/scripts/main.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ function connect(asHost) {
7777
// Forward websocket signalling messages to the connection
7878
webSocket.onopen = (e) => {
7979
connection.create(asHost);
80+
if (!asHost) {
81+
// Request any host info
82+
webSocket.send("request");
83+
}
8084
};
8185
webSocket.onclose = (e) => {
8286
};

src/public/scripts/webConnection.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ class WebConnection extends EventTarget {
5353

5454
// Add tracks to the local video element
5555
this.#pc.ontrack = (e) => {
56-
this.#log("track!");
5756
this.#stream = new MediaStream();
5857
this.#stream.addTrack(e.track);
5958

@@ -69,31 +68,32 @@ class WebConnection extends EventTarget {
6968
// Note: We don't actually do this anymore
7069
// this.#createDataChannelForCanvas();
7170

72-
// Offer the connection
73-
const sdpConstraints = {
74-
offerToReceiveAudio: false,
75-
offerToReceiveVideo: true
76-
};
77-
const sdp = await this.#pc.createOffer(sdpConstraints);
78-
await this.#pc.setLocalDescription(sdp);
79-
this.#sendToWebSocket("offer", sdp);
71+
// Offer the connection as a host
72+
// A client joining will just recieve the offer via the cache in the server
73+
// TODO: Support multiple clients
74+
if (this.#isHost) {
75+
const sdpConstraints = {
76+
offerToReceiveAudio: false,
77+
offerToReceiveVideo: true
78+
};
79+
const sdp = await this.#pc.createOffer(sdpConstraints);
80+
await this.#pc.setLocalDescription(sdp);
81+
this.#sendToWebSocket("offer", sdp);
82+
}
8083
}
8184

8285
async onOffer(from, offer) {
83-
this.#log("onOffer");
8486
await this.#pc.setRemoteDescription(new RTCSessionDescription(offer));
8587
const sdp = await this.#pc.createAnswer();
8688
await this.#pc.setLocalDescription(sdp);
8789
this.#sendToWebSocket("answer", sdp);
8890
}
8991

9092
async onIceCandidate(from, candidate) {
91-
this.#log("onIceCandidate");
9293
await this.#pc.addIceCandidate(new RTCIceCandidate(candidate));
9394
}
9495

9596
async onAnswer(from, answer) {
96-
this.#log("onAnswer");
9797
await this.#pc.setRemoteDescription(new RTCSessionDescription(answer));
9898
}
9999

src/server.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function sendMJpeg(msg) {
4040

4141
// Create the websocket signaling server
4242
const wsList = [];
43+
const wsMessages = [];
4344
const wss = new WebSocketServer({ port: (port + 1) });
4445
wss.on("connection", function (ws) {
4546
wsList.push(ws);
@@ -48,20 +49,37 @@ wss.on("connection", function (ws) {
4849
wsList.splice(wsList.indexOf(ws), 1);
4950

5051
// On a disconnect, clear out the canvas
52+
// We have to do this twice because some browsers (OBS) seem to cache the frames
53+
sendMJpeg(emptyImage);
5154
sendMJpeg(emptyImage);
5255
});
5356

5457
ws.on("message", function (message) {
5558
const msg = message.toString();
56-
59+
5760
if (msg[0] === "d") {
61+
// Update the mjpeg
5862
sendMJpeg(msg);
5963
} else if (msg[0] === "l") {
64+
// Log the message
6065
console.log(msg);
66+
} else if (msg[0] === "r") {
67+
// On a request for host info, just fire any cached messages
68+
while (wsMessages.length > 0) {
69+
const msg = wsMessages.pop();
70+
for (var i = 0; i < wsList.length; i++) {
71+
wsList[i].send(msg);
72+
}
73+
}
6174
} else {
62-
console.log(msg);
63-
for (var i = 0; i < wsList.length; i++) {
64-
wsList[i].send(msg);
75+
if (wsList.length < 2) {
76+
// No client, so cache the messages
77+
wsMessages.push(msg);
78+
} else {
79+
// Broadcast messages
80+
for (var i = 0; i < wsList.length; i++) {
81+
wsList[i].send(msg);
82+
}
6583
}
6684
}
6785
});

0 commit comments

Comments
 (0)