2021-09-27 09:26:22 +13:00
|
|
|
package webrtc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"demodesk/neko/internal/types/codec"
|
|
|
|
"demodesk/neko/internal/webrtc/pionlog"
|
|
|
|
|
|
|
|
"github.com/pion/interceptor"
|
|
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
)
|
|
|
|
|
2021-09-27 09:59:45 +13:00
|
|
|
func (manager *WebRTCManagerCtx) newPeerConnection(codecs []codec.RTPCodec, logger zerolog.Logger) (*webrtc.PeerConnection, error) {
|
2021-09-27 09:26:22 +13:00
|
|
|
// create media engine
|
2021-09-27 09:59:45 +13:00
|
|
|
engine := &webrtc.MediaEngine{}
|
|
|
|
for _, codec := range codecs {
|
|
|
|
if err := codec.Register(engine); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 09:26:22 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// create setting engine
|
2021-09-27 09:59:45 +13:00
|
|
|
settings := webrtc.SettingEngine{
|
|
|
|
LoggerFactory: pionlog.New(logger),
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = settings.SetEphemeralUDPPortRange(manager.config.EphemeralMin, manager.config.EphemeralMax)
|
|
|
|
settings.SetICETimeouts(disconnectedTimeout, failedTimeout, keepAliveInterval)
|
|
|
|
settings.SetNAT1To1IPs(manager.config.NAT1To1IPs, webrtc.ICECandidateTypeHost)
|
|
|
|
settings.SetLite(manager.config.ICELite)
|
2021-09-27 09:26:22 +13:00
|
|
|
|
|
|
|
// create interceptor registry
|
|
|
|
registry := &interceptor.Registry{}
|
|
|
|
if err := webrtc.RegisterDefaultInterceptors(engine, registry); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new API
|
|
|
|
api := webrtc.NewAPI(
|
|
|
|
webrtc.WithMediaEngine(engine),
|
|
|
|
webrtc.WithSettingEngine(settings),
|
|
|
|
webrtc.WithInterceptorRegistry(registry),
|
|
|
|
)
|
|
|
|
|
|
|
|
// create new peer connection
|
|
|
|
configuration := manager.peerConfiguration()
|
|
|
|
return api.NewPeerConnection(configuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *WebRTCManagerCtx) peerConfiguration() webrtc.Configuration {
|
|
|
|
if manager.config.ICELite {
|
|
|
|
return webrtc.Configuration{
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ICEServers := []webrtc.ICEServer{}
|
|
|
|
for _, server := range manager.config.ICEServers {
|
|
|
|
var credential interface{}
|
|
|
|
if server.Credential != "" {
|
|
|
|
credential = server.Credential
|
|
|
|
} else {
|
|
|
|
credential = false
|
|
|
|
}
|
|
|
|
|
|
|
|
ICEServers = append(ICEServers, webrtc.ICEServer{
|
|
|
|
URLs: server.URLs,
|
|
|
|
Username: server.Username,
|
|
|
|
Credential: credential,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return webrtc.Configuration{
|
|
|
|
ICEServers: ICEServers,
|
|
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlanWithFallback,
|
|
|
|
}
|
|
|
|
}
|