register negotiation event after initial offer.

This commit is contained in:
Miroslav Šedivý 2022-02-10 23:39:52 +01:00
parent 369d8f3ccf
commit d20fa071e7
3 changed files with 34 additions and 25 deletions

View File

@ -38,5 +38,5 @@ type WebRTCManager interface {
ICEServers() []ICEServer
CreatePeer(session Session, videoID string) (WebRTCPeer, error)
CreatePeer(session Session, videoID string) (*webrtc.SessionDescription, error)
}

View File

@ -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
}

View File

@ -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
}