-
Notifications
You must be signed in to change notification settings - Fork 119
/
incomingstreamtrack.go
511 lines (412 loc) · 12.2 KB
/
incomingstreamtrack.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
package mediaserver
import (
"sort"
"strconv"
"time"
native "github.com/notedit/media-server-go/wrapper"
"github.com/notedit/sdp"
)
// Layer info
type Layer struct {
// EncodingId str
EncodingId string
// SpatialLayerId int
SpatialLayerId int
// TemporalLayerId int
TemporalLayerId int
// SimulcastIdx int
SimulcastIdx int
// TotalBytes uint
TotalBytes uint
// NumPackets uint
NumPackets uint
// Bitrate uint
Bitrate uint
}
// Encoding info
type Encoding struct {
id string
source native.RTPIncomingSourceGroup
depacketizer native.StreamTrackDepacketizer
}
// GetID encoding id
func (e *Encoding) GetID() string {
return e.id
}
// GetSource get native RTPIncomingSourceGroup
func (e *Encoding) GetSource() native.RTPIncomingSourceGroup {
return e.source
}
// GetDepacketizer get native StreamTrackDepacketizer
func (e *Encoding) GetDepacketizer() native.StreamTrackDepacketizer {
return e.depacketizer
}
// IncomingTrackStopListener stop listener
type IncomingTrackStopListener func()
// IncomingStreamTrack Audio or Video track of a remote media stream
type IncomingStreamTrack struct {
id string
media string
receiver native.RTPReceiverFacade
counter int
encodings []*Encoding
trackInfo *sdp.TrackInfo
stats map[string]*IncomingAllStats
mediaframeMultiplexer *MediaFrameMultiplexer
onStopListeners []func()
onAttachedListeners []func()
onDetachedListeners []func()
}
// IncomingStats info
type IncomingStats struct {
LostPackets uint
DropPackets uint
NumPackets uint
NumRTCPPackets uint
TotalBytes uint
TotalRTCPBytes uint
TotalPLIs uint
TotalNACKs uint
Bitrate uint
Layers []*Layer
}
// IncomingAllStats info
type IncomingAllStats struct {
Rtt uint
MinWaitTime uint
MaxWaitTime uint
AvgWaitTime float64
Media *IncomingStats
Rtx *IncomingStats
Fec *IncomingStats
Bitrate uint
Total uint
Remb uint
SimulcastIdx int
timestamp int64
}
// ActiveEncoding info
type ActiveEncoding struct {
EncodingId string
SimulcastIdx int
Bitrate uint
Layers []*Layer
}
// ActiveLayersInfo info
type ActiveLayersInfo struct {
Active []*ActiveEncoding
Inactive []*ActiveEncoding
Layers []*Layer
}
func getStatsFromIncomingSource(source native.RTPIncomingSource) *IncomingStats {
stats := &IncomingStats{
LostPackets: source.GetLostPackets(),
DropPackets: source.GetDropPackets(),
NumPackets: source.GetNumPackets(),
NumRTCPPackets: source.GetNumRTCPPackets(),
TotalBytes: source.GetTotalBytes(),
TotalRTCPBytes: source.GetTotalRTCPBytes(),
TotalPLIs: source.GetTotalPLIs(),
TotalNACKs: source.GetTotalNACKs(),
Bitrate: source.GetBitrate(),
Layers: []*Layer{},
}
layers := source.Layers()
individual := []*Layer{}
var i int64
for i = 0; i < layers.Size(); i++ {
layer := layers.Get(int64(i))
layerInfo := &Layer{
SpatialLayerId: int(layer.GetSpatialLayerId()),
TemporalLayerId: int(layer.GetTemporalLayerId()),
TotalBytes: layer.GetTotalBytes(),
NumPackets: layer.GetNumPackets(),
Bitrate: layer.GetBitrate(),
}
individual = append(individual, layerInfo)
}
for _, layer := range individual {
aggregated := &Layer{
SpatialLayerId: layer.SpatialLayerId,
TemporalLayerId: layer.TemporalLayerId,
TotalBytes: 0,
NumPackets: 0,
Bitrate: 0,
}
for _, layer2 := range individual {
if layer2.SpatialLayerId <= aggregated.SpatialLayerId && layer2.TemporalLayerId <= aggregated.TemporalLayerId {
aggregated.TotalBytes += layer2.TotalBytes
aggregated.NumPackets += layer2.NumPackets
aggregated.Bitrate += layer2.Bitrate
}
}
stats.Layers = append(stats.Layers, aggregated)
}
return stats
}
// NewIncomingStreamTrack Create incoming audio/video track
func NewIncomingStreamTrack(media string, id string, receiver native.RTPReceiverFacade, sources map[string]native.RTPIncomingSourceGroup) *IncomingStreamTrack {
track := &IncomingStreamTrack{}
track.id = id
track.media = media
track.receiver = receiver
track.counter = 0
track.encodings = make([]*Encoding, 0)
track.trackInfo = sdp.NewTrackInfo(id, media)
for k, source := range sources {
encoding := &Encoding{
id: k,
source: source,
depacketizer: native.NewStreamTrackDepacketizer(source),
}
track.encodings = append(track.encodings, encoding)
//Add ssrcs to track info
if source.GetMedia().GetSsrc() > 0 {
track.trackInfo.AddSSRC(source.GetMedia().GetSsrc())
}
if source.GetRtx().GetSsrc() > 0 {
track.trackInfo.AddSSRC(source.GetRtx().GetSsrc())
}
if source.GetFec().GetSsrc() > 0 {
track.trackInfo.AddSSRC(source.GetFec().GetSsrc())
}
//Add RTX and FEC groups
if source.GetRtx().GetSsrc() > 0 {
sourceGroup := sdp.NewSourceGroupInfo("FID", []uint{source.GetMedia().GetSsrc(), source.GetRtx().GetSsrc()})
track.trackInfo.AddSourceGroup(sourceGroup)
}
if source.GetFec().GetSsrc() > 0 {
sourceGroup := sdp.NewSourceGroupInfo("FEC-FR", []uint{source.GetMedia().GetSsrc(), source.GetFec().GetSsrc()})
track.trackInfo.AddSourceGroup(sourceGroup)
}
// if simulcast
if len(k) > 0 {
// make soure the pasused
encodingInfo := sdp.NewTrackEncodingInfo(k, false)
if source.GetMedia().GetSsrc() > 0 {
ssrc := strconv.FormatUint(uint64(source.GetMedia().GetSsrc()), 10)
encodingInfo.AddParam("ssrc", ssrc)
}
track.trackInfo.AddEncoding(encodingInfo)
}
}
track.onAttachedListeners = make([]func(), 0)
track.onDetachedListeners = make([]func(), 0)
track.onStopListeners = make([]func(), 0)
sort.SliceStable(track.encodings, func(i, j int) bool {
return track.encodings[i].id < track.encodings[j].id
})
return track
}
// GetID get track id
func (i *IncomingStreamTrack) GetID() string {
return i.id
}
// GetMedia get track media type "video" or "audio"
func (i *IncomingStreamTrack) GetMedia() string {
return i.media
}
// GetTrackInfo get track info
func (i *IncomingStreamTrack) GetTrackInfo() *sdp.TrackInfo {
return i.trackInfo
}
// GetSSRCs get all RTPIncomingSource include "media" "rtx" "fec"
func (i *IncomingStreamTrack) GetSSRCs() []map[string]native.RTPIncomingSource {
ssrcs := make([]map[string]native.RTPIncomingSource, 0)
for _, encoding := range i.encodings {
ssrcs = append(ssrcs, map[string]native.RTPIncomingSource{
"media": encoding.source.GetMedia(),
"rtx": encoding.source.GetRtx(),
"fec": encoding.source.GetFec(),
})
}
return ssrcs
}
// GetStats Get stats for all encodings
func (i *IncomingStreamTrack) GetStats() map[string]*IncomingAllStats {
if i.stats == nil {
i.stats = map[string]*IncomingAllStats{}
}
for _, encoding := range i.encodings {
state := i.stats[encoding.id]
if state == nil || (state != nil && time.Now().UnixNano()-state.timestamp > 200000000) {
encoding.GetSource().Update()
media := getStatsFromIncomingSource(encoding.GetSource().GetMedia())
fec := getStatsFromIncomingSource(encoding.GetSource().GetFec())
rtx := getStatsFromIncomingSource(encoding.GetSource().GetRtx())
i.stats[encoding.id] = &IncomingAllStats{
Rtt: encoding.GetSource().GetRtt(),
MinWaitTime: encoding.GetSource().GetMinWaitedTime(),
MaxWaitTime: encoding.GetSource().GetMaxWaitedTime(),
AvgWaitTime: encoding.GetSource().GetAvgWaitedTime(),
Media: media,
Rtx: rtx,
Fec: fec,
Bitrate: media.Bitrate,
Total: media.Bitrate + fec.Bitrate + rtx.Bitrate,
timestamp: time.Now().UnixNano(),
}
}
}
simulcastIdx := 0
stats := []*IncomingAllStats{}
for _, state := range i.stats {
stats = append(stats, state)
}
sort.Slice(stats, func(i, j int) bool { return stats[i].Bitrate > stats[j].Bitrate })
for _, state := range stats {
if state.Bitrate > 0 {
simulcastIdx += 1
state.SimulcastIdx = simulcastIdx
} else {
state.SimulcastIdx = -1
}
for _, layer := range state.Media.Layers {
layer.SimulcastIdx = state.SimulcastIdx
}
}
return i.stats
}
// GetActiveLayers Get active encodings and layers ordered by bitrate
func (i *IncomingStreamTrack) GetActiveLayers() *ActiveLayersInfo {
active := []*ActiveEncoding{}
inactive := []*ActiveEncoding{}
all := []*Layer{}
stats := i.GetStats()
for id, state := range stats {
if state.Bitrate == 0 {
inactive = append(inactive, &ActiveEncoding{
EncodingId: id,
})
continue
}
encoding := &ActiveEncoding{
EncodingId: id,
SimulcastIdx: state.SimulcastIdx,
Bitrate: state.Bitrate,
Layers: []*Layer{},
}
layers := state.Media.Layers
for _, layer := range layers {
encoding.Layers = append(encoding.Layers, &Layer{
SimulcastIdx: layer.SimulcastIdx,
SpatialLayerId: layer.SpatialLayerId,
TemporalLayerId: layer.TemporalLayerId,
Bitrate: layer.Bitrate,
})
all = append(all, &Layer{
EncodingId: id,
SimulcastIdx: layer.SimulcastIdx,
SpatialLayerId: layer.SpatialLayerId,
TemporalLayerId: layer.TemporalLayerId,
Bitrate: layer.Bitrate,
})
}
if len(encoding.Layers) > 0 {
sort.SliceStable(encoding.Layers, func(i, j int) bool { return encoding.Layers[i].Bitrate < encoding.Layers[j].Bitrate })
} else {
all = append(all, &Layer{
EncodingId: encoding.EncodingId,
SimulcastIdx: encoding.SimulcastIdx,
SpatialLayerId: MaxLayerId,
TemporalLayerId: MaxLayerId,
Bitrate: encoding.Bitrate,
})
}
active = append(active, encoding)
}
if len(active) > 0 {
sort.Slice(active, func(i, j int) bool { return active[i].Bitrate < active[j].Bitrate })
}
if len(all) > 0 {
sort.Slice(all, func(i, j int) bool { return all[i].Bitrate < all[j].Bitrate })
}
return &ActiveLayersInfo{
Active: active,
Inactive: inactive,
Layers: all,
}
}
// GetEncodings get all encodings
func (i *IncomingStreamTrack) GetEncodings() []*Encoding {
return i.encodings
}
// GetFirstEncoding get the first Encoding
func (i *IncomingStreamTrack) GetFirstEncoding() *Encoding {
for _, encoding := range i.encodings {
if encoding != nil {
return encoding
}
}
return nil
}
// GetEncoding get Encoding by id
func (i *IncomingStreamTrack) GetEncoding(encodingID string) *Encoding {
for _, encoding := range i.encodings {
if encoding.id == encodingID {
return encoding
}
}
return nil
}
// Attached Signal that this track has been attached.
func (i *IncomingStreamTrack) Attached() {
i.counter = i.counter + 1
if i.counter == 1 {
for _, attach := range i.onAttachedListeners {
attach()
}
}
}
// Refresh Request an intra refres
func (i *IncomingStreamTrack) Refresh() {
for _, encoding := range i.encodings {
//Request an iframe on main ssrc
i.receiver.SendPLI(encoding.source.GetMedia().GetSsrc())
}
}
// Detached Signal that this track has been detached.
func (i *IncomingStreamTrack) Detached() {
i.counter = i.counter - 1
if i.counter == 0 {
for _, detach := range i.onDetachedListeners {
detach()
}
}
}
// OnDetach
func (i *IncomingStreamTrack) OnDetach(detach func()) {
i.onDetachedListeners = append(i.onDetachedListeners, detach)
}
// OnAttach run this func when attached
func (i *IncomingStreamTrack) OnAttach(attach func()) {
i.onAttachedListeners = append(i.onAttachedListeners, attach)
}
// OnMediaFrame callback
func (i *IncomingStreamTrack) OnMediaFrame(listener func([]byte, uint64)) {
if i.mediaframeMultiplexer == nil {
i.mediaframeMultiplexer = NewMediaFrameMultiplexer(i)
}
i.mediaframeMultiplexer.SetMediaFrameListener(listener)
}
// Stop Removes the track from the incoming stream and also detaches any attached outgoing track or recorder
func (i *IncomingStreamTrack) Stop() {
if i.receiver == nil {
return
}
if i.mediaframeMultiplexer != nil {
i.mediaframeMultiplexer.Stop()
i.mediaframeMultiplexer = nil
}
for _, encoding := range i.encodings {
if encoding.depacketizer != nil {
encoding.depacketizer.Stop()
native.DeleteStreamTrackDepacketizer(encoding.depacketizer)
}
if encoding.source != nil {
native.DeleteRTPIncomingSourceGroup(encoding.source)
}
}
i.encodings = nil
i.receiver = nil
}