-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprovider.go
240 lines (194 loc) · 5.94 KB
/
provider.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package wamp_webrtc_go
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
"github.com/xconnio/wampproto-go"
"github.com/xconnio/wampproto-go/util"
"github.com/xconnio/xconn-go"
)
type WebRTCProvider struct {
answerers map[string]*Answerer
onNewAnswerer func(sessionID string, answerer *Answerer)
iceServers []webrtc.ICEServer
sync.Mutex
}
func NewWebRTCHandler() *WebRTCProvider {
return &WebRTCProvider{
answerers: make(map[string]*Answerer),
iceServers: make([]webrtc.ICEServer, 0),
}
}
func (r *WebRTCProvider) OnAnswerer(callback func(sessionID string, answerer *Answerer)) {
r.Lock()
defer r.Unlock()
r.onNewAnswerer = callback
}
func (r *WebRTCProvider) ensureAnswerer(sessionID string) *Answerer {
r.Lock()
defer r.Unlock()
answerer, exists := r.answerers[sessionID]
if !exists {
answerer = NewAnswerer()
r.answerers[sessionID] = answerer
if r.onNewAnswerer != nil {
r.onNewAnswerer(sessionID, answerer)
}
}
return answerer
}
func (r *WebRTCProvider) addIceCandidate(requestID string, candidate webrtc.ICECandidateInit) error {
answerer := r.ensureAnswerer(requestID)
return answerer.AddICECandidate(candidate)
}
func (r *WebRTCProvider) handleOffer(requestID string, offer Offer, answerConfig *AnswerConfig) (*Answer, error) {
answerer := r.ensureAnswerer(requestID)
return answerer.Answer(answerConfig, offer, 100*time.Millisecond)
}
func (r *WebRTCProvider) Setup(config *ProviderConfig) {
r.iceServers = append(r.iceServers, config.IceServers...)
_, err := config.Session.Register(config.ProcedureHandleOffer, r.offerFunc, nil)
if err != nil {
log.Errorf("failed to register webrtc offer: %v", err)
return
}
_, err = config.Session.Subscribe(config.TopicHandleRemoteCandidates, r.onRemoteCandidate, nil)
if err != nil {
log.Errorf("failed to subscribe to webrtc candidates events: %v", err)
return
}
r.OnAnswerer(func(sessionID string, answerer *Answerer) {
answerer.OnIceCandidate(func(candidate *webrtc.ICECandidate) {
answerData, err := json.Marshal(candidate.ToJSON())
if err != nil {
log.Errorf("failed to marshal answer: %v", err)
return
}
args := []any{sessionID, string(answerData)}
if err = config.Session.Publish(config.TopicPublishLocalCandidate, args, nil, nil); err != nil {
log.Errorf("failed to publish answer: %v", err)
}
})
go func() {
select {
case channel := <-answerer.WaitReady():
if err = r.handleWAMPClient(channel, config); err != nil {
log.Errorf("failed to handle answer: %v", err)
_ = answerer.connection.Close()
}
case <-time.After(20 * time.Second):
log.Errorln("webrtc connection didn't establish after 20 seconds")
}
}()
})
}
func (r *WebRTCProvider) handleWAMPClient(channel *webrtc.DataChannel, config *ProviderConfig) error {
rtcPeer := NewWebRTCPeer(channel)
hello, err := xconn.ReadHello(rtcPeer, config.Serializer)
if err != nil {
return err
}
base, err := xconn.Accept(rtcPeer, hello, config.Serializer, config.Authenticator)
if err != nil {
return err
}
if !config.Routed {
return nil
}
xconnRouter := xconn.NewRouter()
xconnRouter.AddRealm("realm1")
if err = xconnRouter.AttachClient(base); err != nil {
return fmt.Errorf("failed to attach client %w", err)
}
parser := NewWebRTCMessageAssembler()
channel.OnMessage(func(msg webrtc.DataChannelMessage) {
fullMsg := parser.Feed(msg.Data)
if fullMsg != nil {
if err = base.Write(fullMsg); err != nil {
log.Errorf("failed to send wamp message: %v", err)
return
}
}
})
channel.OnClose(func() {
_ = base.Close()
})
for {
msg, err := base.ReadMessage()
if err != nil {
_ = xconnRouter.DetachClient(base)
break
}
if err = xconnRouter.ReceiveMessage(base, msg); err != nil {
log.Println(err)
return nil
}
data, err := config.Serializer.Serialize(msg)
if err != nil {
log.Printf("failed to serialize message: %v", err)
return nil
}
for chunk := range parser.ChunkMessage(data) {
if err = channel.Send(chunk); err != nil {
log.Errorf("failed to write message: %v", err)
return nil
}
}
}
return err
}
func (r *WebRTCProvider) offerFunc(_ context.Context, invocation *xconn.Invocation) *xconn.Result {
if len(invocation.Arguments) < 2 {
return &xconn.Result{Err: wampproto.ErrInvalidArgument}
}
requestID, ok := util.AsString(invocation.Arguments[0])
if !ok {
return &xconn.Result{Err: wampproto.ErrInvalidArgument, Arguments: []any{"request ID must be a string"}}
}
offerJSON, ok := util.AsString(invocation.Arguments[1])
if !ok {
return &xconn.Result{Err: wampproto.ErrInvalidArgument, Arguments: []any{"offer must be a string"}}
}
var offer Offer
if err := json.Unmarshal([]byte(offerJSON), &offer); err != nil {
return &xconn.Result{Err: wampproto.ErrInvalidArgument, Arguments: []any{err.Error()}}
}
r.iceServers = append(r.iceServers, webrtc.ICEServer{URLs: []string{"stun:stun.l.google.com:19302"}})
cfg := &AnswerConfig{ICEServers: r.iceServers}
answer, err := r.handleOffer(requestID, offer, cfg)
if err != nil {
return &xconn.Result{Err: wampproto.ErrInvalidArgument, Arguments: []any{err.Error()}}
}
answerData, err := json.Marshal(answer)
if err != nil {
return &xconn.Result{Err: wampproto.ErrInvalidArgument, Arguments: []any{err.Error()}}
}
return &xconn.Result{Arguments: []any{string(answerData)}}
}
func (r *WebRTCProvider) onRemoteCandidate(event *xconn.Event) {
if len(event.Arguments) < 2 {
return
}
requestID, ok := util.AsString(event.Arguments[0])
if !ok {
log.Errorln("request ID must be a string")
return
}
candidateJSON, ok := event.Arguments[1].(string)
if !ok {
log.Errorln("offer must be a string")
return
}
var candidate webrtc.ICECandidateInit
if err := json.Unmarshal([]byte(candidateJSON), &candidate); err != nil {
return
}
if err := r.addIceCandidate(requestID, candidate); err != nil {
log.Errorf("failed to add ice candidate: %v", err)
return
}
}