mirror of
https://github.com/livekit/livekit.git
synced 2026-04-05 05:55:45 +00:00
80 lines
1.6 KiB
Go
80 lines
1.6 KiB
Go
package rtc
|
|
|
|
import (
|
|
"fmt"
|
|
"net/url"
|
|
|
|
"github.com/pion/sdp/v3"
|
|
"github.com/pion/webrtc/v3"
|
|
|
|
"github.com/livekit/livekit-server/pkg/config"
|
|
)
|
|
|
|
type WebRTCConfig struct {
|
|
Configuration webrtc.Configuration
|
|
SettingEngine webrtc.SettingEngine
|
|
feedbackTypes []webrtc.RTCPFeedback
|
|
|
|
receiver ReceiverConfig
|
|
}
|
|
|
|
type ReceiverConfig struct {
|
|
maxBandwidth uint64
|
|
maxBufferTime int
|
|
}
|
|
|
|
func NewWebRTCConfig(conf *config.RTCConfig, externalIP string) (*WebRTCConfig, error) {
|
|
c := webrtc.Configuration{
|
|
SDPSemantics: webrtc.SDPSemanticsUnifiedPlan,
|
|
}
|
|
s := webrtc.SettingEngine{}
|
|
ft := []webrtc.RTCPFeedback{
|
|
{Type: webrtc.TypeRTCPFBCCM},
|
|
{Type: webrtc.TypeRTCPFBNACK},
|
|
{Type: webrtc.TypeRTCPFBGoogREMB},
|
|
{Type: "nack pli"},
|
|
}
|
|
|
|
if conf.ICEPortRangeStart != 0 && conf.ICEPortRangeEnd != 0 {
|
|
if err := s.SetEphemeralUDPPortRange(conf.ICEPortRangeStart, conf.ICEPortRangeEnd); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
iceUrls := make([]string, 0)
|
|
for _, stunServer := range conf.StunServers {
|
|
iceUrls = append(iceUrls, fmt.Sprintf("stun:%s", stunServer))
|
|
}
|
|
c.ICEServers = []webrtc.ICEServer{
|
|
{
|
|
URLs: iceUrls,
|
|
},
|
|
}
|
|
if conf.UseExternalIP {
|
|
s.SetNAT1To1IPs([]string{externalIP}, webrtc.ICECandidateTypeHost)
|
|
}
|
|
|
|
// Configure required extensions
|
|
sdes, _ := url.Parse(sdp.SDESRTPStreamIDURI)
|
|
sdedMid, _ := url.Parse(sdp.SDESMidURI)
|
|
exts := []sdp.ExtMap{
|
|
{
|
|
URI: sdes,
|
|
},
|
|
{
|
|
URI: sdedMid,
|
|
},
|
|
}
|
|
s.AddSDPExtensions(webrtc.SDPSectionVideo, exts)
|
|
|
|
return &WebRTCConfig{
|
|
Configuration: c,
|
|
SettingEngine: s,
|
|
feedbackTypes: ft,
|
|
receiver: ReceiverConfig{
|
|
maxBandwidth: conf.MaxBandwidth,
|
|
maxBufferTime: conf.MaxBufferTime,
|
|
},
|
|
}, nil
|
|
}
|