forked from inlivedev/sfu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packetbuffers.go
505 lines (403 loc) · 12.7 KB
/
packetbuffers.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
package sfu
import (
"container/list"
"context"
"errors"
"sort"
"sync"
"time"
"github.com/inlivedev/sfu/pkg/rtppool"
"github.com/pion/logging"
)
var (
ErrPacketTooLate = errors.New("packetbuffers: packet is too late")
ErrPacketDuplicate = errors.New("packetbuffers: packet is duplicate")
)
type Packet struct {
Packet *rtppool.RetainablePacket
addedTime time.Time
}
// buffer ring for cached packets
type PacketBuffers struct {
context context.Context
init bool
mu sync.RWMutex
buffers *list.List
lastSequenceNumber uint16
latencyMu sync.RWMutex
// min duration to wait before sending
minLatency time.Duration
// max duration to wait before sending
maxLatency time.Duration
oldestPacket *Packet
initSequence uint16
packetCount uint64
waitTimeMu sync.RWMutex
waitTimes []time.Duration
previousAddedTime time.Time
lastSequenceWaitTime uint16
waitTimeResetCounter uint16
packetAvailableWait *sync.Cond
enableDynamicLatency bool
ended bool
log logging.LeveledLogger
}
const waitTimeSize = 2500
func NewPacketBuffers(ctx context.Context, minLatency, maxLatency time.Duration, dynamicLatency bool, log logging.LeveledLogger) *PacketBuffers {
ctx, cancel := context.WithCancel(ctx)
p := &PacketBuffers{
context: ctx,
mu: sync.RWMutex{},
buffers: list.New(),
latencyMu: sync.RWMutex{},
minLatency: minLatency,
maxLatency: maxLatency,
waitTimeMu: sync.RWMutex{},
waitTimes: make([]time.Duration, waitTimeSize),
lastSequenceWaitTime: 0,
packetAvailableWait: sync.NewCond(&sync.Mutex{}),
enableDynamicLatency: dynamicLatency,
log: log,
}
go func() {
defer p.Close()
defer cancel()
<-ctx.Done()
}()
return p
}
func (p *PacketBuffers) MaxLatency() time.Duration {
p.latencyMu.RLock()
defer p.latencyMu.RUnlock()
return p.maxLatency
}
func (p *PacketBuffers) MinLatency() time.Duration {
p.latencyMu.RLock()
defer p.latencyMu.RUnlock()
return p.minLatency
}
func (p *PacketBuffers) Initiated() bool {
p.mu.RLock()
defer p.mu.RUnlock()
return p.init
}
func (p *PacketBuffers) Add(pkt *rtppool.RetainablePacket) error {
p.mu.Lock()
defer func() {
p.checkOrderedPacketAndRecordTimes()
if p.enableDynamicLatency {
p.checkWaitTimeAdjuster()
}
p.mu.Unlock()
}()
if p.init &&
(p.lastSequenceNumber == pkt.Header().SequenceNumber || IsRTPPacketLate(pkt.Header().SequenceNumber, p.lastSequenceNumber)) {
p.log.Warnf("packet cache: packet sequence ", pkt.Header().SequenceNumber, " is too late, last sent was ", p.lastSequenceNumber, ", will not adding the packet")
// add to to front of the list to make sure pop take it first
p.buffers.PushFront(&Packet{
Packet: pkt,
addedTime: time.Now(),
})
return ErrPacketTooLate
}
if p.buffers.Len() == 0 {
p.buffers.PushBack(&Packet{
Packet: pkt,
addedTime: time.Now(),
})
return nil
}
// add packet in order
Loop:
for e := p.buffers.Back(); e != nil; e = e.Prev() {
currentpkt := e.Value.(*Packet)
if err := currentpkt.Packet.Retain(); err != nil {
// already released
continue
}
if currentpkt.Packet.Header().SequenceNumber == pkt.Header().SequenceNumber {
// p.log.Warnf("packet cache: packet sequence ", pkt.SequenceNumber, " already exists in the cache, will not adding the packet")
currentpkt.Packet.Release()
return ErrPacketDuplicate
}
if currentpkt.Packet.Header().SequenceNumber < pkt.Header().SequenceNumber && pkt.Header().SequenceNumber-currentpkt.Packet.Header().SequenceNumber < uint16SizeHalf {
p.buffers.InsertAfter(&Packet{
Packet: pkt,
addedTime: time.Now(),
}, e)
currentpkt.Packet.Release()
break Loop
}
if currentpkt.Packet.Header().SequenceNumber-pkt.Header().SequenceNumber > uint16SizeHalf {
p.buffers.InsertAfter(&Packet{
Packet: pkt,
addedTime: time.Now(),
}, e)
currentpkt.Packet.Release()
break Loop
}
if e.Prev() == nil {
p.buffers.PushFront(&Packet{
Packet: pkt,
addedTime: time.Now(),
})
currentpkt.Packet.Release()
break Loop
}
currentpkt.Packet.Release()
}
return nil
}
func (p *PacketBuffers) pop(el *list.Element) *Packet {
pkt := el.Value.(*Packet)
if err := pkt.Packet.Retain(); err != nil {
// already released
return nil
}
defer func() {
pkt.Packet.Release()
}()
// make sure packet is not late
if IsRTPPacketLate(pkt.Packet.Header().SequenceNumber, p.lastSequenceNumber) {
p.log.Warnf("packet cache: packet sequence ", pkt.Packet.Header().SequenceNumber, " is too late, last sent was ", p.lastSequenceNumber)
}
if p.init && pkt.Packet.Header().SequenceNumber > p.lastSequenceNumber && pkt.Packet.Header().SequenceNumber-p.lastSequenceNumber > 1 {
// make sure packet has no gap
p.log.Warnf("packet cache: packet sequence ", pkt.Packet.Header().SequenceNumber, " has a gap with last sent ", p.lastSequenceNumber)
}
p.mu.Lock()
p.lastSequenceNumber = pkt.Packet.Header().SequenceNumber
p.packetCount++
p.mu.Unlock()
if p.oldestPacket != nil && p.oldestPacket.Packet.Header().SequenceNumber == pkt.Packet.Header().SequenceNumber {
// oldest packet will be remove, find the next oldest packet in the list
p.mu.RLock()
for e := el.Next(); e != nil; e = e.Next() {
packet := e.Value.(*Packet)
if packet.addedTime.After(p.oldestPacket.addedTime) {
p.oldestPacket = packet
}
}
p.mu.RUnlock()
}
// remove the packets from the cache
p.mu.Lock()
p.buffers.Remove(el)
p.mu.Unlock()
return pkt
}
func (p *PacketBuffers) flush() []*Packet {
packets := make([]*Packet, 0)
if p.oldestPacket != nil && time.Since(p.oldestPacket.addedTime) > p.maxLatency {
// we have waited too long, we should send the packets
packets = append(packets, p.sendOldestPacket())
}
Loop:
for e := p.buffers.Front(); p.buffers.Front() != nil; e = p.buffers.Front() {
pkt := p.fetch(e)
if pkt != nil {
packets = append(packets, pkt)
} else {
break Loop
}
}
return packets
}
func (p *PacketBuffers) sendOldestPacket() *Packet {
for e := p.buffers.Front(); e != nil; e = e.Next() {
packet := e.Value.(*Packet)
if packet.Packet.Header().SequenceNumber == p.oldestPacket.Packet.Header().SequenceNumber {
return p.pop(e)
}
}
return nil
}
func (p *PacketBuffers) fetch(e *list.Element) *Packet {
p.latencyMu.RLock()
maxLatency := p.maxLatency
minLatency := p.minLatency
p.latencyMu.RUnlock()
currentPacket := e.Value.(*Packet)
currentSeq := currentPacket.Packet.Header().SequenceNumber
latency := time.Since(currentPacket.addedTime)
if !p.Initiated() && latency > minLatency && e.Next() != nil && !IsRTPPacketLate(e.Next().Value.(*Packet).Packet.Header().SequenceNumber, currentSeq) {
// first packet to send, but make sure we have the packet in order
p.mu.Lock()
p.initSequence = currentSeq
p.init = true
p.mu.Unlock()
return p.pop(e)
}
if (p.lastSequenceNumber < currentSeq || p.lastSequenceNumber-currentSeq > uint16SizeHalf) && currentSeq-p.lastSequenceNumber == 1 {
// the current packet is in sequence with the last packet we popped
if time.Since(currentPacket.addedTime) > minLatency {
// passed the min latency
return p.pop(e)
}
}
// there is a gap between the last packet we popped and the current packet
// we should wait for the next packet
// but check with the latency if there is a packet pass the max latency
// p.log.Infof("packet latency: ", packetLatency, " gap: ", gap, " currentSeq: ", currentSeq, " nextSeq: ", nextSeq)
if latency > maxLatency {
// we have waited too long, we should send the packets
p.log.Warnf("packet cache: packet sequence ", currentPacket.Packet.Header().SequenceNumber, " latency ", latency, ", reached max latency ", maxLatency, ", will sending the packets")
return p.pop(e)
}
return nil
}
func (p *PacketBuffers) Pop() *Packet {
p.mu.RLock()
if p.oldestPacket != nil && time.Since(p.oldestPacket.addedTime) > p.maxLatency {
p.mu.RUnlock()
// we have waited too long, we should send the packets
return p.sendOldestPacket()
}
frontElement := p.buffers.Front()
p.mu.RUnlock()
if frontElement == nil {
return nil
}
item := frontElement.Value.(*Packet)
if err := item.Packet.Retain(); err != nil {
return nil
}
defer func() {
item.Packet.Release()
}()
if IsRTPPacketLate(item.Packet.Header().SequenceNumber, p.lastSequenceNumber) {
return p.pop(frontElement)
}
return p.fetch(frontElement)
}
func (p *PacketBuffers) Flush() []*Packet {
return p.flush()
}
func (p *PacketBuffers) Last() *Packet {
p.mu.RLock()
defer p.mu.RUnlock()
if p.buffers.Len() == 0 {
return nil
}
return p.buffers.Back().Value.(*Packet)
}
func (p *PacketBuffers) Len() int {
p.mu.RLock()
defer p.mu.RUnlock()
return p.buffers.Len()
}
func (p *PacketBuffers) Clear() {
p.mu.Lock()
defer p.mu.Unlock()
for e := p.buffers.Front(); e != nil; e = e.Next() {
packet := e.Value.(*Packet)
packet.Packet.Release()
p.buffers.Remove(e)
}
p.buffers.Init()
p.oldestPacket = nil
}
func (p *PacketBuffers) Close() {
p.mu.Lock()
p.ended = true
p.mu.Unlock()
p.Clear()
// make sure we don't have any waiters
p.packetAvailableWait.Signal()
}
func (p *PacketBuffers) WaitAvailablePacket() {
p.mu.RLock()
if p.buffers.Len() == 0 {
p.mu.RUnlock()
return
}
if p.ended {
return
}
p.mu.RUnlock()
p.packetAvailableWait.L.Lock()
defer p.packetAvailableWait.L.Unlock()
p.packetAvailableWait.Wait()
}
func (p *PacketBuffers) checkOrderedPacketAndRecordTimes() {
for e := p.buffers.Front(); e != nil; e = e.Next() {
pkt := e.Value.(*Packet)
// make sure call retain to prevent packet from being released when we are still using it
if err := pkt.Packet.Retain(); err != nil {
// already released
continue
}
currentSeq := pkt.Packet.Header().SequenceNumber
latency := time.Since(pkt.addedTime)
if !p.init && latency > p.minLatency && e.Next() != nil && !IsRTPPacketLate(e.Next().Value.(*Packet).Packet.Header().SequenceNumber, currentSeq) {
// signal first packet to send
p.packetAvailableWait.Signal()
} else if (p.lastSequenceNumber < currentSeq || p.lastSequenceNumber-currentSeq > uint16SizeHalf) && currentSeq-p.lastSequenceNumber == 1 {
// the current packet is in sequence with the last packet we popped
p.recordWaitTime(e)
if time.Since(pkt.addedTime) > p.minLatency {
// passed the min latency
p.packetAvailableWait.Signal()
}
} else if latency > p.maxLatency {
// passed the max latency
p.packetAvailableWait.Signal()
}
// release the packet after we are done with it
pkt.Packet.Release()
}
}
func (p *PacketBuffers) recordWaitTime(el *list.Element) {
p.waitTimeMu.Lock()
defer p.waitTimeMu.Unlock()
pkt := el.Value.(*Packet)
if p.lastSequenceWaitTime == pkt.Packet.Header().SequenceNumber || IsRTPPacketLate(pkt.Packet.Header().SequenceNumber, p.lastSequenceWaitTime) || p.previousAddedTime.IsZero() {
// don't record late packet or already recorded packet
if p.previousAddedTime.IsZero() {
p.previousAddedTime = pkt.addedTime
}
return
}
p.lastSequenceWaitTime = pkt.Packet.Header().SequenceNumber
// remove oldest packet from the wait times if more than 500
if uint16(len(p.waitTimes)+1) > waitTimeSize {
p.waitTimes = p.waitTimes[1:]
}
gapTime := pkt.addedTime.Sub(p.previousAddedTime)
p.previousAddedTime = pkt.addedTime
p.waitTimes = append(p.waitTimes, gapTime)
}
func (p *PacketBuffers) checkWaitTimeAdjuster() {
if p.waitTimeResetCounter > waitTimeSize {
p.waitTimeMu.Lock()
defer p.waitTimeMu.Unlock()
// calculate the 75th percentile of the wait times
// sort the wait times
sortedWaitTimes := make([]time.Duration, len(p.waitTimes))
copy(sortedWaitTimes, p.waitTimes)
sort.Slice(sortedWaitTimes, func(i, j int) bool {
return sortedWaitTimes[i] < sortedWaitTimes[j]
})
// get the 75th percentile
// percentile90thIndex := int(float64(len(sortedWaitTimes)) * 0.90)
percentileIndex := int(float64(len(sortedWaitTimes)) * 0.7)
// percentile60thIndex := int(float64(len(sortedWaitTimes)) * 0.60)
percentile := sortedWaitTimes[percentileIndex]
if percentile > p.minLatency && percentile < p.maxLatency {
// increase the min latency
p.log.Infof("packet cache: set min latency ", percentile, ", increasing min latency from ", p.minLatency)
p.latencyMu.Lock()
p.minLatency = percentile
p.latencyMu.Unlock()
} else if percentile < p.minLatency && percentile > 0 {
// decrease the min latency
p.log.Infof("packet cache: set min latency ", percentile, ", decreasing min latency from ", p.minLatency)
p.latencyMu.Lock()
p.minLatency = percentile
p.latencyMu.Unlock()
}
p.waitTimeResetCounter = 0
} else {
p.waitTimeResetCounter++
}
}