From 4a9ae3cec34552089bee7383aca0dafcf68ab10e Mon Sep 17 00:00:00 2001 From: sebvanleuven Date: Sat, 25 Jul 2026 01:09:46 +0100 Subject: [PATCH 1/2] pre-gather ICE candidates during wss connection setup --- src/modules/StreamingClient.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/modules/StreamingClient.ts b/src/modules/StreamingClient.ts index 403cb48..49a55b3 100644 --- a/src/modules/StreamingClient.ts +++ b/src/modules/StreamingClient.ts @@ -495,7 +495,20 @@ export class StreamingClient { } this.resetAttemptScopedMilestoneState(); this.connectionMilestones?.record('connection_start_requested'); - // start the connection + // Pre-gather ICE candidates in parallel with the WebSocket handshake. + // Creating the peer connection and local offer now starts ICE gathering + // (including any TURN allocation) immediately, instead of waiting for the + // socket to open. The offer and trickled candidates are queued by the + // SignallingClient's sending buffer and flushed, in order, once the + // socket opens - so the connection can proceed as soon as signalling is up. + void this.initPeerConnectionAndSendOffer().catch((error) => { + console.error( + 'StreamingClient - startConnection: error preparing peer connection', + error, + ); + this.handleWebrtcFailure(error); + }); + // Establish the signalling channel concurrently with ICE gathering. this.signallingClient.connect(); } catch (error) { console.error('StreamingClient - startConnection: error', error); From 034efdf3c08728430ff2a50ba10867a2b3d67221 Mon Sep 17 00:00:00 2001 From: sebvanleuven Date: Sat, 25 Jul 2026 01:40:54 +0100 Subject: [PATCH 2/2] fix race --- src/modules/StreamingClient.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/modules/StreamingClient.ts b/src/modules/StreamingClient.ts index 49a55b3..25413fd 100644 --- a/src/modules/StreamingClient.ts +++ b/src/modules/StreamingClient.ts @@ -506,7 +506,10 @@ export class StreamingClient { 'StreamingClient - startConnection: error preparing peer connection', error, ); - this.handleWebrtcFailure(error); + // If the connection was torn down while the pre-gather task was still + // in flight, shutdown() has already set iceRestartStopped and the + // rejection is expected - don't surface a spurious WebRTC failure. + if (!this.iceRestartStopped) this.handleWebrtcFailure(error); }); // Establish the signalling channel concurrently with ICE gathering. this.signallingClient.connect();