The SettingEngine allows you to influence WebRTC behavior in ways that are not supported by the standard WebRTC API. This enables advanced use cases like custom network configurations, DTLS tuning, and ICE optimization without deviating from the WebRTC API elsewhere.
The SettingEngine must be configured before creating a PeerConnection. Settings are applied when you create an API object with webrtc.NewAPI(webrtc.WithSettingEngine(s)).
package mainimport "github.com/pion/webrtc/v4"func main() { // Create a SettingEngine s := webrtc.SettingEngine{} // Configure settings (examples below) s.SetEphemeralUDPPortRange(10000, 20000) // Create an API with the SettingEngine api := webrtc.NewAPI(webrtc.WithSettingEngine(s)) // Use the API to create PeerConnections peerConnection, err := api.NewPeerConnection(webrtc.Configuration{})}
Control which network types are used for ICE candidates:
network-types.go
s := webrtc.SettingEngine{}// Only use UDP networkss.SetNetworkTypes([]webrtc.NetworkType{ webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6,})// Only use TCP networkss.SetNetworkTypes([]webrtc.NetworkType{ webrtc.NetworkTypeTCP4, webrtc.NetworkTypeTCP6,})
Filter which network interfaces and IP addresses are used:
filtering.go
import "net"s := webrtc.SettingEngine{}// Exclude certain interfaces (e.g., exclude docker interfaces)s.SetInterfaceFilter(func(interfaceName string) bool { // Return true to keep the interface return interfaceName != "docker0"})// Exclude certain IPs (e.g., only use private IPs)s.SetIPFilter(func(ip net.IP) bool { // Return true to keep the IP return ip.IsPrivate()})
Configure external IP addresses for servers behind NAT (e.g., AWS EC2 with Elastic IP):
nat-1to1.go
s := webrtc.SettingEngine{}// Use public IP for host candidatess.SetNAT1To1IPs( []string{"203.0.113.1"}, webrtc.ICECandidateTypeHost,)// Or add server reflexive candidate with public IPs.SetNAT1To1IPs( []string{"203.0.113.1"}, webrtc.ICECandidateTypeSrflx,)
SetNAT1To1IPs is deprecated. Use SetICEAddressRewriteRules for more fine-grained control.
s := webrtc.SettingEngine{}// Set maximum receive buffer sizes.SetSCTPMaxReceiveBufferSize(1024 * 1024) // 1MB// Set maximum message sizes.SetSCTPMaxMessageSize(256 * 1024) // 256KB
Serve multiple PeerConnections on a single UDP port:
udp-mux.go
import "github.com/pion/ice/v4"s := webrtc.SettingEngine{}// Create a UDPMux on port 8443mux, err := ice.NewMultiUDPMuxFromPort(8443)if err != nil { panic(err)}s.SetICEUDPMux(mux)api := webrtc.NewAPI(webrtc.WithSettingEngine(s))// All PeerConnections will use port 8443peerConnection1, _ := api.NewPeerConnection(webrtc.Configuration{})peerConnection2, _ := api.NewPeerConnection(webrtc.Configuration{})
s := webrtc.SettingEngine{}// Enable data channel detachings.DetachDataChannels()api := webrtc.NewAPI(webrtc.WithSettingEngine(s))peerConnection, _ := api.NewPeerConnection(webrtc.Configuration{})dataChannel, _ := peerConnection.CreateDataChannel("data", nil)dataChannel.OnOpen(func() { // Detach the data channel raw, err := dataChannel.Detach() if err != nil { panic(err) } // Now you have direct access to read/write raw.Write([]byte("Hello"))})
s := webrtc.SettingEngine{}// Set DTLS replay protection windows.SetDTLSReplayProtectionWindow(64)// Set SRTP replay protection windows.SetSRTPReplayProtectionWindow(64)// Set SRTCP replay protection window s.SetSRTCPReplayProtectionWindow(64)// Or disable replay protection (not recommended)s.DisableSRTPReplayProtection(true)s.DisableSRTCPReplayProtection(true)