peer webrtc connection check.

This commit is contained in:
Miroslav Šedivý 2022-02-10 00:12:30 +01:00
parent c83883fd1e
commit 369d8f3ccf
2 changed files with 27 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import (
var ( var (
ErrWebRTCVideoNotFound = errors.New("webrtc video not found") ErrWebRTCVideoNotFound = errors.New("webrtc video not found")
ErrWebRTCDataChannelNotFound = errors.New("webrtc data channel not found") ErrWebRTCDataChannelNotFound = errors.New("webrtc data channel not found")
ErrWebRTCConnectionNotFound = errors.New("webrtc connection not found")
) )
type ICEServer struct { type ICEServer struct {

View File

@ -5,6 +5,8 @@ import (
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"github.com/rs/zerolog" "github.com/rs/zerolog"
"demodesk/neko/internal/types"
) )
type WebRTCPeerCtx struct { type WebRTCPeerCtx struct {
@ -20,6 +22,10 @@ func (peer *WebRTCPeerCtx) CreateOffer(ICERestart bool) (*webrtc.SessionDescript
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return nil, types.ErrWebRTCConnectionNotFound
}
offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{ offer, err := peer.connection.CreateOffer(&webrtc.OfferOptions{
ICERestart: ICERestart, ICERestart: ICERestart,
}) })
@ -34,6 +40,10 @@ func (peer *WebRTCPeerCtx) CreateAnswer() (*webrtc.SessionDescription, error) {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return nil, types.ErrWebRTCConnectionNotFound
}
answer, err := peer.connection.CreateAnswer(nil) answer, err := peer.connection.CreateAnswer(nil)
if err != nil { if err != nil {
return nil, err return nil, err
@ -65,6 +75,10 @@ func (peer *WebRTCPeerCtx) SetOffer(sdp string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{ return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp, SDP: sdp,
Type: webrtc.SDPTypeOffer, Type: webrtc.SDPTypeOffer,
@ -75,6 +89,10 @@ func (peer *WebRTCPeerCtx) SetAnswer(sdp string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.SetRemoteDescription(webrtc.SessionDescription{ return peer.connection.SetRemoteDescription(webrtc.SessionDescription{
SDP: sdp, SDP: sdp,
Type: webrtc.SDPTypeAnswer, Type: webrtc.SDPTypeAnswer,
@ -85,6 +103,10 @@ func (peer *WebRTCPeerCtx) SetCandidate(candidate webrtc.ICECandidateInit) error
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
return peer.connection.AddICECandidate(candidate) return peer.connection.AddICECandidate(candidate)
} }
@ -92,6 +114,10 @@ func (peer *WebRTCPeerCtx) SetVideoID(videoID string) error {
peer.mu.Lock() peer.mu.Lock()
defer peer.mu.Unlock() defer peer.mu.Unlock()
if peer.connection == nil {
return types.ErrWebRTCConnectionNotFound
}
peer.logger.Info().Str("video_id", videoID).Msg("change video id") peer.logger.Info().Str("video_id", videoID).Msg("change video id")
return peer.changeVideo(videoID) return peer.changeVideo(videoID)
} }