Skip to content

Commit

Permalink
add support to add SetNAT1To1IPs
Browse files Browse the repository at this point in the history
  • Loading branch information
Yohan Totting committed Dec 28, 2023
1 parent e360c99 commit acd80c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ func NewClient(s *SFU, id string, name string, peerConnectionConfig webrtc.Confi
}
}

if s.publicIP != "" {
settingEngine.SetNAT1To1IPs([]string{s.publicIP}, s.nat1To1IPsCandidateType)
}

// Create a new RTCPeerConnection
peerConnection, err := webrtc.NewAPI(webrtc.WithMediaEngine(m), webrtc.WithSettingEngine(settingEngine), webrtc.WithInterceptorRegistry(i)).NewPeerConnection(peerConnectionConfig)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ func (m *Manager) NewRoom(id, name, roomType string, opts RoomOptions) (*Room, e
PLIInterval: opts.PLIInterval,
QualityPreset: opts.QualityPreset,
EnableBandwidthEstimator: m.options.EnableBandwidthEstimator,
PublicIP: m.options.PublicIP,
NAT1To1IPsCandidateType: m.options.NAT1To1IPsCandidateType,
}

newSFU := New(m.context, sfuOpts)
Expand Down
22 changes: 22 additions & 0 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@ type Options struct {
EnableBridging bool
EnableBandwidthEstimator bool
IceServers []webrtc.ICEServer
PublicIP string
// If PublicIP is set, then you should consider to set this NAT1To1IPsCandidateType as well. By default, it is set to ICECandidateTypeHost.
// Two types of candidates are supported:
//
// ICECandidateTypeHost:
//
// The public IP address will be used for the host candidate in the SDP.
//
// ICECandidateTypeSrflx:
//
// A server reflexive candidate with the given public IP address will be added to the SDP.
//
// Please note that if you choose ICECandidateTypeHost, then the private IP address
// won't be advertised with the peer. Also, this option cannot be used along with mDNS.
//
// If you choose ICECandidateTypeSrflx, it simply adds a server reflexive candidate
// with the public IP. The host candidate is still available along with mDNS
// capabilities unaffected. Also, you cannot give STUN server URL at the same time.
// It will result in an error otherwise.
NAT1To1IPsCandidateType webrtc.ICECandidateType
}

func DefaultOptions() Options {
Expand All @@ -39,6 +59,8 @@ func DefaultOptions() Options {
URLs: []string{"stun:stun.l.google.com:19302"},
},
},
PublicIP: "",
NAT1To1IPsCandidateType: webrtc.ICECandidateTypeHost,
}
}

Expand Down
6 changes: 6 additions & 0 deletions sfu.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type SFU struct {
dataChannels *SFUDataChannelList
idleChan chan bool
iceServers []webrtc.ICEServer
nat1To1IPsCandidateType webrtc.ICECandidateType
mu sync.Mutex
mux *UDPMux
onStop func()
Expand All @@ -117,6 +118,7 @@ type SFU struct {
qualityRef QualityPreset
portStart uint16
portEnd uint16
publicIP string
onTrackAvailableCallbacks []func(tracks []ITrack)
onClientRemovedCallbacks []func(*Client)
onClientAddedCallbacks []func(*Client)
Expand All @@ -138,6 +140,8 @@ type sfuOptions struct {
Codecs []string
PLIInterval time.Duration
EnableBandwidthEstimator bool
PublicIP string
NAT1To1IPsCandidateType webrtc.ICECandidateType
}

// @Param muxPort: port for udp mux
Expand All @@ -157,12 +161,14 @@ func New(ctx context.Context, opts sfuOptions) *SFU {
enableBandwidthEstimator: opts.EnableBandwidthEstimator,
pliInterval: opts.PLIInterval,
qualityRef: opts.QualityPreset,
publicIP: opts.PublicIP,
relayTracks: make(map[string]ITrack),
portStart: opts.PortStart,
portEnd: opts.PortEnd,
onTrackAvailableCallbacks: make([]func(tracks []ITrack), 0),
onClientRemovedCallbacks: make([]func(*Client), 0),
onClientAddedCallbacks: make([]func(*Client), 0),
nat1To1IPsCandidateType: opts.NAT1To1IPsCandidateType,
}

go func() {
Expand Down

0 comments on commit acd80c5

Please sign in to comment.