fix: bound data-track buffering under downlink congestion (targetLatency) (#4667)

* fix: bound data-track buffering under downlink congestion

The SFU data-track down-channel writer was constructed with targetLatency=0,
which disables the buffered-amount/latency-based drop in writeUnreliable. Under
subscriber downlink congestion, frames were queued unbounded and in order in the
per-subscriber SCTP send buffer instead of being dropped, so end-to-end latency
grew without limit and only drained once congestion cleared. This contradicts
data tracks being a low-latency, lossy transport.

Wire a dedicated `datachannel_data_track_target_latency` config through to both
data-track writer call sites (mirroring how `datachannel_lossy_target_latency`
controls the lossy publishData channel), reusing lossyDataChannelMinBufferedAmount
as the drop floor. Defaults to 100ms so data tracks are latency-bounded out of
the box; set to 0 to restore the previous unbounded behavior.

Fixes #4666

* fix: track drained bytes on successful unreliable data channel writes

writeUnreliable only called BitrateCalculator.AddBytes when a write failed,
so on the common success path the calculator never received samples. As a
result Bitrate() had no data and the latency-based drop threshold collapsed to
the static minBufferedAmount floor, making the per-subscriber latency control
(datachannel_lossy_target_latency and the new datachannel_data_track_target_latency)
effectively inert. Call AddBytes after every write, matching writeReliable.
This commit is contained in:
Florian Loretan
2026-07-14 10:06:09 +08:00
committed by GitHub
parent 1c89e727a6
commit 7818dd21b9
7 changed files with 217 additions and 198 deletions
+4
View File
@@ -166,6 +166,10 @@ rtc:
# # max number of bytes to buffer for data channel. 0 means unlimited. # # max number of bytes to buffer for data channel. 0 means unlimited.
# # when this limit is breached, data messages will be dropped till the buffered amount drops below this limit. # # when this limit is breached, data messages will be dropped till the buffered amount drops below this limit.
# data_channel_max_buffered_amount: 0 # data_channel_max_buffered_amount: 0
# # target latency for data track channels. under downlink congestion, frames are dropped
# # to keep the per-subscriber buffer bounded (a fresh frame is preferable to a stale queued one).
# # 0 disables buffering control (unbounded buffering). defaults to 100ms.
# datachannel_data_track_target_latency: 100ms
# when enabled, LiveKit will expose prometheus metrics on :6789/metrics # when enabled, LiveKit will expose prometheus metrics on :6789/metrics
# prometheus_port: 6789 # prometheus_port: 6789
+7
View File
@@ -150,6 +150,12 @@ type RTCConfig struct {
// Target latency for lossy data channels, used to drop packets to reduce latency. // Target latency for lossy data channels, used to drop packets to reduce latency.
DatachannelLossyTargetLatency time.Duration `yaml:"datachannel_lossy_target_latency,omitempty"` DatachannelLossyTargetLatency time.Duration `yaml:"datachannel_lossy_target_latency,omitempty"`
// Target latency for data track channels, used to drop packets to keep the
// per-subscriber buffer bounded under downlink congestion. Data tracks are a
// low-latency lossy transport, so a stale queued frame is worse than a dropped
// one. Set to 0 to disable buffering control (unbounded buffering).
DatachannelDataTrackTargetLatency time.Duration `yaml:"datachannel_data_track_target_latency,omitempty"`
ForwardStats ForwardStatsConfig `yaml:"forward_stats,omitempty"` ForwardStats ForwardStatsConfig `yaml:"forward_stats,omitempty"`
// enable rtp stream restart detection for published tracks // enable rtp stream restart detection for published tracks
@@ -458,6 +464,7 @@ var DefaultConfig = Config{
PacketBufferSizeVideo: 500, PacketBufferSizeVideo: 500,
PacketBufferSizeAudio: 200, PacketBufferSizeAudio: 200,
PLIThrottle: sfu.DefaultPLIThrottleConfig, PLIThrottle: sfu.DefaultPLIThrottleConfig,
DatachannelDataTrackTargetLatency: 100 * time.Millisecond,
CongestionControl: CongestionControlConfig{ CongestionControl: CongestionControlConfig{
Enabled: true, Enabled: true,
AllowPause: false, AllowPause: false,
+2
View File
@@ -215,6 +215,7 @@ type ParticipantParams struct {
DataChannelMaxBufferedAmount uint64 DataChannelMaxBufferedAmount uint64
DatachannelSlowThreshold int DatachannelSlowThreshold int
DatachannelLossyTargetLatency time.Duration DatachannelLossyTargetLatency time.Duration
DatachannelDataTrackTargetLatency time.Duration
FireOnTrackBySdp bool FireOnTrackBySdp bool
DisableCodecRegression bool DisableCodecRegression bool
LastPubReliableSeq uint32 LastPubReliableSeq uint32
@@ -2054,6 +2055,7 @@ func (p *ParticipantImpl) setupTransportManager() error {
DataChannelMaxBufferedAmount: p.params.DataChannelMaxBufferedAmount, DataChannelMaxBufferedAmount: p.params.DataChannelMaxBufferedAmount,
DatachannelSlowThreshold: p.params.DatachannelSlowThreshold, DatachannelSlowThreshold: p.params.DatachannelSlowThreshold,
DatachannelLossyTargetLatency: p.params.DatachannelLossyTargetLatency, DatachannelLossyTargetLatency: p.params.DatachannelLossyTargetLatency,
DatachannelDataTrackTargetLatency: p.params.DatachannelDataTrackTargetLatency,
Logger: p.params.Logger.WithComponent(sutils.ComponentTransport), Logger: p.params.Logger.WithComponent(sutils.ComponentTransport),
PublisherHandler: pth, PublisherHandler: pth,
SubscriberHandler: sth, SubscriberHandler: sth,
+3 -2
View File
@@ -324,6 +324,7 @@ type TransportParams struct {
DataChannelMaxBufferedAmount uint64 DataChannelMaxBufferedAmount uint64
DatachannelSlowThreshold int DatachannelSlowThreshold int
DatachannelLossyTargetLatency time.Duration DatachannelLossyTargetLatency time.Duration
DatachannelDataTrackTargetLatency time.Duration
// for development test // for development test
DatachannelMaxReceiverBufferSize int DatachannelMaxReceiverBufferSize int
@@ -914,7 +915,7 @@ func (t *PCTransport) onDataChannel(dc *webrtc.DataChannel) {
if t.dataTrackDC != nil { if t.dataTrackDC != nil {
t.dataTrackDC.Close() t.dataTrackDC.Close()
} }
t.dataTrackDC = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, 0, 0) t.dataTrackDC = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, t.params.DatachannelDataTrackTargetLatency, uint64(lossyDataChannelMinBufferedAmount))
} }
case kind == livekit.DataPacket_RELIABLE: case kind == livekit.DataPacket_RELIABLE:
@@ -1300,7 +1301,7 @@ func (t *PCTransport) CreateDataChannel(label string, dci *webrtc.DataChannelIni
case dcPtr == &t.lossyDC: case dcPtr == &t.lossyDC:
*dcPtr = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, t.params.DatachannelLossyTargetLatency, uint64(lossyDataChannelMinBufferedAmount)) *dcPtr = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, t.params.DatachannelLossyTargetLatency, uint64(lossyDataChannelMinBufferedAmount))
case dcPtr == &t.dataTrackDC: case dcPtr == &t.dataTrackDC:
*dcPtr = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, 0, 0) *dcPtr = datachannel.NewDataChannelWriterUnreliable(dc, rawDC, t.params.DatachannelDataTrackTargetLatency, uint64(lossyDataChannelMinBufferedAmount))
} }
if dcReady != nil { if dcReady != nil {
*dcReady = true *dcReady = true
+3
View File
@@ -91,6 +91,7 @@ type TransportManagerParams struct {
DataChannelMaxBufferedAmount uint64 DataChannelMaxBufferedAmount uint64
DatachannelSlowThreshold int DatachannelSlowThreshold int
DatachannelLossyTargetLatency time.Duration DatachannelLossyTargetLatency time.Duration
DatachannelDataTrackTargetLatency time.Duration
Logger logger.Logger Logger logger.Logger
PublisherHandler transport.Handler PublisherHandler transport.Handler
SubscriberHandler transport.Handler SubscriberHandler transport.Handler
@@ -167,6 +168,7 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
DataChannelMaxBufferedAmount: params.DataChannelMaxBufferedAmount, DataChannelMaxBufferedAmount: params.DataChannelMaxBufferedAmount,
DatachannelSlowThreshold: params.DatachannelSlowThreshold, DatachannelSlowThreshold: params.DatachannelSlowThreshold,
DatachannelLossyTargetLatency: params.DatachannelLossyTargetLatency, DatachannelLossyTargetLatency: params.DatachannelLossyTargetLatency,
DatachannelDataTrackTargetLatency: params.DatachannelDataTrackTargetLatency,
FireOnTrackBySdp: params.FireOnTrackBySdp, FireOnTrackBySdp: params.FireOnTrackBySdp,
EnableDataTracks: params.EnableDataTracks, EnableDataTracks: params.EnableDataTracks,
ExcludeIPv6LocalCandidates: params.ExcludeIPv6LocalCandidates, ExcludeIPv6LocalCandidates: params.ExcludeIPv6LocalCandidates,
@@ -192,6 +194,7 @@ func NewTransportManager(params TransportManagerParams) (*TransportManager, erro
DataChannelMaxBufferedAmount: params.DataChannelMaxBufferedAmount, DataChannelMaxBufferedAmount: params.DataChannelMaxBufferedAmount,
DatachannelSlowThreshold: params.DatachannelSlowThreshold, DatachannelSlowThreshold: params.DatachannelSlowThreshold,
DatachannelLossyTargetLatency: params.DatachannelLossyTargetLatency, DatachannelLossyTargetLatency: params.DatachannelLossyTargetLatency,
DatachannelDataTrackTargetLatency: params.DatachannelDataTrackTargetLatency,
Transport: livekit.SignalTarget_SUBSCRIBER, Transport: livekit.SignalTarget_SUBSCRIBER,
Handler: TransportManagerTransportHandler{params.SubscriberHandler, t, lgr}, Handler: TransportManagerTransportHandler{params.SubscriberHandler, t, lgr},
FireOnTrackBySdp: params.FireOnTrackBySdp, FireOnTrackBySdp: params.FireOnTrackBySdp,
+1
View File
@@ -535,6 +535,7 @@ func (r *RoomManager) StartSession(
DataChannelMaxBufferedAmount: r.config.RTC.DataChannelMaxBufferedAmount, DataChannelMaxBufferedAmount: r.config.RTC.DataChannelMaxBufferedAmount,
DatachannelSlowThreshold: r.config.RTC.DatachannelSlowThreshold, DatachannelSlowThreshold: r.config.RTC.DatachannelSlowThreshold,
DatachannelLossyTargetLatency: r.config.RTC.DatachannelLossyTargetLatency, DatachannelLossyTargetLatency: r.config.RTC.DatachannelLossyTargetLatency,
DatachannelDataTrackTargetLatency: r.config.RTC.DatachannelDataTrackTargetLatency,
FireOnTrackBySdp: true, FireOnTrackBySdp: true,
UseSinglePeerConnection: pi.UseSinglePeerConnection, UseSinglePeerConnection: pi.UseSinglePeerConnection,
EnableDataTracks: r.config.EnableDataTracks, EnableDataTracks: r.config.EnableDataTracks,
+3 -2
View File
@@ -130,9 +130,10 @@ func (w *DataChannelWriter[T]) writeUnreliable(p []byte) (n int, err error) {
return 0, err return 0, err
} }
n, err = w.rawDC.Write(p) n, err = w.rawDC.Write(p)
if err != nil { // 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()) w.rate.AddBytes(n, int(w.bufferGetter.BufferedAmount()), mono.Now())
}
return return
} }