4
4
"encoding/base64"
5
5
"encoding/json"
6
6
"fmt"
7
+ "net"
7
8
"strings"
8
9
9
10
"github.com/pion/webrtc/v4"
@@ -19,6 +20,12 @@ type Session struct {
19
20
shouldUmountVirtualMedia bool
20
21
}
21
22
23
+ type SessionConfig struct {
24
+ ICEServers []string
25
+ LocalIP string
26
+ IsCloud bool
27
+ }
28
+
22
29
func (s * Session ) ExchangeOffer (offerStr string ) (string , error ) {
23
30
b , err := base64 .StdEncoding .DecodeString (offerStr )
24
31
if err != nil {
@@ -61,9 +68,29 @@ func (s *Session) ExchangeOffer(offerStr string) (string, error) {
61
68
return base64 .StdEncoding .EncodeToString (localDescription ), nil
62
69
}
63
70
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 },
67
94
})
68
95
if err != nil {
69
96
return nil , err
0 commit comments