From ed8fab1afaa61e1a997b865ca83988ebf10c1f39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 25 Nov 2021 23:48:41 +0100 Subject: [PATCH] do not fire onnegotiation when not stable. --- src/component/internal/webrtc.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/component/internal/webrtc.ts b/src/component/internal/webrtc.ts index be5bf9b7..1708a344 100644 --- a/src/component/internal/webrtc.ts +++ b/src/component/internal/webrtc.ts @@ -145,16 +145,26 @@ export class NekoWebRTC extends EventEmitter { } this._peer.onnegotiationneeded = async () => { - const state = this._peer!.iceConnectionState + const offer = await this._peer?.createOffer() + + // If the connection hasn't yet achieved the "stable" state, + // return to the caller. Another negotiationneeded event + // will be fired when the state stabilizes. + + const state = this._peer!.signalingState this._log.warn(`negotiation is needed`, { state }) - const offer = await this._peer?.createOffer() + if (state != 'stable') { + this._log.info(`connection isn't stable yet; postponing...`) + return + } + this._peer!.setLocalDescription(offer) if (offer) { this.emit('negotiation', offer) } else { - this._log.warn(`megotiatoion offer is empty`) + this._log.warn(`negotiatoion offer is empty`) } }