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 (
ErrWebRTCVideoNotFound = errors.New("webrtc video not found")
ErrWebRTCDataChannelNotFound = errors.New("webrtc data channel not found")
ErrWebRTCConnectionNotFound = errors.New("webrtc connection not found")
)
type ICEServer struct {

View File

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