Split ICE Servers to frontend and backend (#37)

* split iceservers to frontend and backend.

* no backend ice servers when ice lite enabled.

* update config.
This commit is contained in:
Miroslav Šedivý 2023-03-27 18:34:24 +02:00 committed by GitHub
parent f8edd988b3
commit 08a5d5abb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 21 deletions

View File

@ -120,6 +120,10 @@ session:
webrtc:
icelite: true
iceservers:
# Backend servers are ignored if icelite is true.
backend:
- urls: [ stun:stun.l.google.com:19302 ]
# username: foo
# credential: bar
frontend:
- urls: [ stun:stun.l.google.com:19305 ]
#username: foo
#credential: bar

View File

@ -18,7 +18,8 @@ const defStunSrv = "stun:stun.l.google.com:19302"
type WebRTC struct {
ICELite bool
ICETrickle bool
ICEServers []types.ICEServer
ICEServersFrontend []types.ICEServer
ICEServersBackend []types.ICEServer
EphemeralMin uint16
EphemeralMax uint16
TCPMux int
@ -43,11 +44,21 @@ func (WebRTC) Init(cmd *cobra.Command) error {
return err
}
cmd.PersistentFlags().String("webrtc.iceservers", "[]", "STUN and TURN servers in JSON format with `urls`, `username`, `password` keys")
cmd.PersistentFlags().String("webrtc.iceservers", "[]", "Global STUN and TURN servers in JSON format with `urls`, `username` and `credential` keys")
if err := viper.BindPFlag("webrtc.iceservers", cmd.PersistentFlags().Lookup("webrtc.iceservers")); err != nil {
return err
}
cmd.PersistentFlags().String("webrtc.iceservers.frontend", "[]", "Frontend only STUN and TURN servers in JSON format with `urls`, `username` and `credential` keys")
if err := viper.BindPFlag("webrtc.iceservers.frontend", cmd.PersistentFlags().Lookup("webrtc.iceservers.frontend")); err != nil {
return err
}
cmd.PersistentFlags().String("webrtc.iceservers.backend", "[]", "Backend only STUN and TURN servers in JSON format with `urls`, `username` and `credential` keys")
if err := viper.BindPFlag("webrtc.iceservers.backend", cmd.PersistentFlags().Lookup("webrtc.iceservers.backend")); err != nil {
return err
}
cmd.PersistentFlags().String("webrtc.epr", "", "limits the pool of ephemeral ports that ICE UDP connections can allocate from")
if err := viper.BindPFlag("webrtc.epr", cmd.PersistentFlags().Lookup("webrtc.epr")); err != nil {
return err
@ -97,18 +108,45 @@ func (s *WebRTC) Set() {
s.ICELite = viper.GetBool("webrtc.icelite")
s.ICETrickle = viper.GetBool("webrtc.icetrickle")
if err := viper.UnmarshalKey("webrtc.iceservers", &s.ICEServers, viper.DecodeHook(
utils.JsonStringAutoDecode(s.ICEServers),
// parse frontend ice servers
if err := viper.UnmarshalKey("webrtc.iceservers.frontend", &s.ICEServersFrontend, viper.DecodeHook(
utils.JsonStringAutoDecode([]types.ICEServer{}),
)); err != nil {
log.Warn().Err(err).Msgf("unable to parse ICE servers")
log.Warn().Err(err).Msgf("unable to parse frontend ICE servers")
}
if len(s.ICEServers) == 0 {
s.ICEServers = append(s.ICEServers, types.ICEServer{
// parse backend ice servers
if err := viper.UnmarshalKey("webrtc.iceservers.backend", &s.ICEServersBackend, viper.DecodeHook(
utils.JsonStringAutoDecode([]types.ICEServer{}),
)); err != nil {
log.Warn().Err(err).Msgf("unable to parse backend ICE servers")
}
if s.ICELite && len(s.ICEServersBackend) > 0 {
log.Warn().Msgf("ICE Lite is enabled, but backend ICE servers are configured. Backend ICE servers will be ignored.")
}
// if no frontend or backend ice servers are configured
if len(s.ICEServersFrontend) == 0 && len(s.ICEServersBackend) == 0 {
// parse global ice servers
var iceServers []types.ICEServer
if err := viper.UnmarshalKey("webrtc.iceservers", &iceServers, viper.DecodeHook(
utils.JsonStringAutoDecode([]types.ICEServer{}),
)); err != nil {
log.Warn().Err(err).Msgf("unable to parse global ICE servers")
}
// add default stun server if none are configured
if len(iceServers) == 0 {
iceServers = append(iceServers, types.ICEServer{
URLs: []string{defStunSrv},
})
}
s.ICEServersFrontend = append(s.ICEServersFrontend, iceServers...)
s.ICEServersBackend = append(s.ICEServersBackend, iceServers...)
}
s.TCPMux = viper.GetInt("webrtc.tcpmux")
s.UDPMux = viper.GetInt("webrtc.udpmux")

View File

@ -47,7 +47,7 @@ func New(desktop types.DesktopManager, capture types.CaptureManager, config *con
if !config.ICELite {
ICEServers := []webrtc.ICEServer{}
for _, server := range config.ICEServers {
for _, server := range config.ICEServersBackend {
var credential any
if server.Credential != "" {
credential = server.Credential
@ -137,7 +137,8 @@ func (manager *WebRTCManagerCtx) Start() {
manager.logger.Info().
Bool("icelite", manager.config.ICELite).
Bool("icetrickle", manager.config.ICETrickle).
Interface("iceservers", manager.config.ICEServers).
Interface("iceservers-frontend", manager.config.ICEServersFrontend).
Interface("iceservers-backend", manager.config.ICEServersBackend).
Str("nat1to1", strings.Join(manager.config.NAT1To1IPs, ",")).
Str("epr", fmt.Sprintf("%d-%d", manager.config.EphemeralMin, manager.config.EphemeralMax)).
Int("tcpmux", manager.config.TCPMux).
@ -155,7 +156,7 @@ func (manager *WebRTCManagerCtx) Shutdown() error {
}
func (manager *WebRTCManagerCtx) ICEServers() []types.ICEServer {
return manager.config.ICEServers
return manager.config.ICEServersFrontend
}
func (manager *WebRTCManagerCtx) newPeerConnection(bitrate int, codecs []codec.RTPCodec, logger zerolog.Logger) (*webrtc.PeerConnection, cc.BandwidthEstimator, error) {