diff --git a/config-sample.yaml b/config-sample.yaml index 7a765f11..fc88b9b8 100644 --- a/config-sample.yaml +++ b/config-sample.yaml @@ -39,6 +39,14 @@ rtc: # # by default LiveKit clients use Google's public STUN servers # stun_servers: # - server1 + # # optional TURN servers for clients. This isn't necessary if using embedded TURN server (see below). + # turn_servers: + # - host: myhost.com + # port: 443 + # # tls, tcp, or udp + # protocol: tls + # username: "" + # credential: "" # # allows LiveKit to monitor congestion when sending streams and automatically # # manage bandwidth utilization to avoid congestion/loss. Enabled by default # congestion_control: diff --git a/pkg/config/config.go b/pkg/config/config.go index 47d1605c..ca1370a9 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -46,14 +46,16 @@ type Config struct { } type RTCConfig struct { - UDPPort uint32 `yaml:"udp_port,omitempty"` - TCPPort uint32 `yaml:"tcp_port,omitempty"` - ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"` - ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"` - NodeIP string `yaml:"node_ip,omitempty"` - STUNServers []string `yaml:"stun_servers,omitempty"` - UseExternalIP bool `yaml:"use_external_ip"` - UseICELite bool `yaml:"use_ice_lite,omitempty"` + UDPPort uint32 `yaml:"udp_port,omitempty"` + TCPPort uint32 `yaml:"tcp_port,omitempty"` + ICEPortRangeStart uint32 `yaml:"port_range_start,omitempty"` + ICEPortRangeEnd uint32 `yaml:"port_range_end,omitempty"` + NodeIP string `yaml:"node_ip,omitempty"` + STUNServers []string `yaml:"stun_servers,omitempty"` + TURNServers []TURNServer `yaml:"turn_servers,omitempty"` + UseExternalIP bool `yaml:"use_external_ip"` + UseICELite bool `yaml:"use_ice_lite,omitempty"` + // Number of packets to buffer for NACK PacketBufferSize int `yaml:"packet_buffer_size,omitempty"` @@ -71,6 +73,14 @@ type RTCConfig struct { ForceTCP bool `yaml:"force_tcp,omitempty"` } +type TURNServer struct { + Host string `yaml:"host"` + Port int `yaml:"port"` + Protocol string `yaml:"protocol"` + Username string `yaml:"username,omitempty"` + Credential string `yaml:"credential,omitempty"` +} + type PLIThrottleConfig struct { LowQuality time.Duration `yaml:"low_quality,omitempty"` MidQuality time.Duration `yaml:"mid_quality,omitempty"` diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index d7eee25c..f47334f5 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -494,6 +494,7 @@ func (r *RoomManager) handleRTCMessage(_ context.Context, roomName livekit.RoomN func (r *RoomManager) iceServersForRoom(ri *livekit.Room) []*livekit.ICEServer { var iceServers []*livekit.ICEServer + rtcConf := r.config.RTC hasSTUN := false if r.config.TURN.Enabled { @@ -515,7 +516,28 @@ func (r *RoomManager) iceServersForRoom(ri *livekit.Room) []*livekit.ICEServer { } } - if len(r.config.RTC.STUNServers) > 0 { + if len(rtcConf.TURNServers) > 0 { + hasSTUN = true + for _, s := range r.config.RTC.TURNServers { + scheme := "turn" + transport := "tcp" + if s.Protocol == "tls" { + scheme = "turns" + } else if s.Protocol == "udp" { + transport = "udp" + } + is := &livekit.ICEServer{ + Urls: []string{ + fmt.Sprintf("%s:%s:%d?transport=%s", scheme, s.Host, s.Port, transport), + }, + Username: s.Username, + Credential: s.Credential, + } + iceServers = append(iceServers, is) + } + } + + if len(rtcConf.STUNServers) > 0 { hasSTUN = true iceServers = append(iceServers, iceServerForStunServers(r.config.RTC.STUNServers)) }