add mutex to webrtc peer.

This commit is contained in:
Miroslav Šedivý 2021-06-30 00:04:41 +02:00
parent c2277c4917
commit a852eddfb7

View File

@ -1,8 +1,13 @@
package webrtc package webrtc
import "github.com/pion/webrtc/v3" import (
"sync"
"github.com/pion/webrtc/v3"
)
type WebRTCPeerCtx struct { type WebRTCPeerCtx struct {
mu sync.Mutex
api *webrtc.API api *webrtc.API
connection *webrtc.PeerConnection connection *webrtc.PeerConnection
dataChannel *webrtc.DataChannel dataChannel *webrtc.DataChannel
@ -11,6 +16,9 @@ type WebRTCPeerCtx struct {
} }
func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) { func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescription, error) {
peer.mu.Lock()
defer peer.mu.Unlock()
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{ offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
ICERestart: ICERestart, ICERestart: ICERestart,
}) })
@ -48,10 +56,16 @@ func (peer *WebRTCPeerCtx) SignalCandidate(candidate webrtc.ICECandidateInit) er
} }
func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error { func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
peer.mu.Lock()
defer peer.mu.Unlock()
return peer.changeVideo(videoID) return peer.changeVideo(videoID)
} }
func (peer *WebRTCPeerCtx) Destroy() error { func (peer *WebRTCPeerCtx) Destroy() error {
peer.mu.Lock()
defer peer.mu.Unlock()
if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected { if peer.connection == nil || peer.connection.ConnectionState() != webrtc.PeerConnectionStateConnected {
return nil return nil
} }