From 2ff47ac920a15f367b91bc8deb254b1ab9575fb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 27 Mar 2023 19:28:28 +0200 Subject: [PATCH] fix: ensure webrtc close is executed only once. --- internal/webrtc/manager.go | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index d0782de4..29fc8654 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -4,6 +4,7 @@ import ( "fmt" "net" "strings" + "sync" "sync/atomic" "time" @@ -567,6 +568,7 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int, logger.Info().Interface("data_channel", dc).Msg("got remote data channel") }) + var once sync.Once connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateConnected: @@ -575,13 +577,16 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int, webrtc.PeerConnectionStateFailed: connection.Close() case webrtc.PeerConnectionStateClosed: - session.SetWebRTCConnected(peer, false) - if err = video.RemoveReceiver(videoTrack); err != nil { - logger.Err(err).Msg("failed to remove video receiver") - } - audioTrack.Shutdown() - videoTrack.Shutdown() - close(videoRtcp) + // ensure we only run this once + once.Do(func() { + session.SetWebRTCConnected(peer, false) + if err = video.RemoveReceiver(videoTrack); err != nil { + logger.Err(err).Msg("failed to remove video receiver") + } + audioTrack.Shutdown() + videoTrack.Shutdown() + close(videoRtcp) + }) } manager.metrics.SetState(session, state)