/
wrtcnegotiator.go
166 lines (131 loc) · 3.59 KB
/
wrtcnegotiator.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
package server
import (
"sync"
"github.com/juju/errors"
"github.com/peer-calls/peer-calls/v4/server/logger"
"github.com/pion/webrtc/v3"
)
type TransceiverRequest struct {
CodecType webrtc.RTPCodecType
Init webrtc.RtpTransceiverInit
}
type Negotiator struct {
log logger.Logger
initiator bool
peerConnection *webrtc.PeerConnection
onOffer func(webrtc.SessionDescription, error)
onRequestNegotiation func()
negotiationDone chan struct{}
mu sync.Mutex
queuedNegotiation bool
queuedTransceiverRequests []TransceiverRequest
}
func NewNegotiator(
log logger.Logger,
initiator bool,
peerConnection *webrtc.PeerConnection,
onOffer func(webrtc.SessionDescription, error),
onRequestNegotiation func(),
) *Negotiator {
n := &Negotiator{
log: log.WithNamespaceAppended("negotiator"),
initiator: initiator,
peerConnection: peerConnection,
onOffer: onOffer,
onRequestNegotiation: onRequestNegotiation,
}
peerConnection.OnSignalingStateChange(n.handleSignalingStateChange)
return n
}
func (n *Negotiator) AddTransceiverFromKind(t TransceiverRequest) {
logCtx := logger.Ctx{
"codec_type": t.CodecType,
"direction": t.Init.Direction,
}
n.log.Info("Add transceiver", logCtx)
n.mu.Lock()
n.queuedTransceiverRequests = append(n.queuedTransceiverRequests, t)
n.mu.Unlock()
n.log.Info("Negotiate because a transceiver was queued", logCtx)
n.Negotiate()
}
func (n *Negotiator) closeDoneChannel() {
if n.negotiationDone != nil {
close(n.negotiationDone)
n.negotiationDone = nil
}
}
func (n *Negotiator) Done() <-chan struct{} {
n.mu.Lock()
defer n.mu.Unlock()
if n.negotiationDone != nil {
return n.negotiationDone
}
ch := make(chan struct{})
close(ch)
return ch
}
func (n *Negotiator) handleSignalingStateChange(state webrtc.SignalingState) {
// TODO check if we need to have a check for first stable state
// like simple-peer has.
logCtx := logger.Ctx{
"signaling_state": state,
}
n.log.Info("Signaling state changed", logCtx)
n.mu.Lock()
defer n.mu.Unlock()
switch state {
case webrtc.SignalingStateClosed:
n.closeDoneChannel()
case webrtc.SignalingStateStable:
if n.queuedNegotiation {
n.log.Info("Execute queued negotiation", logCtx)
n.queuedNegotiation = false
n.negotiate()
} else {
n.closeDoneChannel()
}
}
}
func (n *Negotiator) Negotiate() (done <-chan struct{}) {
n.log.Info("Negotiate", nil)
n.mu.Lock()
defer n.mu.Unlock()
if n.negotiationDone != nil {
n.log.Info("Negotiate: already negotiating, queueing for later", nil)
n.queuedNegotiation = true
return
}
n.log.Info("Negotiate: start", nil)
n.negotiationDone = make(chan struct{})
n.negotiate()
return n.negotiationDone
}
func (n *Negotiator) addQueuedTransceivers() {
for _, t := range n.queuedTransceiverRequests {
logCtx := logger.Ctx{
"codec_type": t.CodecType,
"direction": t.Init.Direction,
}
n.log.Trace("Add queued transceiver", logCtx)
_, err := n.peerConnection.AddTransceiverFromKind(t.CodecType, t.Init)
if err != nil {
n.log.Error("Add queued transceiver", errors.Trace(err), logCtx)
}
}
n.queuedTransceiverRequests = []TransceiverRequest{}
}
func (n *Negotiator) negotiate() {
n.addQueuedTransceivers()
if !n.initiator {
n.log.Info("negotiate: requesting negotiation from initiator", nil)
n.requestNegotiation()
return
}
n.log.Info("negotiate: creating offer", nil)
offer, err := n.peerConnection.CreateOffer(nil)
n.onOffer(offer, errors.Annotate(err, "create offer"))
}
func (n *Negotiator) requestNegotiation() {
n.onRequestNegotiation()
}