diff --git a/pkg/sfu/buffer/rtpstats.go b/pkg/sfu/buffer/rtpstats.go index 0265c95c5..43f6a1a77 100644 --- a/pkg/sfu/buffer/rtpstats.go +++ b/pkg/sfu/buffer/rtpstats.go @@ -192,29 +192,7 @@ func (r *RTPStats) Update(rtph *rtp.Header, payloadSize int, paddingSize int, pa } // adjust start to account for out-of-order packets before a cycle completes - if !r.isCycleCompleted() && (rtph.SequenceNumber-uint16(r.extStartSN)) > (1<<15) { - // LK-DEBUG-REMOVE START - r.logger.Debugw( - "RTPSTATS_DEBUG moving starting SN back", - "diff", rtph.SequenceNumber-uint16(r.extStartSN), - "loss added", uint16(r.extStartSN)-rtph.SequenceNumber, - "highestSN", r.highestSN, - "sn", rtph.SequenceNumber, - "startSN", r.extStartSN, - "lost", r.packetsLost, - "highestTS", r.highestTS, - "ts", rtph.Timestamp, - "tsDiff", rtph.Timestamp-r.highestTS, - "highestTime", r.highestTime, - "now", packetTime, - "timeDiff(ms)", float64(packetTime-r.highestTime)/float64(1e6), - ) - // LK-DEBUG-REMOVE END - // NOTE: current sequence number is counted as loss as it will be deducted in the duplicate check below - r.packetsLost += uint32(uint16(r.extStartSN) - rtph.SequenceNumber) - r.extStartSN = uint32(rtph.SequenceNumber) - r.logger.Debugw("RTPSTATS_DEBUG, setting extStartSN moving back", "extStartSN", r.extStartSN) // LK-DEBUG-REMOVE - } + r.maybeAdjustStartSN(rtph, packetTime) if r.isSeenSN(rtph.SequenceNumber) { r.bytesDuplicate += pktSize @@ -289,6 +267,38 @@ func (r *RTPStats) Update(rtph *rtp.Header, payloadSize int, paddingSize int, pa return } +func (r *RTPStats) maybeAdjustStartSN(rtph *rtp.Header, packetTime int64) { + if (r.getExtHighestSN() - r.extStartSN + 1) >= (NumSequenceNumbers / 2) { + return + } + + if (rtph.SequenceNumber - uint16(r.extStartSN)) < (1 << 15) { + return + } + + // LK-DEBUG-REMOVE START + r.logger.Debugw( + "RTPSTATS_DEBUG moving starting SN back", + "diff", rtph.SequenceNumber-uint16(r.extStartSN), + "loss added", uint16(r.extStartSN)-rtph.SequenceNumber, + "highestSN", r.highestSN, + "sn", rtph.SequenceNumber, + "startSN", r.extStartSN, + "lost", r.packetsLost, + "highestTS", r.highestTS, + "ts", rtph.Timestamp, + "tsDiff", rtph.Timestamp-r.highestTS, + "highestTime", r.highestTime, + "now", packetTime, + "timeDiff(ms)", float64(packetTime-r.highestTime)/float64(1e6), + ) + // LK-DEBUG-REMOVE END + // NOTE: current sequence number is counted as loss as it will be deducted in the duplicate check + r.packetsLost += uint32(uint16(r.extStartSN) - rtph.SequenceNumber) + r.extStartSN = uint32(rtph.SequenceNumber) + r.logger.Debugw("RTPSTATS_DEBUG, setting extStartSN moving back", "extStartSN", r.extStartSN) // LK-DEBUG-REMOVE +} + func (r *RTPStats) GetTotalPacketsPrimary() uint32 { r.lock.RLock() defer r.lock.RUnlock() @@ -817,10 +827,6 @@ func (r *RTPStats) getExtHighestSN() uint32 { return (uint32(r.cycles) << 16) | uint32(r.highestSN) } -func (r *RTPStats) isCycleCompleted() bool { - return (r.getExtHighestSN() - r.extStartSN) >= NumSequenceNumbers -} - func (r *RTPStats) getNumPacketsSeen() uint32 { packetsExpected := r.getExtHighestSN() - r.extStartSN + 1 if r.packetsLost > packetsExpected {