Skip to content

Commit

Permalink
fix peer connection leak
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasIO committed Nov 6, 2023
1 parent 7ba3e85 commit 62cd815
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
31 changes: 19 additions & 12 deletions src/room/PCTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -334,51 +335,57 @@ 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) {
this.trackBitrates.push(info);
}

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<string | undefined> {
Expand Down
2 changes: 0 additions & 2 deletions src/room/PCTransportManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 62cd815

Please sign in to comment.