From d20fa071e77cf92e848d65114c4c32268c46677a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 10 Feb 2022 23:39:52 +0100 Subject: [PATCH] register negotiation event after initial offer. --- internal/types/webrtc.go | 2 +- internal/webrtc/manager.go | 50 ++++++++++++++++++---------- internal/websocket/handler/signal.go | 7 +--- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/internal/types/webrtc.go b/internal/types/webrtc.go index 54a7c6e6..12c92973 100644 --- a/internal/types/webrtc.go +++ b/internal/types/webrtc.go @@ -38,5 +38,5 @@ type WebRTCManager interface { ICEServers() []ICEServer - CreatePeer(session Session, videoID string) (WebRTCPeer, error) + CreatePeer(session Session, videoID string) (*webrtc.SessionDescription, error) } diff --git a/internal/webrtc/manager.go b/internal/webrtc/manager.go index 5d381925..d6debfe8 100644 --- a/internal/webrtc/manager.go +++ b/internal/webrtc/manager.go @@ -117,7 +117,7 @@ func (manager *WebRTCManagerCtx) ICEServers() []types.ICEServer { return manager.config.ICEServers } -func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (types.WebRTCPeer, error) { +func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID string) (*webrtc.SessionDescription, error) { // add session id to logger context logger := manager.logger.With().Str("session_id", session.ID()).Logger() logger.Info().Msg("creating webrtc peer") @@ -295,22 +295,6 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin logger.Info().Interface("data-channel", dc).Msg("got remote data channel") }) - connection.OnNegotiationNeeded(func() { - logger.Warn().Msg("negotiation is needed") - - offer, err := peer.CreateOffer(false) - if err != nil { - logger.Err(err).Msg("sdp offer failed") - return - } - - session.Send( - event.SIGNAL_OFFER, - message.SignalDescription{ - SDP: offer.SDP, - }) - }) - connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { switch state { case webrtc.PeerConnectionStateConnected: @@ -370,5 +354,35 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin }) session.SetWebRTCPeer(peer) - return peer, nil + + offer, err := peer.CreateOffer(false) + if err != nil { + return nil, err + } + + // on negotiation needed handler must be registered after creating initial + // offer, otherwise it can fire and intercept sucessful negotiation + + connection.OnNegotiationNeeded(func() { + logger.Warn().Msg("negotiation is needed") + + if connection.SignalingState() != webrtc.SignalingStateStable { + logger.Warn().Msg("connection isn't stable yet; postponing...") + return + } + + offer, err := peer.CreateOffer(false) + if err != nil { + logger.Err(err).Msg("sdp offer failed") + return + } + + session.Send( + event.SIGNAL_OFFER, + message.SignalDescription{ + SDP: offer.SDP, + }) + }) + + return offer, nil } diff --git a/internal/websocket/handler/signal.go b/internal/websocket/handler/signal.go index 88229265..2a49fa1e 100644 --- a/internal/websocket/handler/signal.go +++ b/internal/websocket/handler/signal.go @@ -19,12 +19,7 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session, payload *messag payload.Video = videos[0] } - peer, err := h.webrtc.CreatePeer(session, payload.Video) - if err != nil { - return err - } - - offer, err := peer.CreateOffer(false) + offer, err := h.webrtc.CreatePeer(session, payload.Video) if err != nil { return err }