* fix: don't undercount data channel bitrate over idle gaps
BitrateCalculator divided drained bytes by wall-clock time, including
idle gaps between sparse writes. A fast channel fed sparsely (e.g. 1 KB
every second, acked in a few ms) therefore reported its low offered load
(~8 kbps) instead of its drain capacity (~800 kbps). In the unreliable
writer that shrinks targetLatencyLimit and drops bursts on a channel
that is actually keeping up.
Measure the rate over backlogged ("busy") time only, so idle time no
longer dilutes the estimate. Backlog is detected from the buffer
occupancy just before a write (bufferedAmount - bytesWritten): since no
bytes are added between writes the buffer only drains, so a non-zero
pre-write level means it never emptied and the interval was genuinely
busy. Gating on the post-write buffered amount would be wrong -- it
always includes the just-enqueued (not yet SACKed) bytes and is ~never
zero, collapsing the estimate back to wall-clock. When no backlog is
ever observed the calculator reports no estimate (ok=false) instead of a
confidently-low number.
Introduce BitrateMode:
- BusyOnly (writer default): busy denominator, all drained bytes counted.
- ExcludeIdleDrain: also drops bytes drained across non-backlogged
intervals for a clean drain-capacity estimate free of idle
contamination.
- WallClock: elapsed-time denominator, all bytes; for the test-client
reader which has no send buffer to observe.
Call sites keep the conservative behaviour (writers BusyOnly, reader
WallClock). Tests exercise both writer modes and the mixed idle-then-
backlog window where they diverge.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* Trim comments in data channel bitrate calculator
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
* Make BitrateMode.String a method on the enum
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
153 lines
4.6 KiB
Go
153 lines
4.6 KiB
Go
package datachannel
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/pion/datachannel"
|
|
|
|
"github.com/livekit/protocol/utils/mono"
|
|
)
|
|
|
|
const (
|
|
singleWriteTimeout = 50 * time.Millisecond
|
|
)
|
|
|
|
var (
|
|
ErrDataDroppedBySlowReader = errors.New("data dropped by slow reader")
|
|
ErrDataDroppedByHighBufferedAmount = errors.New("data dropped due to high buffered amount")
|
|
)
|
|
|
|
type BufferedAmountGetter interface {
|
|
BufferedAmount() uint64
|
|
}
|
|
|
|
type DataChannelWriter[T BufferedAmountGetter] struct {
|
|
bufferGetter T
|
|
rawDC datachannel.ReadWriteCloserDeadliner
|
|
slowThreshold int
|
|
rate *BitrateCalculator
|
|
reliable bool
|
|
targetLatency time.Duration
|
|
minBufferedAmount uint64
|
|
}
|
|
|
|
// NewDataChannelWriterReliable creates a new DataChannelWriter for reliable data channel by
|
|
// detaching it, when writing to the datachanel times out, it will block and retry if the
|
|
// receiver's bitrate is above the slowThreshold or drop the data if it's below the threshold.
|
|
// If the slowThreshold is 0, it will never retry on write timeout.
|
|
func NewDataChannelWriterReliable[T BufferedAmountGetter](bufferGetter T, rawDC datachannel.ReadWriteCloserDeadliner, slowThreshold int) *DataChannelWriter[T] {
|
|
var rate *BitrateCalculator
|
|
if slowThreshold > 0 {
|
|
rate = NewBitrateCalculator(BitrateDuration, BitrateWindow, BitrateModeBusyOnly)
|
|
}
|
|
return &DataChannelWriter[T]{
|
|
bufferGetter: bufferGetter,
|
|
rawDC: rawDC,
|
|
slowThreshold: slowThreshold,
|
|
rate: rate,
|
|
reliable: true,
|
|
}
|
|
}
|
|
|
|
// NewDataChannelWriterUnreliable creates a new DataChannelWriter for unreliable data channel.
|
|
// It will drop data when the buffered amount is too high to maintain the target latency.
|
|
// The latency is estimated based on the bitrate in past 1 second. If targetLatency is 0, no
|
|
// buffering control is applied.
|
|
func NewDataChannelWriterUnreliable[T BufferedAmountGetter](bufferGetter T, rawDC datachannel.ReadWriteCloserDeadliner, targetLatency time.Duration, minBufferedAmount uint64) *DataChannelWriter[T] {
|
|
var rate *BitrateCalculator
|
|
if targetLatency > 0 {
|
|
rate = NewBitrateCalculator(BitrateDuration, BitrateWindow, BitrateModeBusyOnly)
|
|
}
|
|
return &DataChannelWriter[T]{
|
|
bufferGetter: bufferGetter,
|
|
rawDC: rawDC,
|
|
rate: rate,
|
|
targetLatency: targetLatency,
|
|
minBufferedAmount: minBufferedAmount,
|
|
reliable: false,
|
|
}
|
|
}
|
|
|
|
func (w *DataChannelWriter[T]) BufferedAmountGetter() T {
|
|
return w.bufferGetter
|
|
}
|
|
|
|
func (w *DataChannelWriter[T]) Write(p []byte) (n int, err error) {
|
|
if w.reliable {
|
|
return w.writeReliable(p)
|
|
} else {
|
|
return w.writeUnreliable(p)
|
|
}
|
|
}
|
|
|
|
func (w *DataChannelWriter[T]) writeReliable(p []byte) (n int, err error) {
|
|
for {
|
|
err = w.rawDC.SetWriteDeadline(time.Now().Add(singleWriteTimeout))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err = w.rawDC.Write(p)
|
|
if w.slowThreshold == 0 {
|
|
return
|
|
}
|
|
|
|
now := mono.Now()
|
|
w.rate.AddBytes(n, int(w.bufferGetter.BufferedAmount()), now)
|
|
// retry if the write timed out on a non-slow receiver
|
|
if errors.Is(err, context.DeadlineExceeded) {
|
|
if bitrate, ok := w.rate.Bitrate(now); !ok || bitrate >= w.slowThreshold {
|
|
continue
|
|
} else {
|
|
err = fmt.Errorf("%w: bitrate %d, threshold %d", ErrDataDroppedBySlowReader, bitrate, w.slowThreshold)
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
}
|
|
|
|
func (w *DataChannelWriter[T]) writeUnreliable(p []byte) (n int, err error) {
|
|
if w.targetLatency == 0 {
|
|
err = w.rawDC.SetWriteDeadline(time.Now().Add(singleWriteTimeout))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return w.rawDC.Write(p)
|
|
}
|
|
|
|
if bitrate, ok := w.rate.Bitrate(time.Now()); ok {
|
|
// control buffer latency to target
|
|
bufferedAmount := w.bufferGetter.BufferedAmount()
|
|
targetLatencyLimit := uint64(time.Duration(bitrate) * w.targetLatency / 8 / time.Second)
|
|
if bufferedAmount > targetLatencyLimit && bufferedAmount > w.minBufferedAmount {
|
|
return 0, fmt.Errorf(
|
|
"%w: bitrate %d, buffered amount %d, target latency limit %d, min buffered amount %d",
|
|
ErrDataDroppedByHighBufferedAmount,
|
|
bitrate,
|
|
bufferedAmount,
|
|
targetLatencyLimit,
|
|
w.minBufferedAmount,
|
|
)
|
|
}
|
|
}
|
|
|
|
err = w.rawDC.SetWriteDeadline(time.Now().Add(singleWriteTimeout))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
n, err = w.rawDC.Write(p)
|
|
// Track drained bytes on every write (not just failures) so the bitrate
|
|
// calculator has samples; otherwise Bitrate() has no data and the
|
|
// latency-based drop threshold above collapses to the static minBufferedAmount.
|
|
w.rate.AddBytes(n, int(w.bufferGetter.BufferedAmount()), mono.Now())
|
|
|
|
return
|
|
}
|
|
|
|
func (w *DataChannelWriter[T]) Close() error {
|
|
return w.rawDC.Close()
|
|
}
|