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: webrtc:
icelite: true icelite: true
iceservers: iceservers:
- urls: [ stun:stun.l.google.com:19302 ] # Backend servers are ignored if icelite is true.
# username: foo backend:
# credential: bar - urls: [ stun:stun.l.google.com:19302 ]
frontend:
- urls: [ stun:stun.l.google.com:19305 ]
#username: foo
#credential: bar

View File

@ -16,13 +16,14 @@ import (
const defStunSrv = "stun:stun.l.google.com:19302" const defStunSrv = "stun:stun.l.google.com:19302"
type WebRTC struct { type WebRTC struct {
ICELite bool ICELite bool
ICETrickle bool ICETrickle bool
ICEServers []types.ICEServer ICEServersFrontend []types.ICEServer
EphemeralMin uint16 ICEServersBackend []types.ICEServer
EphemeralMax uint16 EphemeralMin uint16
TCPMux int EphemeralMax uint16
UDPMux int TCPMux int
UDPMux int
NAT1To1IPs []string NAT1To1IPs []string
IpRetrievalUrl string IpRetrievalUrl string
@ -43,11 +44,21 @@ func (WebRTC) Init(cmd *cobra.Command) error {
return err 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 { if err := viper.BindPFlag("webrtc.iceservers", cmd.PersistentFlags().Lookup("webrtc.iceservers")); err != nil {
return err 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") 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 { if err := viper.BindPFlag("webrtc.epr", cmd.PersistentFlags().Lookup("webrtc.epr")); err != nil {
return err return err
@ -97,16 +108,43 @@ func (s *WebRTC) Set() {
s.ICELite = viper.GetBool("webrtc.icelite") s.ICELite = viper.GetBool("webrtc.icelite")
s.ICETrickle = viper.GetBool("webrtc.icetrickle") s.ICETrickle = viper.GetBool("webrtc.icetrickle")
if err := viper.UnmarshalKey("webrtc.iceservers", &s.ICEServers, viper.DecodeHook( // parse frontend ice servers
utils.JsonStringAutoDecode(s.ICEServers), if err := viper.UnmarshalKey("webrtc.iceservers.frontend", &s.ICEServersFrontend, viper.DecodeHook(
utils.JsonStringAutoDecode([]types.ICEServer{}),
)); err != nil { )); 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 { // parse backend ice servers
s.ICEServers = append(s.ICEServers, types.ICEServer{ if err := viper.UnmarshalKey("webrtc.iceservers.backend", &s.ICEServersBackend, viper.DecodeHook(
URLs: []string{defStunSrv}, 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.TCPMux = viper.GetInt("webrtc.tcpmux")

View File

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