diff --git a/pkg/sfu/bwe/bwe.go b/pkg/sfu/bwe/bwe.go index f6857e64..3185c757 100644 --- a/pkg/sfu/bwe/bwe.go +++ b/pkg/sfu/bwe/bwe.go @@ -85,6 +85,8 @@ type BWE interface { UpdateRTT(rtt float64) + CongestionState() CongestionState + CanProbe() bool ProbeDuration() time.Duration ProbeClusterStarting(pci ccutils.ProbeClusterInfo) @@ -96,7 +98,7 @@ type BWE interface { // ------------------------------------------------ type BWEListener interface { - OnCongestionStateChange(congestionState CongestionState, estimatedAvailableChannelCapacity int64) + OnCongestionStateChange(fromState CongestionState, toState CongestionState, estimatedAvailableChannelCapacity int64) } // ------------------------------------------------ diff --git a/pkg/sfu/bwe/null_bwe.go b/pkg/sfu/bwe/null_bwe.go index 140635cd..2a1c394e 100644 --- a/pkg/sfu/bwe/null_bwe.go +++ b/pkg/sfu/bwe/null_bwe.go @@ -50,6 +50,10 @@ func (n *NullBWE) HandleTWCCFeedback(_report *rtcp.TransportLayerCC) {} func (n *NullBWE) UpdateRTT(rtt float64) {} +func (n *NullBWE) CongestionState() CongestionState { + return CongestionStateNone +} + func (n *NullBWE) CanProbe() bool { return false } diff --git a/pkg/sfu/bwe/remotebwe/remote_bwe.go b/pkg/sfu/bwe/remotebwe/remote_bwe.go index 9d19866f..bfe90f87 100644 --- a/pkg/sfu/bwe/remotebwe/remote_bwe.go +++ b/pkg/sfu/bwe/remotebwe/remote_bwe.go @@ -137,12 +137,12 @@ func (r *RemoteBWE) HandleREMB( r.channelObserver.AddEstimate(r.lastReceivedEstimate) r.channelObserver.AddNack(sentPackets, repeatedNacks) - shouldNotify, state, committedChannelCapacity := r.congestionDetectionStateMachine() + shouldNotify, fromState, toState, committedChannelCapacity := r.congestionDetectionStateMachine() r.lock.Unlock() if shouldNotify { if bweListener := r.getBWEListener(); bweListener != nil { - bweListener.OnCongestionStateChange(state, committedChannelCapacity) + bweListener.OnCongestionStateChange(fromState, toState, committedChannelCapacity) } } } @@ -154,18 +154,19 @@ func (r *RemoteBWE) UpdateRTT(rtt float64) { r.probeController.UpdateRTT(rtt) } -func (r *RemoteBWE) congestionDetectionStateMachine() (bool, bwe.CongestionState, int64) { - newState := r.congestionState +func (r *RemoteBWE) congestionDetectionStateMachine() (bool, bwe.CongestionState, bwe.CongestionState, int64) { + fromState := r.congestionState + toState := r.congestionState update := false trend, reason := r.channelObserver.GetTrend() - switch r.congestionState { + switch fromState { case bwe.CongestionStateNone: if trend == channelTrendCongesting { if r.probeController.IsInProbe() || r.estimateAvailableChannelCapacity(reason) { // when in probe, if congested, stays there till probe is done, // the estimate stays at pre-probe level - newState = bwe.CongestionStateCongested + toState = bwe.CongestionStateCongested } } @@ -176,26 +177,26 @@ func (r *RemoteBWE) congestionDetectionStateMachine() (bool, bwe.CongestionState update = true } } else { - newState = bwe.CongestionStateCongestedHangover + toState = bwe.CongestionStateCongestedHangover } case bwe.CongestionStateCongestedHangover: if trend == channelTrendCongesting { if r.estimateAvailableChannelCapacity(reason) { - newState = bwe.CongestionStateCongested + toState = bwe.CongestionStateCongested } } else if time.Since(r.congestionStateSwitchedAt) >= r.params.Config.CongestedHangoverDuration { - newState = bwe.CongestionStateNone + toState = bwe.CongestionStateNone } } shouldNotify := false - if newState != r.congestionState || update { - r.updateCongestionState(newState, reason) + if toState != fromState || update { + fromState, toState = r.updateCongestionState(toState, reason) shouldNotify = true } - return shouldNotify, r.congestionState, r.committedChannelCapacity + return shouldNotify, fromState, toState, r.committedChannelCapacity } func (r *RemoteBWE) estimateAvailableChannelCapacity(reason channelCongestionReason) bool { @@ -229,7 +230,7 @@ func (r *RemoteBWE) estimateAvailableChannelCapacity(reason channelCongestionRea return true } -func (r *RemoteBWE) updateCongestionState(state bwe.CongestionState, reason channelCongestionReason) { +func (r *RemoteBWE) updateCongestionState(state bwe.CongestionState, reason channelCongestionReason) (bwe.CongestionState, bwe.CongestionState) { r.params.Logger.Infow( "remote bwe: congestion state change", "from", r.congestionState, @@ -238,8 +239,17 @@ func (r *RemoteBWE) updateCongestionState(state bwe.CongestionState, reason chan "committedChannelCapacity", r.committedChannelCapacity, ) + fromState := r.congestionState r.congestionState = state r.congestionStateSwitchedAt = mono.Now() + return fromState, r.congestionState +} + +func (r *RemoteBWE) CongestionState() bwe.CongestionState { + r.lock.Lock() + defer r.lock.Unlock() + + return r.congestionState } func (r *RemoteBWE) CanProbe() bool { diff --git a/pkg/sfu/bwe/sendsidebwe/congestion_detector.go b/pkg/sfu/bwe/sendsidebwe/congestion_detector.go index 0a36c546..d302bb29 100644 --- a/pkg/sfu/bwe/sendsidebwe/congestion_detector.go +++ b/pkg/sfu/bwe/sendsidebwe/congestion_detector.go @@ -40,23 +40,23 @@ func (c CongestionSignalConfig) IsTriggered(numGroups int, duration int64) bool var ( defaultQueuingDelayEarlyWarningCongestionSignalConfig = CongestionSignalConfig{ - MinNumberOfGroups: 1, - MinDuration: 100 * time.Millisecond, + MinNumberOfGroups: 2, + MinDuration: 200 * time.Millisecond, } defaultLossEarlyWarningCongestionSignalConfig = CongestionSignalConfig{ - MinNumberOfGroups: 2, - MinDuration: 300 * time.Millisecond, - } - - defaultQueuingDelayCongestedCongestionSignalConfig = CongestionSignalConfig{ MinNumberOfGroups: 3, MinDuration: 300 * time.Millisecond, } + defaultQueuingDelayCongestedCongestionSignalConfig = CongestionSignalConfig{ + MinNumberOfGroups: 4, + MinDuration: 400 * time.Millisecond, + } + defaultLossCongestedCongestionSignalConfig = CongestionSignalConfig{ MinNumberOfGroups: 6, - MinDuration: 900 * time.Millisecond, + MinDuration: 600 * time.Millisecond, } ) @@ -140,12 +140,14 @@ type qdMeasurement struct { jqrMin int64 dqrMax int64 - numGroups int - minSendTime int64 - maxSendTime int64 + numGroups int + numJQRGroups int + minSendTime int64 + maxSendTime int64 - isSealed bool - sealedGroupIdx int + isSealed bool + minGroupIdx int + maxGroupIdx int queuingRegion queuingRegion } @@ -169,16 +171,21 @@ func (q *qdMeasurement) ProcessPacketGroup(pg *packetGroup, groupIdx int) { return } + q.numGroups++ + if q.minGroupIdx == 0 || q.minGroupIdx > groupIdx { + groupIdx = q.minGroupIdx + } + q.maxGroupIdx = max(q.maxGroupIdx, groupIdx) + if pqd < q.dqrMax { // a DQR breaks continuity q.isSealed = true - q.sealedGroupIdx = groupIdx q.queuingRegion = queuingRegionDQR return } if pqd > q.jqrMin { - q.numGroups++ + q.numJQRGroups++ minSendTime, maxSendTime := pg.SendWindow() if q.minSendTime == 0 || minSendTime < q.minSendTime { q.minSendTime = minSendTime @@ -186,9 +193,8 @@ func (q *qdMeasurement) ProcessPacketGroup(pg *packetGroup, groupIdx int) { q.maxSendTime = max(q.maxSendTime, maxSendTime) } - if q.config.IsTriggered(q.numGroups, q.maxSendTime-q.minSendTime) { + if q.config.IsTriggered(q.numJQRGroups, q.maxSendTime-q.minSendTime) { q.isSealed = true - q.sealedGroupIdx = groupIdx q.queuingRegion = queuingRegionJQR } } @@ -197,8 +203,8 @@ func (q *qdMeasurement) IsSealed() bool { return q.isSealed } -func (q *qdMeasurement) Result() (queuingRegion, int) { - return q.queuingRegion, q.sealedGroupIdx +func (q *qdMeasurement) Result() (queuingRegion, int, int) { + return q.queuingRegion, q.minGroupIdx, q.maxGroupIdx } func (q *qdMeasurement) MarshalLogObject(e zapcore.ObjectEncoder) error { @@ -207,11 +213,13 @@ func (q *qdMeasurement) MarshalLogObject(e zapcore.ObjectEncoder) error { } e.AddInt("numGroups", q.numGroups) + e.AddInt("numJQRGroups", q.numJQRGroups) e.AddInt64("minSendTime", q.minSendTime) e.AddInt64("maxSendTime", q.maxSendTime) e.AddDuration("duration", time.Duration((q.maxSendTime-q.minSendTime)*1000)) e.AddBool("isSealed", q.isSealed) - e.AddInt("sealedGroupIdx", q.sealedGroupIdx) + e.AddInt("minGroupIdx", q.minGroupIdx) + e.AddInt("maxGroupIdx", q.maxGroupIdx) e.AddString("queuingRegion", q.queuingRegion.String()) return nil } @@ -226,8 +234,9 @@ type lossMeasurement struct { numGroups int ts *trafficStats - isSealed bool - sealedGroupIdx int + isSealed bool + minGroupIdx int + maxGroupIdx int weightedLoss float64 @@ -259,12 +268,16 @@ func (l *lossMeasurement) ProcessPacketGroup(pg *packetGroup, groupIdx int) { } l.numGroups++ + if l.minGroupIdx == 0 || l.minGroupIdx > groupIdx { + groupIdx = l.minGroupIdx + } + l.maxGroupIdx = max(l.maxGroupIdx, groupIdx) + l.ts.Merge(pg.Traffic()) duration := l.ts.Duration() if l.config.IsTriggered(l.numGroups, duration) { l.isSealed = true - l.sealedGroupIdx = groupIdx l.weightedLoss = l.ts.WeightedLoss() if l.weightedLoss < l.dqrMaxLoss { @@ -279,8 +292,8 @@ func (l *lossMeasurement) IsSealed() bool { return l.isSealed } -func (l *lossMeasurement) Result() (queuingRegion, int) { - return l.queuingRegion, l.sealedGroupIdx +func (l *lossMeasurement) Result() (queuingRegion, int, int) { + return l.queuingRegion, l.minGroupIdx, l.maxGroupIdx } func (l *lossMeasurement) MarshalLogObject(e zapcore.ObjectEncoder) error { @@ -291,7 +304,8 @@ func (l *lossMeasurement) MarshalLogObject(e zapcore.ObjectEncoder) error { e.AddInt("numGroups", l.numGroups) e.AddObject("ts", l.ts) e.AddBool("isSealed", l.isSealed) - e.AddInt("sealedGroupIdx", l.sealedGroupIdx) + e.AddInt("minGroupIdx", l.minGroupIdx) + e.AddInt("maxGroupIdx", l.maxGroupIdx) e.AddFloat64("weightedLoss", l.weightedLoss) e.AddString("queuingRegion", l.queuingRegion.String()) return nil @@ -331,7 +345,7 @@ var ( defaultTrendDetectorConfigCongestedCTR = ccutils.TrendDetectorConfig{ RequiredSamples: 4, RequiredSamplesMin: 2, - DownwardTrendThreshold: -0.5, + DownwardTrendThreshold: -0.7, DownwardTrendMaxWait: 2 * time.Second, CollapseThreshold: 500 * time.Millisecond, ValidityWindow: 10 * time.Second, @@ -583,12 +597,12 @@ func (c *congestionDetector) HandleTWCCFeedback(report *rtcp.TransportLayerCC) { } c.prunePacketGroups() - shouldNotify, state, committedChannelCapacity := c.congestionDetectionStateMachine() + shouldNotify, fromState, toState, committedChannelCapacity := c.congestionDetectionStateMachine() c.lock.Unlock() if shouldNotify { if bweListener := c.getBWEListener(); bweListener != nil { - bweListener.OnCongestionStateChange(state, committedChannelCapacity) + bweListener.OnCongestionStateChange(fromState, toState, committedChannelCapacity) } } } @@ -608,6 +622,13 @@ func (c *congestionDetector) UpdateRTT(rtt float64) { } } +func (c *congestionDetector) CongestionState() bwe.CongestionState { + c.lock.Lock() + defer c.lock.Unlock() + + return c.congestionState +} + func (c *congestionDetector) CanProbe() bool { c.lock.Lock() defer c.lock.Unlock() @@ -718,7 +739,7 @@ func (c *congestionDetector) getCongestionSignal( stage string, qdConfig CongestionSignalConfig, lossConfig CongestionSignalConfig, -) (queuingRegion, string, int) { +) (queuingRegion, string, int, int) { qdMeasurement := newQdMeasurement( qdConfig, c.params.Config.JQRMinDelay.Microseconds(), @@ -744,24 +765,21 @@ func (c *congestionDetector) getCongestionSignal( } } - oldestContributingGroup := max(0, idx) reason := "" - qr, groupIdx := qdMeasurement.Result() + qr, minGroupIdx, maxGroupIdx := qdMeasurement.Result() if qr == queuingRegionJQR { reason = "queuing-delay" - oldestContributingGroup = groupIdx } else { - qr, groupIdx = lossMeasurement.Result() + qr, minGroupIdx, maxGroupIdx = lossMeasurement.Result() if qr == queuingRegionJQR { reason = "loss" - oldestContributingGroup = groupIdx } } - return qr, reason, oldestContributingGroup + return qr, reason, max(0, minGroupIdx), max(0, maxGroupIdx) } -func (c *congestionDetector) getEarlyWarningSignal() (queuingRegion, string, int) { +func (c *congestionDetector) getEarlyWarningSignal() (queuingRegion, string, int, int) { return c.getCongestionSignal( "early-warning", c.params.Config.QueuingDelayEarlyWarning, @@ -769,7 +787,7 @@ func (c *congestionDetector) getEarlyWarningSignal() (queuingRegion, string, int ) } -func (c *congestionDetector) getCongestedSignal() (queuingRegion, string, int) { +func (c *congestionDetector) getCongestedSignal() (queuingRegion, string, int, int) { return c.getCongestionSignal( "congested", c.params.Config.QueuingDelayCongested, @@ -777,84 +795,85 @@ func (c *congestionDetector) getCongestedSignal() (queuingRegion, string, int) { ) } -func (c *congestionDetector) congestionDetectionStateMachine() (bool, bwe.CongestionState, int64) { - state := c.congestionState - newState := c.congestionState +func (c *congestionDetector) congestionDetectionStateMachine() (bool, bwe.CongestionState, bwe.CongestionState, int64) { + fromState := c.congestionState + toState := c.congestionState var ( - qr queuingRegion - reason string - oldestContributingGroup int + qr queuingRegion + reason string + minGroupIdx, maxGroupIdx int ) - switch state { + switch fromState { case bwe.CongestionStateNone: - qr, reason, oldestContributingGroup = c.getEarlyWarningSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getEarlyWarningSignal() if qr == queuingRegionJQR { - newState = bwe.CongestionStateEarlyWarning + toState = bwe.CongestionStateEarlyWarning } case bwe.CongestionStateEarlyWarning: - qr, reason, oldestContributingGroup = c.getCongestedSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getCongestedSignal() if qr == queuingRegionJQR { - newState = bwe.CongestionStateCongested + toState = bwe.CongestionStateCongested } else { - qr, _, _ := c.getEarlyWarningSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getEarlyWarningSignal() if qr == queuingRegionDQR { - newState = bwe.CongestionStateEarlyWarningHangover + toState = bwe.CongestionStateEarlyWarningHangover } } case bwe.CongestionStateEarlyWarningHangover: - qr, reason, oldestContributingGroup = c.getEarlyWarningSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getEarlyWarningSignal() if qr == queuingRegionJQR { - newState = bwe.CongestionStateEarlyWarning + toState = bwe.CongestionStateEarlyWarning } else if time.Since(c.congestionStateSwitchedAt) >= c.params.Config.EarlyWarningHangover { - newState = bwe.CongestionStateNone + toState = bwe.CongestionStateNone } case bwe.CongestionStateCongested: - qr, _, _ = c.getCongestedSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getCongestedSignal() if qr == queuingRegionDQR { - newState = bwe.CongestionStateCongestedHangover + toState = bwe.CongestionStateCongestedHangover } case bwe.CongestionStateCongestedHangover: - qr, reason, oldestContributingGroup = c.getEarlyWarningSignal() + qr, reason, minGroupIdx, maxGroupIdx = c.getEarlyWarningSignal() if qr == queuingRegionJQR { - newState = bwe.CongestionStateEarlyWarning + toState = bwe.CongestionStateEarlyWarning } else if time.Since(c.congestionStateSwitchedAt) >= c.params.Config.CongestedHangover { - newState = bwe.CongestionStateNone + toState = bwe.CongestionStateNone } } - c.estimateAvailableChannelCapacity(oldestContributingGroup) + c.estimateAvailableChannelCapacity(minGroupIdx) // update after running the above estimate as state change callback includes the estimated available channel capacity shouldNotify := false - if newState != state { - c.updateCongestionState(newState, reason, oldestContributingGroup) + if toState != fromState { + fromState, toState = c.updateCongestionState(toState, reason, minGroupIdx, maxGroupIdx) shouldNotify = true } if c.congestedCTRTrend != nil && c.congestedCTRTrend.GetDirection() == ccutils.TrendDirectionDownward { - shouldNotify = true - congestedAckedBitrate := c.congestedTrafficStats.AcknowledgedBitrate() if congestedAckedBitrate < c.estimatedAvailableChannelCapacity { c.estimatedAvailableChannelCapacity = congestedAckedBitrate + + c.params.Logger.Infow( + "send side bwe: captured traffic ratio is trending downward", + "channel", c.congestedCTRTrend, + "trafficStats", c.congestedTrafficStats, + "estimatedAvailableChannelCapacity", c.estimatedAvailableChannelCapacity, + ) + + shouldNotify = true } - c.params.Logger.Infow( - "send side bwe: captured traffic ratio is trending downward", - "channel", c.congestedCTRTrend, - "trafficStats", c.congestedTrafficStats, - "estimatedAvailableChannelCapacity", c.estimatedAvailableChannelCapacity, - ) // reset to get new set of samples for next trend c.resetCTRTrend() } - return shouldNotify, c.congestionState, c.estimatedAvailableChannelCapacity + return shouldNotify, fromState, toState, c.estimatedAvailableChannelCapacity } func (c *congestionDetector) createCTRTrend() { @@ -943,23 +962,26 @@ func (c *congestionDetector) estimateAvailableChannelCapacity(oldestContributing c.estimatedAvailableChannelCapacity = agg.AcknowledgedBitrate() } -func (c *congestionDetector) updateCongestionState(state bwe.CongestionState, reason string, oldestContributingGroup int) { - c.params.Logger.Infow( - "send side bwe: congestion state change", +func (c *congestionDetector) updateCongestionState(state bwe.CongestionState, reason string, minGroupIdx int, maxGroupIdx int) (bwe.CongestionState, bwe.CongestionState) { + loggingFields := []any{ "from", c.congestionState, "to", state, "reason", reason, "numPacketGroups", len(c.packetGroups), - "numContributingGroups", len(c.packetGroups[oldestContributingGroup:]), - "contributingGroups", logger.ObjectSlice(c.packetGroups[oldestContributingGroup:]), + "minGroupIdx", minGroupIdx, + "maxGroupIdx", maxGroupIdx, "estimatedAvailableChannelCapacity", c.estimatedAvailableChannelCapacity, - ) + } + if state == bwe.CongestionStateEarlyWarning || state == bwe.CongestionStateCongested { + loggingFields = append(loggingFields, "contributingGroups", logger.ObjectSlice(c.packetGroups[minGroupIdx:maxGroupIdx+1])) + } + c.params.Logger.Infow("send side bwe: congestion state change", loggingFields...) if state != c.congestionState { c.congestionStateSwitchedAt = mono.Now() } - prevState := c.congestionState + fromState := c.congestionState c.congestionState = state // when in congested state, monitor changes in captured traffic ratio (CTR) @@ -968,9 +990,11 @@ func (c *congestionDetector) updateCongestionState(state bwe.CongestionState, re // sub-optimal and not enough to reduce/relieve congestion, by monitoing CTR // on a continuous basis allocations can be adjusted in the direction of // reducing/relieving congestion - if state == bwe.CongestionStateCongested && prevState != bwe.CongestionStateCongested { + if state == bwe.CongestionStateCongested && fromState != bwe.CongestionStateCongested { c.createCTRTrend() } else if state != bwe.CongestionStateCongested { c.clearCTRTrend() } + + return fromState, c.congestionState } diff --git a/pkg/sfu/bwe/sendsidebwe/packet_group.go b/pkg/sfu/bwe/sendsidebwe/packet_group.go index a9b7b192..0c9f2d0f 100644 --- a/pkg/sfu/bwe/sendsidebwe/packet_group.go +++ b/pkg/sfu/bwe/sendsidebwe/packet_group.go @@ -39,7 +39,7 @@ type PacketGroupConfig struct { var ( defaultPacketGroupConfig = PacketGroupConfig{ - MinPackets: 20, + MinPackets: 30, MaxWindowDuration: 500 * time.Millisecond, } ) @@ -142,18 +142,18 @@ type packetGroup struct { lost classStat snBitmap *utils.Bitmap[uint64] - aggregateSendDelta int64 - aggregateRecvDelta int64 - queuingDelay int64 + aggregateSendDelta int64 + aggregateRecvDelta int64 + inheritedQueuingDelay int64 isFinalized bool } -func newPacketGroup(params packetGroupParams, queuingDelay int64) *packetGroup { +func newPacketGroup(params packetGroupParams, inheritedQueuingDelay int64) *packetGroup { return &packetGroup{ - params: params, - queuingDelay: queuingDelay, - snBitmap: utils.NewBitmap[uint64](params.Config.MinPackets), + params: params, + inheritedQueuingDelay: inheritedQueuingDelay, + snBitmap: utils.NewBitmap[uint64](params.Config.MinPackets), } } @@ -231,8 +231,8 @@ func (p *packetGroup) SendWindow() (int64, int64) { } func (p *packetGroup) PropagatedQueuingDelay() int64 { - if p.queuingDelay+p.aggregateRecvDelta-p.aggregateSendDelta > 0 { - return p.queuingDelay + p.aggregateRecvDelta - p.aggregateSendDelta + if p.inheritedQueuingDelay+p.aggregateRecvDelta-p.aggregateSendDelta > 0 { + return p.inheritedQueuingDelay + p.aggregateRecvDelta - p.aggregateSendDelta } return max(0, p.aggregateRecvDelta-p.aggregateSendDelta) @@ -290,7 +290,7 @@ func (p *packetGroup) MarshalLogObject(e zapcore.ObjectEncoder) error { }) ts.Merge(p.Traffic()) e.AddObject("trafficStats", ts) - e.AddInt64("queuingDelay", p.queuingDelay) + e.AddInt64("inheritedQueuingDelay", p.inheritedQueuingDelay) e.AddInt64("propagatedQueuingDelay", p.PropagatedQueuingDelay()) e.AddBool("isFinalized", p.isFinalized) diff --git a/pkg/sfu/bwe/sendsidebwe/send_side_bwe.go b/pkg/sfu/bwe/sendsidebwe/send_side_bwe.go index 59fdc061..1e3cc744 100644 --- a/pkg/sfu/bwe/sendsidebwe/send_side_bwe.go +++ b/pkg/sfu/bwe/sendsidebwe/send_side_bwe.go @@ -114,6 +114,10 @@ func (s *SendSideBWE) UpdateRTT(rtt float64) { s.congestionDetector.UpdateRTT(rtt) } +func (s *SendSideBWE) CongestionState() bwe.CongestionState { + return s.congestionDetector.CongestionState() +} + func (s *SendSideBWE) CanProbe() bool { return s.congestionDetector.CanProbe() } diff --git a/pkg/sfu/bwe/sendsidebwe/traffic_stats.go b/pkg/sfu/bwe/sendsidebwe/traffic_stats.go index ba210275..9158b22d 100644 --- a/pkg/sfu/bwe/sendsidebwe/traffic_stats.go +++ b/pkg/sfu/bwe/sendsidebwe/traffic_stats.go @@ -32,8 +32,8 @@ type WeightedLossConfig struct { var ( defaultWeightedLossConfig = WeightedLossConfig{ - MinDurationForLossValidity: 200 * time.Millisecond, - MinPPSForLossValidity: 20, + MinDurationForLossValidity: 250 * time.Millisecond, + MinPPSForLossValidity: 30, LossPenaltyFactor: 0.25, } ) diff --git a/pkg/sfu/streamallocator/streamallocator.go b/pkg/sfu/streamallocator/streamallocator.go index ba9a7a5c..2abd9f21 100644 --- a/pkg/sfu/streamallocator/streamallocator.go +++ b/pkg/sfu/streamallocator/streamallocator.go @@ -205,8 +205,8 @@ type StreamAllocator struct { isAllocateAllPending bool rembTrackingSSRC uint32 - state streamAllocatorState - isHolding bool + state streamAllocatorState + activeProbeClusterId ccutils.ProbeClusterId activeProbeGoalReached bool activeProbeCongesting bool @@ -454,15 +454,16 @@ func (s *StreamAllocator) onTargetBitrateChange(bitrate int) { // called when congestion state changes (send side bandwidth estimation) type congestionStateChangeData struct { - congestionState bwe.CongestionState + fromState bwe.CongestionState + toState bwe.CongestionState estimatedAvailableChannelCapacity int64 } // BWEListener implementation -func (s *StreamAllocator) OnCongestionStateChange(congestionState bwe.CongestionState, estimatedAvailableChannelCapacity int64) { +func (s *StreamAllocator) OnCongestionStateChange(fromState bwe.CongestionState, toState bwe.CongestionState, estimatedAvailableChannelCapacity int64) { s.postEvent(Event{ Signal: streamAllocatorSignalCongestionStateChange, - Data: congestionStateChangeData{congestionState, estimatedAvailableChannelCapacity}, + Data: congestionStateChangeData{fromState, toState, estimatedAvailableChannelCapacity}, }) } @@ -828,32 +829,23 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) { func (s *StreamAllocator) handleSignalCongestionStateChange(event Event) { cscd := event.Data.(congestionStateChangeData) - if cscd.congestionState != bwe.CongestionStateNone { + if cscd.toState != bwe.CongestionStateNone { // end/abort any running probe if channel is not clear s.maybeStopProbe() } - if cscd.congestionState == bwe.CongestionStateEarlyWarning || - cscd.congestionState == bwe.CongestionStateEarlyWarningHangover { - s.isHolding = true - } else { - // early warning is done and hold has been released, - // if there is no congestion, allocate all tracks optimally as - // some tracks may have been held at sub-optimal allocation - // during early warning hold - if s.isHolding && cscd.congestionState == bwe.CongestionStateNone && s.state == streamAllocatorStateStable { - update := NewStreamStateUpdate() - for _, track := range s.getTracks() { - allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, false) - updateStreamStateChange(track, allocation, update) - } - s.maybeSendUpdate(update) + // some tracks may have been held at sub-optimal allocation + // during early warning hold (if there was one) + if isHoldableCongestionState(cscd.fromState) && cscd.toState == bwe.CongestionStateNone && s.state == streamAllocatorStateStable { + update := NewStreamStateUpdate() + for _, track := range s.getTracks() { + allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, false) + updateStreamStateChange(track, allocation, update) } - - s.isHolding = false + s.maybeSendUpdate(update) } - if cscd.congestionState == bwe.CongestionStateCongested { + if cscd.toState == bwe.CongestionStateCongested { if s.activeProbeClusterId != ccutils.ProbeClusterIdInvalid { if !s.activeProbeCongesting { s.activeProbeCongesting = true @@ -910,9 +902,10 @@ func (s *StreamAllocator) allocateTrack(track *Track) { s.maybeStopProbe() // if not deficient, free pass allocate track - if !s.enabled || s.state == streamAllocatorStateStable || !track.IsManaged() { + bweCongestionState := s.params.BWE.CongestionState() + if !s.enabled || (s.state == streamAllocatorStateStable && !isDeficientCongestionState(bweCongestionState)) || !track.IsManaged() { update := NewStreamStateUpdate() - allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, s.isHolding) + allocation := track.AllocateOptimal(FlagAllowOvershootWhileOptimal, isHoldableCongestionState(bweCongestionState)) updateStreamStateChange(track, allocation, update) s.maybeSendUpdate(update) return @@ -1445,4 +1438,12 @@ func updateStreamStateChange(track *Track, allocation sfu.VideoAllocation, updat } } +func isHoldableCongestionState(bweCongestionState bwe.CongestionState) bool { + return bweCongestionState == bwe.CongestionStateEarlyWarning || bweCongestionState == bwe.CongestionStateEarlyWarningHangover +} + +func isDeficientCongestionState(bweCongestionState bwe.CongestionState) bool { + return bweCongestionState == bwe.CongestionStateCongested || bweCongestionState == bwe.CongestionStateCongestedHangover +} + // ------------------------------------------------