This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconn.go
130 lines (114 loc) · 3.38 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package main
import (
"fmt"
"github.com/pion/logging"
"github.com/pion/webrtc/v4"
"strconv"
"strings"
)
type conn struct {
api *webrtc.API
gatherer *webrtc.ICEGatherer
ice *webrtc.ICETransport
dtl *webrtc.DTLSTransport
sct *webrtc.SCTPTransport
}
type webRtcConfig struct {
Username string `json:"Username"`
Password string `json:"Password"`
ExpirationInSeconds int `json:"ExpirationInSeconds"`
TurnAuthServers []struct {
Username string `json:"Username"`
Password string `json:"Password"`
Urls []string `json:"Urls"`
} `json:"TurnAuthServers"`
}
func newConn(conf webRtcConfig) (*conn, error) {
factory := logging.NewDefaultLoggerFactory()
factory.DefaultLogLevel = logging.LogLevelDebug
var s webrtc.SettingEngine
s.LoggerFactory = factory
api := webrtc.NewAPI(webrtc.WithSettingEngine(s))
c := &conn{api: api}
iceServers := make([]webrtc.ICEServer, 0, len(conf.TurnAuthServers))
for _, turnAuthServer := range conf.TurnAuthServers {
iceServers = append(iceServers, webrtc.ICEServer{
URLs: turnAuthServer.Urls,
Username: turnAuthServer.Username,
Credential: turnAuthServer.Password,
CredentialType: webrtc.ICECredentialTypePassword,
})
}
var err error
c.gatherer, err = api.NewICEGatherer(webrtc.ICEGatherOptions{
ICEServers: iceServers,
})
if err != nil {
return nil, fmt.Errorf("failed to create ice gatherer: %w", err)
}
c.ice = api.NewICETransport(c.gatherer)
if c.dtl, err = api.NewDTLSTransport(c.ice, nil); err != nil {
return nil, fmt.Errorf("failed to create dtls transport: %w", err)
}
c.sct = api.NewSCTPTransport(c.dtl)
return c, nil
}
func (c *conn) localCandidates() ([]webrtc.ICECandidate, error) {
gatherFinished := make(chan struct{})
c.gatherer.OnLocalCandidate(func(i *webrtc.ICECandidate) {
if i == nil {
close(gatherFinished)
}
})
if err := c.gatherer.Gather(); err != nil {
return nil, fmt.Errorf("failed to gather ice candidates: %w", err)
}
<-gatherFinished
return c.gatherer.GetLocalCandidates()
}
func (c *conn) formatIceCandidate(networkId int, candidate webrtc.ICECandidate, iceParams webrtc.ICEParameters) string {
sb := strings.Builder{}
sb.WriteString("candidate:")
sb.WriteString(candidate.Foundation)
sb.WriteRune(' ')
sb.WriteRune('1')
sb.WriteRune(' ')
sb.WriteString("udp")
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(int(candidate.Priority)))
sb.WriteRune(' ')
sb.WriteString(candidate.Address)
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(int(candidate.Port)))
sb.WriteRune(' ')
sb.WriteString("typ")
sb.WriteRune(' ')
sb.WriteString(candidate.Typ.String())
sb.WriteRune(' ')
if candidate.Typ == webrtc.ICECandidateTypeRelay || candidate.Typ == webrtc.ICECandidateTypeSrflx {
sb.WriteString("raddr")
sb.WriteRune(' ')
sb.WriteString(candidate.RelatedAddress)
sb.WriteRune(' ')
sb.WriteString("rport")
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(int(candidate.RelatedPort)))
sb.WriteRune(' ')
}
sb.WriteString("generation")
sb.WriteRune(' ')
sb.WriteRune('0')
sb.WriteRune(' ')
sb.WriteString("ufrag")
sb.WriteRune(' ')
sb.WriteString(iceParams.UsernameFragment)
sb.WriteRune(' ')
sb.WriteString("network-id")
sb.WriteRune(' ')
sb.WriteString(strconv.Itoa(networkId))
sb.WriteRune(' ')
sb.WriteString("network-cost")
sb.WriteRune(' ')
sb.WriteRune('0') // TODO: Actually calculate this?
return sb.String()
}