forked from pion/webrtc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signaling.go
455 lines (392 loc) · 15.6 KB
/
signaling.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package webrtc
import (
"fmt"
"math/rand"
"net"
"strings"
"time"
"github.com/pions/pkg/stun"
"github.com/servicekit/webrtc/internal/sdp"
"github.com/servicekit/webrtc/pkg/ice"
"github.com/pkg/errors"
)
/*
setRemote(OFFER) setLocal(PRANSWER)
/-----\ /-----\
| | | |
v | v |
+---------------+ | +---------------+ |
| |----/ | |----/
| have- | setLocal(PRANSWER) | have- |
| remote-offer |------------------- >| local-pranswer|
| | | |
| | | |
+---------------+ +---------------+
^ | |
| | setLocal(ANSWER) |
setRemote(OFFER) | |
| V setLocal(ANSWER) |
+---------------+ |
| | |
| |<---------------------------+
| stable |
| |<---------------------------+
| | |
+---------------+ setRemote(ANSWER) |
^ | |
| | setLocal(OFFER) |
setRemote(ANSWER) | |
| V |
+---------------+ +---------------+
| | | |
| have- | setRemote(PRANSWER) |have- |
| local-offer |------------------- >|remote-pranswer|
| | | |
| |----\ | |----\
+---------------+ | +---------------+ |
^ | ^ |
| | | |
\-----/ \-----/
setLocal(OFFER) setRemote(PRANSWER)
*/
// RTCSignalingState indicates the state of the offer/answer process
type RTCSignalingState int
const (
// RTCSignalingStateStable indicates there is no offeranswer exchange in progress.
RTCSignalingStateStable RTCSignalingState = iota + 1
// RTCSignalingStateHaveLocalOffer indicates A local description, of type "offer", has been successfully applied.
RTCSignalingStateHaveLocalOffer
// RTCSignalingStateHaveRemoteOffer indicates A remote description, of type "offer", has been successfully applied.
RTCSignalingStateHaveRemoteOffer
// RTCSignalingStateHaveLocalPranswer indicates A remote description of type "offer" has been successfully applied and a local description of type "pranswer" has been successfully applied.
RTCSignalingStateHaveLocalPranswer
// RTCSignalingStateHaveRemotePranswer indicates A local description of type "offer" has been successfully applied and a remote description of type "pranswer" has been successfully applied.
RTCSignalingStateHaveRemotePranswer
// RTCSignalingStateClosed indicates The RTCPeerConnection has been closed.
RTCSignalingStateClosed
)
func (t RTCSignalingState) String() string {
switch t {
case RTCSignalingStateStable:
return "stable"
case RTCSignalingStateHaveLocalOffer:
return "have-local-offer"
case RTCSignalingStateHaveRemoteOffer:
return "have-remote-offer"
case RTCSignalingStateHaveLocalPranswer:
return "have-local-pranswer"
case RTCSignalingStateHaveRemotePranswer:
return "have-remote-pranswer"
case RTCSignalingStateClosed:
return "closed"
default:
return "Unknown"
}
}
// RTCSdpType describes the type of an RTCSessionDescription
type RTCSdpType int
const (
// RTCSdpTypeOffer indicates that a description MUST be treated as an SDP offer.
RTCSdpTypeOffer RTCSdpType = iota + 1
// RTCSdpTypePranswer indicates that a description MUST be treated as an SDP answer, but not a final answer.
RTCSdpTypePranswer
// RTCSdpTypeAnswer indicates that a description MUST be treated as an SDP final answer, and the offer-answer exchange MUST be considered complete.
RTCSdpTypeAnswer
// RTCSdpTypeRollback indicates that a description MUST be treated as canceling the current SDP negotiation and moving the SDP offer and answer back to what it was in the previous stable state.
RTCSdpTypeRollback
)
func (t RTCSdpType) String() string {
switch t {
case RTCSdpTypeOffer:
return "offer"
case RTCSdpTypePranswer:
return "pranswer"
case RTCSdpTypeAnswer:
return "answer"
case RTCSdpTypeRollback:
return "rollback"
default:
return "Unknown"
}
}
// RTCSessionDescription is used to expose local and remote session descriptions.
type RTCSessionDescription struct {
Type RTCSdpType
Sdp string
}
// SetRemoteDescription sets the SessionDescription of the remote peer
func (r *RTCPeerConnection) SetRemoteDescription(desc RTCSessionDescription) error {
if r.remoteDescription != nil {
return errors.Errorf("remoteDescription is already defined, SetRemoteDescription can only be called once")
}
r.currentRemoteDescription = &desc
r.remoteDescription = &sdp.SessionDescription{}
return r.remoteDescription.Unmarshal(desc.Sdp)
}
// RTCOfferOptions describes the options used to control the offer creation process
type RTCOfferOptions struct {
VoiceActivityDetection bool
ICERestart bool
}
// TODO
type candidate struct {
transport string
basePriority uint16
ip string
port int
typ string
}
// CreateOffer starts the RTCPeerConnection and generates the localDescription
func (r *RTCPeerConnection) CreateOffer(options *RTCOfferOptions) (RTCSessionDescription, error) {
panic("TODO")
// if options != nil {
// panic("TODO handle options")
// }
// if r.IsClosed {
// return RTCSessionDescription{}, &InvalidStateError{Err: ErrConnectionClosed}
// }
// useIdentity := r.idpLoginURL != nil
// if useIdentity {
// panic("TODO handle identity provider")
// }
// d := sdp.NewJSEPSessionDescription(
// r.tlscfg.Fingerprint(),
// useIdentity).
// WithValueAttribute(sdp.AttrKeyGroup, "BUNDLE audio video") // TODO: Support BUNDLE
// var streamlabels string
// for _, transceiver := range r.rtpTransceivers {
// if transceiver.Sender == nil ||
// transceiver.Sender.Track == nil {
// continue
// }
// track := transceiver.Sender.Track
// cname := "pion" // TODO: Support RTP streams synchronisation
// steamlabel := "pion" // TODO: Support steam labels
// codec, err := r.mediaEngine.getCodec(track.PayloadType)
// if err != nil {
// return RTCSessionDescription{}, err
// }
// media := sdp.NewJSEPMediaDescription(track.Kind.String(), []string{}).
// WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
// WithValueAttribute(sdp.AttrKeyMID, transceiver.Mid).
// WithPropertyAttribute(transceiver.Direction.String()).
// WithICECredentials(r.iceAgent.Ufrag, r.iceAgent.Pwd).
// WithPropertyAttribute(sdp.AttrKeyICELite). // TODO: get ICE type from ICE Agent
// WithPropertyAttribute(sdp.AttrKeyRtcpMux). // TODO: support RTCP fallback
// WithPropertyAttribute(sdp.AttrKeyRtcpRsize). // TODO: Support Reduced-Size RTCP?
// WithCodec(
// codec.PayloadType,
// codec.Name,
// codec.ClockRate,
// codec.Channels,
// codec.SdpFmtpLine,
// ).
// WithMediaSource(track.Ssrc, cname, steamlabel, track.Label)
// err = r.addICECandidates(media)
// if err != nil {
// return RTCSessionDescription{}, err
// }
// streamlabels = streamlabels + " " + steamlabel
// d.WithMedia(media)
// }
// d.WithValueAttribute(sdp.AttrKeyMsidSemantic, " "+sdp.SemanticTokenWebRTCMediaStreams+streamlabels)
// return RTCSessionDescription{
// Type: RTCSdpTypeOffer,
// Sdp: d.Marshal(),
// }, nil
}
// RTCAnswerOptions describes the options used to control the answer creation process
type RTCAnswerOptions struct {
VoiceActivityDetection bool
}
// CreateAnswer starts the RTCPeerConnection and generates the localDescription
func (r *RTCPeerConnection) CreateAnswer(options *RTCOfferOptions) (RTCSessionDescription, error) {
if options != nil {
panic("TODO handle options")
}
if r.IsClosed {
return RTCSessionDescription{}, &InvalidStateError{Err: ErrConnectionClosed}
}
useIdentity := r.idpLoginURL != nil
if useIdentity {
panic("TODO handle identity provider")
}
candidates, err := r.buildCandidates()
if err != nil {
return RTCSessionDescription{}, &InvalidStateError{Err: ErrConnectionClosed}
}
d := sdp.NewJSEPSessionDescription(
r.networkManager.DTLSFingerprint(),
useIdentity)
bundleValue := "BUNDLE"
for _, remoteMedia := range r.remoteDescription.MediaDescriptions {
if strings.HasPrefix(remoteMedia.MediaName, "audio") {
bundleValue += " audio"
_, err := r.addAnswerMedia(d, RTCRtpCodecTypeAudio, candidates)
if err != nil {
return RTCSessionDescription{}, err
}
} else if strings.HasPrefix(remoteMedia.MediaName, "video") {
bundleValue += " video"
_, err := r.addAnswerMedia(d, RTCRtpCodecTypeVideo, candidates)
if err != nil {
return RTCSessionDescription{}, err
}
} else if strings.HasPrefix(remoteMedia.MediaName, "application") {
bundleValue += " data"
r.addAnswerData(d, candidates)
}
}
d = d.WithValueAttribute(sdp.AttrKeyGroup, bundleValue)
return RTCSessionDescription{
Type: RTCSdpTypeAnswer,
Sdp: d.Marshal(),
}, nil
}
func (r *RTCPeerConnection) addAnswerMedia(d *sdp.SessionDescription, codecType RTCRtpCodecType, candidates []candidate) (string, error) {
added := false
var streamlabels string
for _, tranceiver := range r.rtpTransceivers {
if tranceiver.Sender == nil ||
tranceiver.Sender.Track == nil ||
tranceiver.Sender.Track.Kind != codecType {
continue
}
track := tranceiver.Sender.Track
cname := track.Label // TODO: Support RTP streams synchronisation
steamlabel := track.Label // TODO: Support steam labels
codec, err := r.mediaEngine.getCodec(track.PayloadType)
if err != nil {
return "", err
}
media := sdp.NewJSEPMediaDescription(track.Kind.String(), []string{}).
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
WithValueAttribute(sdp.AttrKeyMID, tranceiver.Mid).
WithPropertyAttribute(tranceiver.Direction.String()).
WithICECredentials(r.iceAgent.Ufrag, r.iceAgent.Pwd).
WithPropertyAttribute(sdp.AttrKeyICELite). // TODO: get ICE type from ICE Agent
WithPropertyAttribute(sdp.AttrKeyRtcpMux). // TODO: support RTCP fallback
WithPropertyAttribute(sdp.AttrKeyRtcpRsize). // TODO: Support Reduced-Size RTCP?
WithCodec(
codec.PayloadType,
codec.Name,
codec.ClockRate,
codec.Channels,
codec.SdpFmtpLine,
).
WithMediaSource(track.Ssrc, cname, steamlabel, track.Label)
for _, c := range candidates {
media.WithCandidate(1, c.transport, c.basePriority, c.ip, c.port, c.typ)
}
media.WithPropertyAttribute("end-of-candidates") // TODO: Support full trickle-ice
d.WithMedia(media)
streamlabels = streamlabels + " " + steamlabel
added = true
}
if !added {
// Add media line to advertise capabilities
media := sdp.NewJSEPMediaDescription(codecType.String(), []string{}).
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
WithValueAttribute(sdp.AttrKeyMID, codecType.String()).
WithPropertyAttribute(RTCRtpTransceiverDirectionSendrecv.String()).
WithICECredentials(r.iceAgent.Ufrag, r.iceAgent.Pwd). // TODO: get credendials form ICE agent
WithPropertyAttribute(sdp.AttrKeyICELite). // TODO: get ICE type from ICE Agent (#23)
WithPropertyAttribute(sdp.AttrKeyRtcpMux). // TODO: support RTCP fallback
WithPropertyAttribute(sdp.AttrKeyRtcpRsize) // TODO: Support Reduced-Size RTCP?
for _, codec := range r.mediaEngine.getCodecsByKind(codecType) {
media.WithCodec(
codec.PayloadType,
codec.Name,
codec.ClockRate,
codec.Channels,
codec.SdpFmtpLine,
)
}
for _, c := range candidates {
media.WithCandidate(1, c.transport, c.basePriority, c.ip, c.port, c.typ)
}
media.WithPropertyAttribute("end-of-candidates") // TODO: Support full trickle-ice
d.WithMedia(media)
}
return streamlabels, nil
}
func (r *RTCPeerConnection) addAnswerData(d *sdp.SessionDescription, candidates []candidate) {
media := (&sdp.MediaDescription{
MediaName: "application 9 DTLS/SCTP 5000",
ConnectionData: "IN IP4 0.0.0.0",
Attributes: []string{},
}).
WithValueAttribute(sdp.AttrKeyConnectionSetup, sdp.ConnectionRoleActive.String()). // TODO: Support other connection types
WithValueAttribute(sdp.AttrKeyMID, "data").
WithValueAttribute("sctpmap:5000", "webrtc-datachannel 1024").
WithICECredentials(r.iceAgent.Ufrag, r.iceAgent.Pwd).
WithPropertyAttribute(sdp.AttrKeyICELite) // TODO: get ICE type from ICE Agent
for _, c := range candidates {
media.WithCandidate(1, c.transport, c.basePriority, c.ip, c.port, c.typ)
}
media.WithPropertyAttribute("end-of-candidates") // TODO: Support full trickle-ice
d.WithMedia(media)
}
func (r *RTCPeerConnection) buildCandidates() ([]candidate, error) {
basePriority := uint16(rand.Uint32() & (1<<16 - 1))
candidates := make([]candidate, 0)
for _, c := range ice.HostInterfaces() {
boundAddress, err := r.networkManager.Listen(c + ":0")
if err != nil {
return nil, err
}
candidates = append(candidates, candidate{
transport: "udp",
basePriority: basePriority,
ip: boundAddress.IP.String(),
port: boundAddress.Port,
typ: "host",
})
basePriority = basePriority + 1
}
for _, servers := range r.iceAgent.Servers {
for _, server := range servers {
if server.Type != ice.ServerTypeSTUN {
continue
}
// TODO Do we want the timeout to be configurable?
proto := server.TransportType.String()
client, err := stun.NewClient(proto, fmt.Sprintf("%s:%d", server.Host, server.Port), time.Second*5)
if err != nil {
return nil, errors.Wrapf(err, "Failed to create STUN client")
}
localAddr, ok := client.LocalAddr().(*net.UDPAddr)
if !ok {
return nil, errors.Errorf("Failed to cast STUN client to UDPAddr")
}
resp, err := client.Request()
if err != nil {
return nil, errors.Wrapf(err, "Failed to make STUN request")
}
if err := client.Close(); err != nil {
return nil, errors.Wrapf(err, "Failed to close STUN client")
}
attr, ok := resp.GetOneAttribute(stun.AttrXORMappedAddress)
if !ok {
return nil, errors.Errorf("Got respond from STUN server that did not contain XORAddress")
}
var addr stun.XorAddress
if err := addr.Unpack(resp, attr); err != nil {
return nil, errors.Wrapf(err, "Failed to unpack STUN XorAddress response")
}
boundAddress, err := r.networkManager.Listen(fmt.Sprintf("0.0.0.0:%d", localAddr.Port))
if err != nil {
return nil, err
}
candidates = append(candidates, candidate{
transport: "udp",
basePriority: basePriority,
ip: addr.IP.String(),
port: boundAddress.Port,
typ: "srflx",
})
basePriority = basePriority + 1
}
}
return candidates, nil
}