-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanswerer.go
133 lines (107 loc) · 2.81 KB
/
answerer.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
package wamp_webrtc_go
import (
"sync"
"time"
"github.com/pion/webrtc/v4"
log "github.com/sirupsen/logrus"
)
type Answerer struct {
connection *webrtc.PeerConnection
channel chan *webrtc.DataChannel
onIceCandidate func(candidate *webrtc.ICECandidate)
cachedCandidates []webrtc.ICECandidateInit
sync.Mutex
}
func NewAnswerer() *Answerer {
return &Answerer{
channel: make(chan *webrtc.DataChannel, 1),
}
}
func (a *Answerer) Answer(answerConfig *AnswerConfig, offer Offer, trickleAfter time.Duration) (*Answer, error) {
start := time.Now()
end := start.Add(trickleAfter)
config := webrtc.Configuration{
ICEServers: answerConfig.ICEServers,
ICECandidatePoolSize: 10,
}
connection, err := webrtc.NewPeerConnection(config)
if err != nil {
return nil, err
}
a.Lock()
a.connection = connection
a.Unlock()
if err = connection.SetRemoteDescription(offer.Description); err != nil {
return nil, err
}
for _, candidate := range offer.Candidates {
if err = connection.AddICECandidate(candidate); err != nil {
return nil, err
}
}
a.Lock()
for _, candidate := range a.cachedCandidates {
if err = connection.AddICECandidate(candidate); err != nil {
log.Errorf("failed to add ice candidate: %v", err)
}
}
a.cachedCandidates = nil
a.Unlock()
done := make(chan struct{})
var trickle = false
var initialCandidates []webrtc.ICECandidateInit
connection.OnICECandidate(func(candidate *webrtc.ICECandidate) {
if candidate == nil {
log.Debugf("ice candidates gathering took %s", time.Since(start))
return
}
if trickle || time.Now().After(end) {
a.onIceCandidate(candidate)
} else {
initialCandidates = append(initialCandidates, candidate.ToJSON())
// host candidate gathering is done, any further candidates should
// be signaled with Trickle ICE.
if candidate.Typ != webrtc.ICECandidateTypeHost {
trickle = true
done <- struct{}{}
}
}
})
var once sync.Once
connection.OnDataChannel(func(d *webrtc.DataChannel) {
once.Do(func() { a.channel <- d })
})
answer, err := connection.CreateAnswer(nil)
if err != nil {
return nil, err
}
if err = connection.SetLocalDescription(answer); err != nil {
return nil, err
}
select {
case <-done:
case <-time.After(time.Until(end)):
}
return &Answer{
Candidates: initialCandidates,
Description: answer,
}, nil
}
func (a *Answerer) OnIceCandidate(callback func(candidate *webrtc.ICECandidate)) {
a.Lock()
defer a.Unlock()
a.onIceCandidate = callback
}
func (a *Answerer) AddICECandidate(candidate webrtc.ICECandidateInit) error {
a.Lock()
defer a.Unlock()
if a.connection == nil {
a.cachedCandidates = append(a.cachedCandidates, candidate)
return nil
} else {
return a.connection.AddICECandidate(candidate)
}
}
func (a *Answerer) WaitReady() chan *webrtc.DataChannel {
return a.channel
}