From 62cd8157716a09589f316b049da217263c5a4ee1 Mon Sep 17 00:00:00 2001 From: lukasIO Date: Mon, 6 Nov 2023 16:40:22 +0200 Subject: [PATCH] fix peer connection leak --- src/room/PCTransport.ts | 31 +++++++++++++++++++------------ src/room/PCTransportManager.ts | 2 -- 2 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/room/PCTransport.ts b/src/room/PCTransport.ts index e406fc3de9..bb0e0d745f 100644 --- a/src/room/PCTransport.ts +++ b/src/room/PCTransport.ts @@ -3,7 +3,7 @@ import type { MediaDescription } from 'sdp-transform'; import { parse, write } from 'sdp-transform'; import { debounce } from 'ts-debounce'; import log from '../logger'; -import { NegotiationError } from './errors'; +import { NegotiationError, UnexpectedConnectionState } from './errors'; import { ddExtensionURI, isChromiumBased, isSVCCodec } from './utils'; /** @internal */ @@ -34,6 +34,7 @@ export default class PCTransport extends EventEmitter { private get pc() { if (!this._pc) { + console.warn('creating new peer connection'); this._pc = this.createPC(); // FIXME this seems to leak peer connections } return this._pc; @@ -334,7 +335,10 @@ export default class PCTransport extends EventEmitter { } addTrack(track: MediaStreamTrack) { - return this.pc.addTrack(track); + if (!this._pc) { + throw new UnexpectedConnectionState('PC closed, cannot add track'); + } + return this._pc.addTrack(track); } setTrackCodecBitrate(info: TrackBitrateInfo) { @@ -342,43 +346,46 @@ export default class PCTransport extends EventEmitter { } setConfiguration(rtcConfig: RTCConfiguration) { - return this.pc.setConfiguration(rtcConfig); + if (!this._pc) { + throw new UnexpectedConnectionState('PC closed, cannot configure'); + } + return this._pc?.setConfiguration(rtcConfig); } canRemoveTrack(): boolean { - return !!this.pc.removeTrack; + return !!this._pc?.removeTrack; } removeTrack(sender: RTCRtpSender) { - return this.pc.removeTrack(sender); + return this._pc?.removeTrack(sender); } getConnectionState() { - return this.pc.connectionState; + return this._pc?.connectionState ?? 'closed'; } getICEConnectionState() { - return this.pc.iceConnectionState; + return this._pc?.iceConnectionState ?? 'closed'; } getSignallingState() { - return this.pc.signalingState; + return this._pc?.signalingState ?? 'closed'; } getTransceivers() { - return this.pc.getTransceivers(); + return this._pc?.getTransceivers() ?? []; } getSenders() { - return this.pc.getSenders(); + return this._pc?.getSenders() ?? []; } getLocalDescription() { - return this.pc.localDescription; + return this._pc?.localDescription; } getRemoteDescription() { - return this.pc.remoteDescription; + return this.pc?.remoteDescription; } async getConnectedAddress(): Promise { diff --git a/src/room/PCTransportManager.ts b/src/room/PCTransportManager.ts index 32680a0f87..3c2c8dba20 100644 --- a/src/room/PCTransportManager.ts +++ b/src/room/PCTransportManager.ts @@ -209,12 +209,10 @@ export class PCTransportManager { abortController.signal.addEventListener('abort', abortHandler); this.publisher.once(PCEvents.NegotiationStarted, () => { - console.log('negotiation started'); if (abortController.signal.aborted) { return; } this.publisher.once(PCEvents.NegotiationComplete, () => { - console.log('negotiation complete'); clearTimeout(negotiationTimeout); resolve(); });