diff --git a/pkg/sfu/buffer/buffer_base.go b/pkg/sfu/buffer/buffer_base.go index e99c73aa8..dd82b6b88 100644 --- a/pkg/sfu/buffer/buffer_base.go +++ b/pkg/sfu/buffer/buffer_base.go @@ -866,6 +866,11 @@ func (b *BufferBase) HandleIncomingPacketLocked( return 0, fmt.Errorf("unhandled reason: %s", flowState.UnhandledReason.String()) } + if isRTX && flowState.IsLossFilled { + // RTX retransmissions and FEC recoveries both arrive as repairs + b.rtpStats.UpdateRepaired() + } + if b.params.IsOOBSequenceNumber { b.updateOOBNACKState(oobSequenceNumber, arrivalTime, len(rawPkt)) } else { diff --git a/pkg/sfu/rtpstats/rtpstats_base.go b/pkg/sfu/rtpstats/rtpstats_base.go index 25697edd4..821441b34 100644 --- a/pkg/sfu/rtpstats/rtpstats_base.go +++ b/pkg/sfu/rtpstats/rtpstats_base.go @@ -117,6 +117,9 @@ type snapshot struct { fecPacketsDiscarded uint64 fecPacketsRecovered uint64 + // lost packets filled by a repair (RTX or FEC recovery), see RTPStatsReceiver.UpdateRepaired + packetsRepaired uint64 + frames uint32 plis uint32 @@ -143,6 +146,7 @@ func (s *snapshot) MarshalLogObject(e zapcore.ObjectEncoder) error { e.AddUint64("fecBytes", s.fecBytes) e.AddUint64("fecPacketsDiscarded", s.fecPacketsDiscarded) e.AddUint64("fecPacketsRecovered", s.fecPacketsRecovered) + e.AddUint64("packetsRepaired", s.packetsRepaired) e.AddUint32("frames", s.frames) e.AddUint32("plis", s.plis) e.AddUint32("firs", s.firs) @@ -244,6 +248,9 @@ type rtpStatsBase struct { fecPacketsDiscarded uint64 fecPacketsRecovered uint64 + // lost packets filled by a repair (RTX or FEC recovery), see RTPStatsReceiver.UpdateRepaired + packetsRepaired uint64 + frames uint32 jitter float64 @@ -311,6 +318,7 @@ func (r *rtpStatsBase) seed(from *rtpStatsBase) bool { r.fecBytes = from.fecBytes r.fecPacketsDiscarded = from.fecPacketsDiscarded r.fecPacketsRecovered = from.fecPacketsRecovered + r.packetsRepaired = from.packetsRepaired r.frames = from.frames @@ -646,6 +654,7 @@ func (r *rtpStatsBase) marshalLogObject( e.AddUint64("fecBytes", r.fecBytes) e.AddUint64("fecPacketsDiscarded", r.fecPacketsDiscarded) e.AddUint64("fecPacketsRecovered", r.fecPacketsRecovered) + e.AddUint64("packetsRepaired", r.packetsRepaired) e.AddUint32("frames", r.frames) e.AddFloat64("frameRate", float64(r.frames)/elapsedSeconds) @@ -881,6 +890,7 @@ func (r *rtpStatsBase) getSnapshot(startTime int64, extStartSN uint64) snapshot fecBytes: r.fecBytes, fecPacketsDiscarded: r.fecPacketsDiscarded, fecPacketsRecovered: r.fecPacketsRecovered, + packetsRepaired: r.packetsRepaired, frames: r.frames, plis: r.plis, firs: r.firs, diff --git a/pkg/sfu/rtpstats/rtpstats_receiver.go b/pkg/sfu/rtpstats/rtpstats_receiver.go index 193e44363..1e7ca5846 100644 --- a/pkg/sfu/rtpstats/rtpstats_receiver.go +++ b/pkg/sfu/rtpstats/rtpstats_receiver.go @@ -93,6 +93,9 @@ type RTPFlowState struct { IsDuplicate bool IsOutOfOrder bool + // IsLossFilled is set when the packet arrived for a sequence number that + // was counted as lost. + IsLossFilled bool ExtSequenceNumber uint64 ExtTimestamp uint64 @@ -539,6 +542,7 @@ func (r *RTPStatsReceiver) Update( flowState.IsDuplicate = true } else { r.packetsLost-- + flowState.IsLossFilled = true } } @@ -832,6 +836,19 @@ func (r *RTPStatsReceiver) LastSenderReportTime() time.Time { return time.Time{} } +// UpdateRepaired records a lost packet that was filled by a repair, an RTX +// retransmission or a FEC recovery. Receiver reports keep counting it as lost. +func (r *RTPStatsReceiver) UpdateRepaired() { + r.lock.Lock() + defer r.lock.Unlock() + + if r.endTime != 0 { + return + } + + r.packetsRepaired++ +} + func (r *RTPStatsReceiver) GetRtcpReceptionReport(ssrc uint32, proxyFracLost uint8, snapshotID uint32) *rtcp.ReceptionReport { r.lock.Lock() defer r.lock.Unlock() @@ -855,14 +872,18 @@ func (r *RTPStatsReceiver) GetRtcpReceptionReport(ssrc uint32, proxyFracLost uin return nil } - packetsLost := uint32(now.packetsLost - then.packetsLost) + // Report loss before repair, as libwebrtc receivers do: packets restored by + // RTX or recovered by FEC are not counted as received. Otherwise a sender's + // loss-based protection, such as FlexFEC, sees no loss as long as repairs + // keep up, and backs off. + packetsLost := uint32((now.packetsLost + now.packetsRepaired) - (then.packetsLost + then.packetsRepaired)) if int32(packetsLost) < 0 { packetsLost = 0 } lossRate := float32(packetsLost) / float32(packetsExpected) - fracLost := max(proxyFracLost, uint8(lossRate*256.0)) + fracLost := max(proxyFracLost, uint8(min(lossRate*256.0, 255.0))) - totalLost := min(r.packetsLost, 0xffffff) // 24-bits max + totalLost := min(r.packetsLost+r.packetsRepaired, 0xffffff) // 24-bits max lastSR := uint32(0) dlsr := uint32(0) diff --git a/pkg/sfu/rtpstats/rtpstats_test.go b/pkg/sfu/rtpstats/rtpstats_test.go index 752c749a2..0a4d0b3b0 100644 --- a/pkg/sfu/rtpstats/rtpstats_test.go +++ b/pkg/sfu/rtpstats/rtpstats_test.go @@ -269,6 +269,67 @@ func Test_RTPStatsReceiver_UpdateFEC(t *testing.T) { require.EqualValues(t, 3, stats.FecPacketsRecovered) } +func Test_RTPStatsReceiver_ReceptionReportCountsRepairedAsLost(t *testing.T) { + r := NewRTPStatsReceiver(RTPStatsParams{}) + r.SetClockRate(90000) + snapshotID := r.NewSnapshotId() + + update := func(sn uint16) RTPFlowState { + packet := getPacket(sn, 3000*uint32(sn), 1000) + return r.Update( + time.Now().UnixNano(), + packet.SequenceNumber, + packet.Timestamp, + packet.Marker, + packet.Header.MarshalSize(), + len(packet.Payload), + 0, + ) + } + + // 101-104 are lost + update(100) + for sn := uint16(105); sn < 110; sn++ { + require.False(t, update(sn).IsLossFilled) + } + // 101 and 102 are repaired, 103 is only reordered + for _, sn := range []uint16{101, 102} { + require.True(t, update(sn).IsLossFilled) + r.UpdateRepaired() + } + require.True(t, update(103).IsLossFilled) + // a duplicate does not fill a loss + require.False(t, update(102).IsLossFilled) + for sn := uint16(110); sn < 120; sn++ { + update(sn) + } + + // 20 packets expected, 1 still lost and 2 repaired + rr := r.GetRtcpReceptionReport(1234, 0, snapshotID) + require.NotNil(t, rr) + require.EqualValues(t, 38, rr.FractionLost) // 3/20 in Q8 + require.EqualValues(t, 3, rr.TotalLost) + + // a late repair does not take loss away from the next interval + require.True(t, update(104).IsLossFilled) + r.UpdateRepaired() + for sn := uint16(120); sn < 130; sn++ { + update(sn) + } + rr = r.GetRtcpReceptionReport(1234, 0, snapshotID) + require.NotNil(t, rr) + require.EqualValues(t, 0, rr.FractionLost) + require.EqualValues(t, 3, rr.TotalLost) + + // the proxied loss still applies when it is higher + for sn := uint16(130); sn < 140; sn++ { + update(sn) + } + rr = r.GetRtcpReceptionReport(1234, 64, snapshotID) + require.NotNil(t, rr) + require.EqualValues(t, 64, rr.FractionLost) +} + func Test_RTPStatsReceiver_Restart(t *testing.T) { clockRate := uint32(90000) r := NewRTPStatsReceiver(RTPStatsParams{})