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 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 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 // add session id to logger context
logger := manager.logger.With().Str("session_id", session.ID()).Logger() logger := manager.logger.With().Str("session_id", session.ID()).Logger()
logger.Info().Msg("creating webrtc peer") 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") 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) { connection.OnConnectionStateChange(func(state webrtc.PeerConnectionState) {
switch state { switch state {
case webrtc.PeerConnectionStateConnected: case webrtc.PeerConnectionStateConnected:
@ -370,5 +354,35 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session, videoID strin
}) })
session.SetWebRTCPeer(peer) 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] payload.Video = videos[0]
} }
peer, err := h.webrtc.CreatePeer(session, payload.Video) offer, err := h.webrtc.CreatePeer(session, payload.Video)
if err != nil {
return err
}
offer, err := peer.CreateOffer(false)
if err != nil { if err != nil {
return err return err
} }