-
Notifications
You must be signed in to change notification settings - Fork 119
/
transport.go
632 lines (499 loc) · 17.6 KB
/
transport.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
package mediaserver
import (
"fmt"
"sync"
"github.com/gofrs/uuid"
native "github.com/notedit/media-server-go/wrapper"
"github.com/notedit/sdp"
)
type senderSideEstimatorListener interface {
native.SenderSideEstimatorListener
deleteSenderSideEstimatorListener()
}
type goSenderSideEstimatorListener struct {
native.SenderSideEstimatorListener
}
func (r *goSenderSideEstimatorListener) deleteSenderSideEstimatorListener() {
native.DeleteDirectorSenderSideEstimatorListener(r.SenderSideEstimatorListener)
}
type overwrittenSenderSideEstimatorListener struct {
p native.SenderSideEstimatorListener
}
func (p *overwrittenSenderSideEstimatorListener) OnTargetBitrateRequested(bitrate uint) {
fmt.Println(bitrate)
}
type dtlsICETransportListener interface {
native.DTLSICETransportListener
deleteDTLSICETransportListener()
}
type goDTLSICETransportListener struct {
native.DTLSICETransportListener
}
func (d *goDTLSICETransportListener) deleteDTLSICETransportListener() {
native.DeleteDTLSICETransportListener(d.DTLSICETransportListener)
}
type overwrittenDTLSICETransportListener struct {
p native.DTLSICETransportListener
}
func (p *overwrittenDTLSICETransportListener) OnDTLSStateChange(state uint) {
fmt.Println("OnDTLSStateChange", state)
}
type (
// TransportStopListener listener
TransportStopListener func()
// IncomingTrackListener new track listener
IncomingTrackListener func(*IncomingStreamTrack, *IncomingStream)
// OutgoingTrackListener new track listener
OutgoingTrackListener func(*OutgoingStreamTrack, *OutgoingStream)
// DTLSStateListener listener
DTLSStateListener func(state string)
)
// ICEStats ice stats for this connection
type ICEStats struct {
RequestsSent int64
RequestsReceived int64
ResponsesSent int64
ResponsesReceived int64
}
// Transport represent a connection between a local ICE candidate and a remote set of ICE candidates over a single DTLS session
type Transport struct {
localIce *sdp.ICEInfo
localDtls *sdp.DTLSInfo
localCandidates []*sdp.CandidateInfo
remoteIce *sdp.ICEInfo
remoteDtls *sdp.DTLSInfo
remoteCandidates []*sdp.CandidateInfo
bundle native.RTPBundleTransport
transport native.DTLSICETransport
connection native.RTPBundleTransportConnection
dtlsState string
username string
incomingStreams map[string]*IncomingStream
outgoingStreams map[string]*OutgoingStream
incomingStreamTracks map[string]*IncomingStreamTrack
outgoingStreamTracks map[string]*OutgoingStreamTrack
iceStats *ICEStats
senderSideListener senderSideEstimatorListener
dtlsICEListener dtlsICETransportListener
outDTLSStateListener DTLSStateListener
onIncomingTrackListeners []IncomingTrackListener
onOutgoingTrackListeners []OutgoingTrackListener
sync.Mutex
}
// NewTransport create a new transport
func NewTransport(bundle native.RTPBundleTransport, remoteIce *sdp.ICEInfo, remoteDtls *sdp.DTLSInfo, remoteCandidates []*sdp.CandidateInfo,
localIce *sdp.ICEInfo, localDtls *sdp.DTLSInfo, localCandidates []*sdp.CandidateInfo, disableSTUNKeepAlive bool) *Transport {
transport := new(Transport)
transport.remoteIce = remoteIce
transport.remoteDtls = remoteDtls
transport.localIce = localIce
transport.localDtls = localDtls
transport.bundle = bundle
transport.dtlsState = "new"
properties := native.NewPropertiesFacade()
properties.SetPropertyStr("ice.localUsername", localIce.GetUfrag())
properties.SetPropertyStr("ice.localPassword", localIce.GetPassword())
properties.SetPropertyStr("ice.remoteUsername", remoteIce.GetUfrag())
properties.SetPropertyStr("ice.remotePassword", remoteIce.GetPassword())
properties.SetPropertyStr("dtls.setup", remoteDtls.GetSetup().String())
properties.SetPropertyStr("dtls.hash", remoteDtls.GetHash())
properties.SetPropertyStr("dtls.fingerprint", remoteDtls.GetFingerprint())
stunKeepAlive := false
if disableSTUNKeepAlive {
stunKeepAlive = true
}
properties.SetPropertyBool("disableSTUNKeepAlive", stunKeepAlive)
transport.username = localIce.GetUfrag() + ":" + remoteIce.GetUfrag()
transport.connection = bundle.AddICETransport(transport.username, properties)
transport.transport = transport.connection.GetTransport()
transport.iceStats = &ICEStats{}
native.DeletePropertiesFacade(properties)
sseListener := &overwrittenSenderSideEstimatorListener{}
p := native.NewDirectorSenderSideEstimatorListener(sseListener)
sseListener.p = p
transport.senderSideListener = &goSenderSideEstimatorListener{SenderSideEstimatorListener: p}
transport.transport.SetSenderSideEstimatorListener(transport.senderSideListener)
dtlsListener := &overwrittenDTLSICETransportListener{}
dtlsl := native.NewDirectorDTLSICETransportListener(dtlsListener)
dtlsListener.p = dtlsl
transport.dtlsICEListener = &goDTLSICETransportListener{DTLSICETransportListener: dtlsl}
transport.transport.SetListener(transport.dtlsICEListener)
var address string
var port int
for _, candidate := range remoteCandidates {
if candidate.GetType() == "relay" {
address = candidate.GetRelAddr()
port = candidate.GetRelPort()
} else {
address = candidate.GetAddress()
port = candidate.GetPort()
}
bundle.AddRemoteCandidate(transport.username, address, uint16(port))
}
transport.localCandidates = localCandidates
transport.remoteCandidates = remoteCandidates
transport.incomingStreams = make(map[string]*IncomingStream)
transport.outgoingStreams = make(map[string]*OutgoingStream)
transport.incomingStreamTracks = make(map[string]*IncomingStreamTrack)
transport.outgoingStreamTracks = make(map[string]*OutgoingStreamTrack)
transport.onIncomingTrackListeners = make([]IncomingTrackListener, 0)
transport.onOutgoingTrackListeners = make([]OutgoingTrackListener, 0)
return transport
}
// Dump dump incoming and outgoint rtp and rtcp packets into a pcap file
func (t *Transport) Dump(filename string, incoming bool, outgoing bool, rtcp bool) bool {
ret := t.transport.Dump(filename, incoming, outgoing, rtcp)
if ret == 0 {
return false
}
return true
}
// SetBandwidthProbing Enable/Disable bitrate probing
// This will send padding only RTX packets to allow bandwidth estimation algortithm to probe bitrate beyonf current sent values.
// The ammoung of probing bitrate would be limited by the sender bitrate estimation and the limit set on the setMaxProbing Bitrate.
func (t *Transport) SetBandwidthProbing(probe bool) {
t.transport.SetBandwidthProbing(probe)
}
// SetMaxProbingBitrate Set the maximum bitrate to be used if probing is enabled.
func (t *Transport) SetMaxProbingBitrate(bitrate uint) {
t.transport.SetMaxProbingBitrate(bitrate)
}
// GetDTLSState get dtls state
func (t *Transport) GetDTLSState() string {
return t.dtlsState
}
// GetICEStats get ice stats
func (t *Transport) GetICEStats() *ICEStats {
t.iceStats.RequestsSent = t.connection.GetIceRequestsSent()
t.iceStats.RequestsReceived = t.connection.GetIceRequestsReceived()
t.iceStats.ResponsesSent = t.connection.GetIceResponsesSent()
t.iceStats.ResponsesReceived = t.connection.GetIceResponsesReceived()
return t.iceStats
}
// SetRemoteProperties Set remote RTP properties
func (t *Transport) SetRemoteProperties(audio *sdp.MediaInfo, video *sdp.MediaInfo) {
properties := native.NewPropertiesFacade()
defer native.DeletePropertiesFacade(properties)
if audio != nil {
num := 0
for _, codec := range audio.GetCodecs() {
item := fmt.Sprintf("audio.codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("audio.codecs.length", num)
num = 0
for id, uri := range audio.GetExtensions() {
item := fmt.Sprintf("audio.ext.%d", num)
properties.SetPropertyInt(item+".id", id)
properties.SetPropertyStr(item+".uri", uri)
num = num + 1
}
properties.SetPropertyInt("audio.ext.length", num)
}
if video != nil {
num := 0
for _, codec := range video.GetCodecs() {
item := fmt.Sprintf("video.codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("video.codecs.length", num)
num = 0
for id, uri := range video.GetExtensions() {
item := fmt.Sprintf("video.ext.%d", num)
properties.SetPropertyInt(item+".id", id)
properties.SetPropertyStr(item+".uri", uri)
num = num + 1
}
properties.SetPropertyInt("video.ext.length", num)
}
t.transport.SetRemoteProperties(properties)
}
// SetLocalProperties Set local RTP properties
func (t *Transport) SetLocalProperties(audio *sdp.MediaInfo, video *sdp.MediaInfo) {
properties := native.NewPropertiesFacade()
defer native.DeletePropertiesFacade(properties)
if audio != nil {
num := 0
for _, codec := range audio.GetCodecs() {
item := fmt.Sprintf("audio.codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("audio.codecs.length", num)
num = 0
for id, uri := range audio.GetExtensions() {
item := fmt.Sprintf("audio.ext.%d", num)
properties.SetPropertyInt(item+".id", id)
properties.SetPropertyStr(item+".uri", uri)
num = num + 1
}
properties.SetPropertyInt("audio.ext.length", num)
}
if video != nil {
num := 0
for _, codec := range video.GetCodecs() {
item := fmt.Sprintf("video.codecs.%d", num)
properties.SetPropertyStr(item+".codec", codec.GetCodec())
properties.SetPropertyInt(item+".pt", codec.GetType())
if codec.HasRTX() {
properties.SetPropertyInt(item+".rtx", codec.GetRTX())
}
num = num + 1
}
properties.SetPropertyInt("video.codecs.length", num)
num = 0
for id, uri := range video.GetExtensions() {
item := fmt.Sprintf("video.ext.%d", num)
properties.SetPropertyInt(item+".id", id)
properties.SetPropertyStr(item+".uri", uri)
num = num + 1
}
properties.SetPropertyInt("video.ext.length", num)
}
t.transport.SetLocalProperties(properties)
}
// GetLocalDTLSInfo Get transport local DTLS info
func (t *Transport) GetLocalDTLSInfo() *sdp.DTLSInfo {
return t.localDtls
}
// GetLocalICEInfo Get transport local ICE info
func (t *Transport) GetLocalICEInfo() *sdp.ICEInfo {
return t.localIce
}
// GetLocalCandidates Get local ICE candidates for this transport
func (t *Transport) GetLocalCandidates() []*sdp.CandidateInfo {
return t.localCandidates
}
// GetRemoteCandidates Get remote ICE candidates for this transport
func (t *Transport) GetRemoteCandidates() []*sdp.CandidateInfo {
return t.remoteCandidates
}
// AddRemoteCandidate register a remote candidate info. Only needed for ice-lite to ice-lite endpoints
func (t *Transport) AddRemoteCandidate(candidate *sdp.CandidateInfo) {
var address string
var port int
if candidate.GetType() == "relay" {
address = candidate.GetRelAddr()
port = candidate.GetRelPort()
} else {
address = candidate.GetAddress()
port = candidate.GetPort()
}
if t.bundle.AddRemoteCandidate(t.username, address, uint16(port)) != 0 {
return
}
t.remoteCandidates = append(t.remoteCandidates, candidate)
}
// CreateOutgoingStream Create new outgoing stream in this transport using StreamInfo
func (t *Transport) CreateOutgoingStream(streamInfo *sdp.StreamInfo) *OutgoingStream {
if _, ok := t.outgoingStreams[streamInfo.GetID()]; ok {
return nil
}
info := streamInfo.Clone()
outgoingStream := NewOutgoingStream(t.transport, info)
t.Lock()
t.outgoingStreams[outgoingStream.GetID()] = outgoingStream
t.Unlock()
outgoingStream.OnTrack(func(track *OutgoingStreamTrack) {
for _, trackFunc := range t.onOutgoingTrackListeners {
trackFunc(track, outgoingStream)
}
})
for _, track := range outgoingStream.GetTracks() {
for _, trackFunc := range t.onOutgoingTrackListeners {
trackFunc(track, outgoingStream)
}
}
return outgoingStream
}
// CreateOutgoingStreamWithID alias CreateOutgoingStream
func (t *Transport) CreateOutgoingStreamWithID(streamID string, audio bool, video bool) *OutgoingStream {
streamInfo := sdp.NewStreamInfo(streamID)
if audio {
audioTrack := sdp.NewTrackInfo(uuid.Must(uuid.NewV4()).String(), "audio")
ssrc := NextSSRC()
audioTrack.AddSSRC(ssrc)
streamInfo.AddTrack(audioTrack)
}
if video {
videoTrack := sdp.NewTrackInfo(uuid.Must(uuid.NewV4()).String(), "video")
ssrc := NextSSRC()
videoTrack.AddSSRC(ssrc)
streamInfo.AddTrack(videoTrack)
}
stream := t.CreateOutgoingStream(streamInfo)
return stream
}
// CreateOutgoingStreamTrack Create new outgoing track in this transport
func (t *Transport) CreateOutgoingStreamTrack(media string, trackId string, ssrcs map[string]uint) *OutgoingStreamTrack {
var mediaType native.MediaFrameType = 0
if media == "video" {
mediaType = 1
}
if trackId == "" {
trackId = uuid.Must(uuid.NewV4()).String()
}
source := native.NewRTPOutgoingSourceGroup(mediaType)
if ssrc, ok := ssrcs["media"]; ok {
source.GetMedia().SetSsrc(ssrc)
} else {
source.GetMedia().SetSsrc(NextSSRC())
}
if ssrc, ok := ssrcs["rtx"]; ok {
source.GetRtx().SetSsrc(ssrc)
} else {
source.GetRtx().SetSsrc(NextSSRC())
}
if ssrc, ok := ssrcs["fec"]; ok {
source.GetFec().SetSsrc(ssrc)
} else {
source.GetFec().SetSsrc(NextSSRC())
}
// todo error handle
t.transport.AddOutgoingSourceGroup(source)
outgoingTrack := newOutgoingStreamTrack(media, trackId, native.TransportToSender(t.transport), source)
for _, trackFunc := range t.onOutgoingTrackListeners {
trackFunc(outgoingTrack, nil)
}
return outgoingTrack
}
// CreateIncomingStream Create an incoming stream object from the media stream info objet
func (t *Transport) CreateIncomingStream(streamInfo *sdp.StreamInfo) *IncomingStream {
if _, ok := t.incomingStreams[streamInfo.GetID()]; ok {
return nil
}
incomingStream := newIncomingStream(t.transport, native.TransportToReceiver(t.transport), streamInfo)
t.Lock()
t.incomingStreams[incomingStream.GetID()] = incomingStream
t.Unlock()
return incomingStream
}
// CreateIncomingStreamTrack Create new incoming stream in this transport. TODO: Simulcast is still not supported
// You can use IncomingStream's CreateTrack
func (t *Transport) CreateIncomingStreamTrack(media string, trackId string, ssrcs map[string]uint) *IncomingStreamTrack {
var mediaType native.MediaFrameType = 0
if media == "video" {
mediaType = 1
}
if trackId == "" {
trackId = uuid.Must(uuid.NewV4()).String()
}
source := native.NewRTPIncomingSourceGroup(mediaType, t.transport.GetTimeService())
if ssrc, ok := ssrcs["media"]; ok {
source.GetMedia().SetSsrc(ssrc)
} else {
source.GetMedia().SetSsrc(NextSSRC())
}
if ssrc, ok := ssrcs["rtx"]; ok {
source.GetRtx().SetSsrc(ssrc)
} else {
source.GetRtx().SetSsrc(NextSSRC())
}
if ssrc, ok := ssrcs["fec"]; ok {
source.GetFec().SetSsrc(ssrc)
} else {
source.GetFec().SetSsrc(NextSSRC())
}
t.transport.AddIncomingSourceGroup(source)
sources := map[string]native.RTPIncomingSourceGroup{"": source}
incomingTrack := NewIncomingStreamTrack(media, trackId, native.TransportToReceiver(t.transport), sources)
for _, trackFunc := range t.onIncomingTrackListeners {
trackFunc(incomingTrack, nil)
}
return incomingTrack
}
func (t *Transport) RemoveIncomingStream(incomingStream *IncomingStream) {
t.Lock()
delete(t.incomingStreams, incomingStream.GetID())
t.Unlock()
}
// GetIncomingStreams get all incoming streams
func (t *Transport) GetIncomingStreams() []*IncomingStream {
incomings := []*IncomingStream{}
for _, stream := range t.incomingStreams {
incomings = append(incomings, stream)
}
return incomings
}
// GetIncomingStream get one incoming stream
func (t *Transport) GetIncomingStream(streamId string) *IncomingStream {
t.Lock()
defer t.Unlock()
return t.incomingStreams[streamId]
}
// GetOutgoingStreams get all outgoing streams
func (t *Transport) GetOutgoingStreams() []*OutgoingStream {
outgoings := []*OutgoingStream{}
for _, stream := range t.outgoingStreams {
outgoings = append(outgoings, stream)
}
return outgoings
}
// GetOutgoingStream get one outgoing stream
func (t *Transport) GetOutgoingStream(streamId string) *OutgoingStream {
t.Lock()
defer t.Unlock()
return t.outgoingStreams[streamId]
}
// OnIncomingTrack register incoming track
func (t *Transport) OnIncomingTrack(listener IncomingTrackListener) {
t.Lock()
defer t.Unlock()
t.onIncomingTrackListeners = append(t.onIncomingTrackListeners, listener)
}
// OnOutgoingTrack register outgoing track
func (t *Transport) OnOutgoingTrack(listener OutgoingTrackListener) {
t.Lock()
defer t.Unlock()
t.onOutgoingTrackListeners = append(t.onOutgoingTrackListeners, listener)
}
// OnDTLSICEState OnDTLSICEState
func (t *Transport) OnDTLSICEState(listener DTLSStateListener) {
t.Lock()
defer t.Unlock()
t.outDTLSStateListener = listener
}
func (t *Transport) GetLastActiveTime() uint64 {
return t.transport.GetLastActiveTime()
}
// Stop stop this transport
func (t *Transport) Stop() {
if t.bundle == nil {
return
}
for _, incoming := range t.incomingStreams {
incoming.Stop()
}
for _, outgoing := range t.outgoingStreams {
outgoing.Stop()
}
if t.senderSideListener != nil {
t.senderSideListener.deleteSenderSideEstimatorListener()
t.senderSideListener = nil
}
if t.dtlsICEListener != nil {
t.dtlsICEListener.deleteDTLSICETransportListener()
t.dtlsICEListener = nil
}
t.bundle.RemoveICETransport(t.username)
t.incomingStreams = nil
t.outgoingStreams = nil
t.connection = nil
t.transport = nil
t.username = ""
t.bundle = nil
}