Don't drop message if calculate duration is too small (#3442)

* Don't drop message if calculate duration is too small

* fix test
This commit is contained in:
cnderrauber
2025-02-18 14:41:41 +08:00
committed by GitHub
parent b3da3ff2cb
commit b2a54729f5
6 changed files with 23 additions and 11 deletions
+7 -3
View File
@@ -50,6 +50,10 @@ func (c *BitrateCalculator) AddBytes(bytes int, bufferedAmout int, ts time.Time)
defer c.lock.Unlock()
bytes -= bufferedAmout - c.lastBufferedAmount
if bytes < 0 {
// it is possible that internal buffering (non-data like DCEP packet from webrtc) caused bytes to be negative
bytes = 0
}
c.lastBufferedAmount = bufferedAmout
if ts.Sub(c.active.start) >= c.windowDuration {
c.windows.PushBack(c.active)
@@ -76,15 +80,15 @@ func (c *BitrateCalculator) AddBytes(bytes int, bufferedAmout int, ts time.Time)
}
func (c *BitrateCalculator) Bitrate(ts time.Time) int {
func (c *BitrateCalculator) Bitrate(ts time.Time) (int, bool) {
c.lock.Lock()
defer c.lock.Unlock()
duration := ts.Sub(c.start)
if duration < c.windowDuration {
duration = c.windowDuration
return 0, false
}
return c.bytes * 8 * 1000 / int(duration.Milliseconds())
return c.bytes * 8 * 1000 / int(duration.Milliseconds()), true
}
type bitrateWindow struct {