44 "encoding/base64"
55 "encoding/json"
66 "fmt"
7+ "net"
78 "strings"
89
910 "github.com/pion/webrtc/v4"
@@ -19,6 +20,12 @@ type Session struct {
1920 shouldUmountVirtualMedia bool
2021}
2122
23+ type SessionConfig struct {
24+ ICEServers []string
25+ LocalIP string
26+ IsCloud bool
27+ }
28+
2229func (s * Session ) ExchangeOffer (offerStr string ) (string , error ) {
2330 b , err := base64 .StdEncoding .DecodeString (offerStr )
2431 if err != nil {
@@ -61,9 +68,29 @@ func (s *Session) ExchangeOffer(offerStr string) (string, error) {
6168 return base64 .StdEncoding .EncodeToString (localDescription ), nil
6269}
6370
64- func newSession () (* Session , error ) {
65- peerConnection , err := webrtc .NewPeerConnection (webrtc.Configuration {
66- ICEServers : []webrtc.ICEServer {{}},
71+ func newSession (config SessionConfig ) (* Session , error ) {
72+ webrtcSettingEngine := webrtc.SettingEngine {}
73+ iceServer := webrtc.ICEServer {}
74+
75+ if config .IsCloud {
76+ if config .ICEServers == nil {
77+ fmt .Printf ("ICE Servers not provided by cloud" )
78+ } else {
79+ iceServer .URLs = config .ICEServers
80+ fmt .Printf ("Using ICE Servers provided by cloud: %v\n " , iceServer .URLs )
81+ }
82+
83+ if config .LocalIP == "" || net .ParseIP (config .LocalIP ) == nil {
84+ fmt .Printf ("Local IP address %v not provided or invalid, won't set NAT1To1IPs\n " , config .LocalIP )
85+ } else {
86+ webrtcSettingEngine .SetNAT1To1IPs ([]string {config .LocalIP }, webrtc .ICECandidateTypeSrflx )
87+ fmt .Printf ("Setting NAT1To1IPs to %s\n " , config .LocalIP )
88+ }
89+ }
90+
91+ api := webrtc .NewAPI (webrtc .WithSettingEngine (webrtcSettingEngine ))
92+ peerConnection , err := api .NewPeerConnection (webrtc.Configuration {
93+ ICEServers : []webrtc.ICEServer {iceServer },
6794 })
6895 if err != nil {
6996 return nil , err
0 commit comments