fix data channel slow reader test (#3453)
This commit is contained in:
@@ -81,11 +81,23 @@ func (c *BitrateCalculator) AddBytes(bytes int, bufferedAmout int, ts time.Time)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *BitrateCalculator) Bitrate(ts time.Time) (int, bool) {
|
func (c *BitrateCalculator) Bitrate(ts time.Time) (int, bool) {
|
||||||
|
return c.bitrate(ts, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *BitrateCalculator) ForceBitrate(ts time.Time) (int, bool) {
|
||||||
|
return c.bitrate(ts, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *BitrateCalculator) bitrate(ts time.Time, force bool) (int, bool) {
|
||||||
c.lock.Lock()
|
c.lock.Lock()
|
||||||
defer c.lock.Unlock()
|
defer c.lock.Unlock()
|
||||||
duration := ts.Sub(c.start)
|
duration := ts.Sub(c.start)
|
||||||
if duration < c.windowDuration {
|
if duration < c.windowDuration {
|
||||||
return 0, false
|
if force {
|
||||||
|
duration = c.windowDuration
|
||||||
|
} else {
|
||||||
|
return 0, false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.bytes * 8 * 1000 / int(duration.Milliseconds()), true
|
return c.bytes * 8 * 1000 / int(duration.Milliseconds()), true
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ func NewDataChannelReader(bitrate int) *DataChannelReader {
|
|||||||
|
|
||||||
func (d *DataChannelReader) Read(p []byte, sid string) {
|
func (d *DataChannelReader) Read(p []byte, sid string) {
|
||||||
for {
|
for {
|
||||||
if bitrate, ok := d.bitrate.Bitrate(time.Now()); ok && bitrate > 0 && bitrate > d.target {
|
if bitrate, ok := d.bitrate.ForceBitrate(time.Now()); ok && bitrate > 0 && bitrate > d.target {
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
d.bitrate.AddBytes(0, 0, time.Now())
|
d.bitrate.AddBytes(0, 0, time.Now())
|
||||||
continue
|
continue
|
||||||
@@ -29,8 +29,3 @@ func (d *DataChannelReader) Read(p []byte, sid string) {
|
|||||||
}
|
}
|
||||||
d.bitrate.AddBytes(len(p), 0, time.Now())
|
d.bitrate.AddBytes(len(p), 0, time.Now())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DataChannelReader) Bitrate() int {
|
|
||||||
b, _ := d.bitrate.Bitrate(time.Now())
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user