fix: ensure webrtc close is executed only once.

This commit is contained in:
Miroslav Šedivý 2023-03-27 19:28:28 +02:00
parent 08a5d5abb4
commit 2ff47ac920

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"net" "net"
"strings" "strings"
"sync"
"sync/atomic" "sync/atomic"
"time" "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") logger.Info().Interface("data_channel", dc).Msg("got remote data channel")
}) })
var once sync.Once
connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
@ -575,13 +577,16 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, bitrate int,
webrtc.PeerConnectionStateFailed: webrtc.PeerConnectionStateFailed:
connection.Close() connection.Close()
case webrtc.PeerConnectionStateClosed: case webrtc.PeerConnectionStateClosed:
session.SetWebRTCConnected(peer, false) // ensure we only run this once
if err = video.RemoveReceiver(videoTrack); err != nil { once.Do(func() {
logger.Err(err).Msg("failed to remove video receiver") session.SetWebRTCConnected(peer, false)
} if err = video.RemoveReceiver(videoTrack); err != nil {
audioTrack.Shutdown() logger.Err(err).Msg("failed to remove video receiver")
videoTrack.Shutdown() }
close(videoRtcp) audioTrack.Shutdown()
videoTrack.Shutdown()
close(videoRtcp)
})
} }
manager.metrics.SetState(session, state) manager.metrics.SetState(session, state)