From a852eddfb7203fe100ed4cbb27018f9560509b0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Wed, 30 Jun 2021 00:04:41 +0200 Subject: [PATCH] add mutex to webrtc peer. --- internal/webrtc/peer.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/webrtc/peer.go b/internal/webrtc/peer.go index ea51ec40..dc212819 100644 --- a/internal/webrtc/peer.go +++ b/internal/webrtc/peer.go @@ -1,8 +1,13 @@ package webrtc -import "github.com/pion/webrtc/v3" +import ( + "sync" + + "github.com/pion/webrtc/v3" +) type WebRTCPeerCtx struct { + mu sync.Mutex api *webrtc.API connection *webrtc.PeerConnection dataChannel *webrtc.DataChannel @@ -11,6 +16,9 @@ type WebRTCPeerCtx struct { } func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) { + peer.mu.Lock() + defer peer.mu.Unlock() + offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{ ICERestart: ICERestart, }) @@ -48,10 +56,16 @@ func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) er } func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error { + peer.mu.Lock() + defer peer.mu.Unlock() + return peer.changeVideo(videoID) } func (peer *WebRTCPeerCtx) Destroy() error { + peer.mu.Lock() + defer peer.mu.Unlock() + if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { return nil }