Skip to content

Commit

Permalink
Clear queued candidates (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 authored Jul 23, 2024
1 parent 09f1d4e commit 31d1be6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/rtc_peer.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export declare class RTCPeer extends EventEmitter {
private onICEConnectionStateChange;
private onNegotiationNeeded;
private onTrack;
private flushICECandidates;
signal(data: string): Promise<void>;
addTrack(track: MediaStreamTrack, stream: MediaStream, opts?: RTCTrackOptions): Promise<void>;
addStream(stream: MediaStream, opts?: RTCTrackOptions[]): void;
Expand Down
22 changes: 17 additions & 5 deletions lib/rtc_peer.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,17 @@ export class RTCPeer extends EventEmitter {
}
this.emit('stream', new MediaStream([ev.track]));
}
flushICECandidates() {
var _a;
this.logger.logDebug(`RTCPeer.flushICECandidates: flushing ${this.candidates.length} candidates`);
for (const candidate of this.candidates) {
this.logger.logDebug('RTCPeer.flushICECandidates: adding queued ice candidate');
(_a = this.pc) === null || _a === void 0 ? void 0 : _a.addIceCandidate(candidate).catch((err) => {
this.logger.logErr('RTCPeer.flushICECandidates: failed to add candidate', err);
});
}
this.candidates = [];
}
signal(data) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
Expand Down Expand Up @@ -166,16 +177,16 @@ export class RTCPeer extends EventEmitter {
break;
case 'offer':
yield this.pc.setRemoteDescription(msg);
if (this.candidates.length > 0) {
this.flushICECandidates();
}
yield this.pc.setLocalDescription();
this.emit('answer', this.pc.localDescription);
break;
case 'answer':
yield this.pc.setRemoteDescription(msg);
for (const candidate of this.candidates) {
this.logger.logDebug('RTCPeer.signal: adding queued ice candidate');
this.pc.addIceCandidate(candidate).catch((err) => {
this.logger.logErr('RTCPeer.signal: failed to add candidate', err);
});
if (this.candidates.length > 0) {
this.flushICECandidates();
}
break;
default:
Expand Down Expand Up @@ -306,6 +317,7 @@ export class RTCPeer extends EventEmitter {
this.pc.close();
this.pc = null;
this.connected = false;
this.candidates = [];
clearInterval(this.pingIntervalID);
clearTimeout(this.connTimeoutID);
}
Expand Down
22 changes: 17 additions & 5 deletions src/rtc_peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ export class RTCPeer extends EventEmitter {
this.emit('stream', new MediaStream([ev.track]));
}

private flushICECandidates() {
this.logger.logDebug(`RTCPeer.flushICECandidates: flushing ${this.candidates.length} candidates`);
for (const candidate of this.candidates) {
this.logger.logDebug('RTCPeer.flushICECandidates: adding queued ice candidate');
this.pc?.addIceCandidate(candidate).catch((err) => {
this.logger.logErr('RTCPeer.flushICECandidates: failed to add candidate', err);
});
}
this.candidates = [];
}

public async signal(data: string) {
if (!this.pc) {
throw new Error('peer has been destroyed');
Expand Down Expand Up @@ -183,16 +194,16 @@ export class RTCPeer extends EventEmitter {
break;
case 'offer':
await this.pc.setRemoteDescription(msg);
if (this.candidates.length > 0) {
this.flushICECandidates();
}
await this.pc.setLocalDescription();
this.emit('answer', this.pc.localDescription);
break;
case 'answer':
await this.pc.setRemoteDescription(msg);
for (const candidate of this.candidates) {
this.logger.logDebug('RTCPeer.signal: adding queued ice candidate');
this.pc.addIceCandidate(candidate).catch((err) => {
this.logger.logErr('RTCPeer.signal: failed to add candidate', err);
});
if (this.candidates.length > 0) {
this.flushICECandidates();
}
break;
default:
Expand Down Expand Up @@ -337,6 +348,7 @@ export class RTCPeer extends EventEmitter {
this.pc.close();
this.pc = null;
this.connected = false;
this.candidates = [];
clearInterval(this.pingIntervalID);
clearTimeout(this.connTimeoutID);
}
Expand Down

0 comments on commit 31d1be6

Please sign in to comment.