skip onnegotiationneeded if in progress. (#22)

This commit is contained in:
Miroslav Šedivý 2023-01-30 11:33:52 +01:00 committed by GitHub
parent dc2ef37e17
commit ae80753378
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -184,27 +184,34 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
} }
let negotiating = false
this._peer.onnegotiationneeded = async () => { this._peer.onnegotiationneeded = async () => {
if (!this._peer) { if (!this._peer) {
this._log.warn(`attempting to call 'onsignalingstatechange' for nonexistent peer`) this._log.warn(`attempting to call 'onsignalingstatechange' for nonexistent peer`)
return return
} }
try { const state = this._peer.signalingState
const offer = await this._peer.createOffer() this._log.warn(`negotiation is needed`, { state })
if (negotiating) {
this._log.info(`negotiation already in progress; skipping...`)
return
}
negotiating = true
try {
// If the connection hasn't yet achieved the "stable" state, // If the connection hasn't yet achieved the "stable" state,
// return to the caller. Another negotiationneeded event // return to the caller. Another negotiationneeded event
// will be fired when the state stabilizes. // will be fired when the state stabilizes.
const state = this._peer.signalingState
this._log.warn(`negotiation is needed`, { state })
if (state != 'stable') { if (state != 'stable') {
this._log.info(`connection isn't stable yet; postponing...`) this._log.info(`connection isn't stable yet; postponing...`)
return return
} }
const offer = await this._peer.createOffer()
await this._peer.setLocalDescription(offer) await this._peer.setLocalDescription(offer)
if (offer) { if (offer) {
@ -214,6 +221,8 @@ export class NekoWebRTC extends EventEmitter<NekoWebRTCEvents> {
} }
} catch (error: any) { } catch (error: any) {
this._log.error(`on negotiation needed failed`, { error }) this._log.error(`on negotiation needed failed`, { error })
} finally {
negotiating = false
} }
} }