split webrtc ConnectPeer function.

This commit is contained in:
Miroslav Šedivý 2021-02-02 19:21:48 +01:00
parent 91f1727723
commit ace7b254a3
4 changed files with 98 additions and 66 deletions

View File

@ -1,5 +1,7 @@
package types package types
import "github.com/pion/webrtc/v3"
type WebRTCPeer interface { type WebRTCPeer interface {
SignalAnswer(sdp string) error SignalAnswer(sdp string) error
Destroy() error Destroy() error
@ -8,5 +10,9 @@ type WebRTCPeer interface {
type WebRTCManager interface { type WebRTCManager interface {
Start() Start()
Shutdown() error Shutdown() error
CreatePeer(session Session) (string, bool, []string, error)
ICELite() bool
ICEServers() []string
CreatePeer(session Session) (*webrtc.SessionDescription, error)
} }

View File

@ -77,84 +77,48 @@ func (manager *WebRTCManagerCtx) Shutdown() error {
return nil return nil
} }
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool, []string, error) { func (manager *WebRTCManagerCtx) ICELite() bool {
configuration := &webrtc.Configuration{ return manager.config.ICELite
ICEServers: []webrtc.ICEServer{ }
{
URLs: manager.config.ICEServers,
},
},
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
settings := webrtc.SettingEngine{ func (manager *WebRTCManagerCtx) ICEServers() []string {
LoggerFactory: loggerFactory{ return manager.config.ICEServers
logger: manager.logger.With().Str("id", session.ID()).Logger(), }
},
}
if manager.config.ICELite {
configuration = &webrtc.Configuration{
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
settings.SetLite(true)
}
err := settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (*webrtc.SessionDescription, error) {
// Create MediaEngine // Create MediaEngine
engine := &webrtc.MediaEngine{} engine, err := manager.mediaEngine()
if err != nil {
if err := manager.videoCodec.Register(engine); err != nil { return nil, err
return "", manager.config.ICELite, manager.config.ICEServers, err
} }
if err := manager.audioCodec.Register(engine); err != nil { // Custom settings & configuration
return "", manager.config.ICELite, manager.config.ICEServers, err settings := manager.apiSettings(session)
} configuration := manager.apiConfiguration()
// Create NewAPI with MediaEngine and SettingEngine // Create NewAPI with MediaEngine and SettingEngine
api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(settings)) api := webrtc.NewAPI(webrtc.WithMediaEngine(engine), webrtc.WithSettingEngine(*settings))
// Create NewPeerConnection // Create NewPeerConnection
connection, err := api.NewPeerConnection(*configuration) connection, err := api.NewPeerConnection(*configuration)
if err != nil { if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return nil, err
} }
_, err = connection.CreateDataChannel("data", nil) // Register video, audio & data tracks
if err != nil { manager.registerTracks(connection)
return "", manager.config.ICELite, manager.config.ICEServers, err
}
if _, err = connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
if _, err = connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err
}
// Create Offer
offer, err := connection.CreateOffer(nil) offer, err := connection.CreateOffer(nil)
if err != nil { if err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return nil, err
} }
// TODO: Refactor, send request to client. // TODO: Refactor, send request to client.
gatherComplete := webrtc.GatheringCompletePromise(connection) gatherComplete := webrtc.GatheringCompletePromise(connection)
if err := connection.SetLocalDescription(offer); err != nil { if err := connection.SetLocalDescription(offer); err != nil {
return "", manager.config.ICELite, manager.config.ICEServers, err return nil, err
} }
<-gatherComplete <-gatherComplete
@ -187,10 +151,74 @@ func (manager *WebRTCManagerCtx) CreatePeer(session types.Session) (string, bool
session.SetWebRTCPeer(&WebRTCPeerCtx{ session.SetWebRTCPeer(&WebRTCPeerCtx{
api: api, api: api,
engine: engine, engine: engine,
settings: &settings, settings: settings,
connection: connection, connection: connection,
configuration: configuration, configuration: configuration,
}) })
return connection.LocalDescription().SDP, manager.config.ICELite, manager.config.ICEServers, nil return connection.LocalDescription(), nil
}
func (manager *WebRTCManagerCtx) mediaEngine() (*webrtc.MediaEngine, error) {
// Create MediaEngine
engine := &webrtc.MediaEngine{}
if err := manager.videoCodec.Register(engine); err != nil {
return nil, err
}
if err := manager.audioCodec.Register(engine); err != nil {
return nil, err
}
return engine, nil
}
func (manager *WebRTCManagerCtx) apiSettings(session types.Session) *webrtc.SettingEngine {
settings := &webrtc.SettingEngine{
LoggerFactory: loggerFactory{
logger: manager.logger.With().Str("id", session.ID()).Logger(),
},
}
//nolint
settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
settings.SetLite(manager.config.ICELite)
return settings
}
func (manager *WebRTCManagerCtx) apiConfiguration() *webrtc.Configuration {
if manager.config.ICELite {
return &webrtc.Configuration{
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
}
return &webrtc.Configuration{
ICEServers: []webrtc.ICEServer{
{
URLs: manager.config.ICEServers,
},
},
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
}
}
func (manager *WebRTCManagerCtx) registerTracks(connection *webrtc.PeerConnection) error {
if _, err := connection.AddTransceiverFromTrack(manager.videoTrack, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return err
}
if _, err := connection.AddTransceiverFromTrack(manager.audioTrack, webrtc.RtpTransceiverInit{
Direction: webrtc.RTPTransceiverDirectionSendonly,
}); err != nil {
return err
}
_, err := connection.CreateDataChannel("data", nil)
return err
} }

View File

@ -1,8 +1,6 @@
package webrtc package webrtc
import ( import "github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3"
)
type WebRTCPeerCtx struct { type WebRTCPeerCtx struct {
api *webrtc.API api *webrtc.API

View File

@ -11,7 +11,7 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error {
return nil return nil
} }
sdp, lite, ice, err := h.webrtc.CreatePeer(session) offer, err := h.webrtc.CreatePeer(session)
if err != nil { if err != nil {
return err return err
} }
@ -19,9 +19,9 @@ func (h *MessageHandlerCtx) signalRequest(session types.Session) error {
return session.Send( return session.Send(
message.SignalProvide{ message.SignalProvide{
Event: event.SIGNAL_PROVIDE, Event: event.SIGNAL_PROVIDE,
SDP: sdp, SDP: offer.SDP,
Lite: lite, Lite: h.webrtc.ICELite(),
ICE: ice, ICE: h.webrtc.ICEServers(),
}) })
} }