Closing the old peer connection

This commit is contained in:
Filipi Fuchter
2025-03-13 17:35:47 -03:00
parent 8f2dadf5a0
commit 8a7e305619
2 changed files with 21 additions and 13 deletions

View File

@@ -135,8 +135,12 @@ export class SmallWebRTCTransport {
// aiortc it is not working fine when just trying to restart the ice
// so in this case we are creating a new peer connection on both sides
if (recreatePeerConnection) {
//this.pc?.close()
const oldPC = this.pc
await this.startNewPeerConnection(recreatePeerConnection)
if (oldPC) {
this.log("closing old peer connection")
this.closePeerConnection(oldPC)
}
} else {
await this.negotiate();
}
@@ -356,6 +360,20 @@ export class SmallWebRTCTransport {
return constraints;
}
private closePeerConnection(pc:RTCPeerConnection) {
pc.getTransceivers().forEach((transceiver) => {
if (transceiver.stop) {
transceiver.stop();
}
});
pc.getSenders().forEach((sender) => {
sender.track?.stop();
});
pc.close();
}
stop(): void {
if (!this.pc) {
this.log("Peer connection is already closed or null.");
@@ -366,17 +384,7 @@ export class SmallWebRTCTransport {
this.dc.close();
}
this.pc.getTransceivers().forEach((transceiver) => {
if (transceiver.stop) {
transceiver.stop();
}
});
this.pc.getSenders().forEach((sender) => {
sender.track?.stop();
});
this.pc.close();
this.closePeerConnection(this.pc)
// For some reason after we close the peer connection, it is not triggering the listeners
this.pc_id = null

View File

@@ -189,7 +189,7 @@ class SmallWebRTCClient:
except asyncio.TimeoutError:
if self._webrtcConnection.is_connected():
logger.warning("Timeout: No video frame received within the specified time.")
self._webrtcConnection.ask_to_renegotiate()
# self._webrtcConnection.ask_to_renegotiate()
frame = None
except MediaStreamError:
logger.warning("Received an unexpected media stream error while reading the audio.")