* Thottle the publisher data channel sending when subscriber is slow Avoid the publisher overwhelm the sfu data channel buffer when the subscriber has lower receive bitrates. It will drop message if the subscriber is considered too slow to block the entire room. * Enable nack in mediaengine and disable it in transceiver as need pion doesn't support per transciver codec configuration, so the nack of this session will be disabled forever once it is first disabled by a transceiver. https://github.com/pion/webrtc/pull/2972
94 lines
1.9 KiB
Go
94 lines
1.9 KiB
Go
package datachannel
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/gammazero/deque"
|
|
|
|
"github.com/livekit/protocol/utils/mono"
|
|
)
|
|
|
|
const (
|
|
BitrateDuration = 2 * time.Second
|
|
BitrateWindow = 100 * time.Millisecond
|
|
)
|
|
|
|
// BitrateCalculator calculates bitrate over sliding window
|
|
type BitrateCalculator struct {
|
|
lock sync.Mutex
|
|
windowDuration time.Duration
|
|
duration time.Duration
|
|
|
|
windows *deque.Deque[bitrateWindow]
|
|
active bitrateWindow
|
|
|
|
bytes int
|
|
lastBufferedAmount int
|
|
start time.Time
|
|
}
|
|
|
|
func NewBitrateCalculator(duration time.Duration, window time.Duration) *BitrateCalculator {
|
|
windowCnt := int((duration + (window - 1)) / window)
|
|
if windowCnt == 0 {
|
|
windowCnt = 1
|
|
}
|
|
now := mono.Now()
|
|
c := &BitrateCalculator{
|
|
duration: duration,
|
|
windowDuration: window,
|
|
windows: deque.New[bitrateWindow](windowCnt+1, windowCnt+1),
|
|
start: now,
|
|
active: bitrateWindow{start: now},
|
|
}
|
|
|
|
return c
|
|
}
|
|
|
|
func (c *BitrateCalculator) AddBytes(bytes int, bufferedAmout int, ts time.Time) {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
|
|
bytes -= bufferedAmout - c.lastBufferedAmount
|
|
c.lastBufferedAmount = bufferedAmout
|
|
if ts.Sub(c.active.start) >= c.windowDuration {
|
|
c.windows.PushBack(c.active)
|
|
c.active.start = ts
|
|
c.active.bytes = 0
|
|
|
|
for c.windows.Len() > 0 {
|
|
// pop expired windows
|
|
if w := c.windows.Front(); ts.Sub(w.start) > (c.duration + c.windowDuration) {
|
|
c.bytes -= w.bytes
|
|
c.windows.PopFront()
|
|
} else {
|
|
c.start = w.start
|
|
break
|
|
}
|
|
}
|
|
if c.windows.Len() == 0 {
|
|
c.start = ts
|
|
c.bytes = 0
|
|
}
|
|
}
|
|
c.bytes += bytes
|
|
c.active.bytes += bytes
|
|
|
|
}
|
|
|
|
func (c *BitrateCalculator) Bitrate(ts time.Time) int {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
duration := ts.Sub(c.start)
|
|
if duration < c.windowDuration {
|
|
duration = c.windowDuration
|
|
}
|
|
|
|
return c.bytes * 8 * 1000 / int(duration.Milliseconds())
|
|
}
|
|
|
|
type bitrateWindow struct {
|
|
start time.Time
|
|
bytes int
|
|
}
|