From dd995899bf25120aed3e418f173bd745eff71422 Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Wed, 19 Jul 2023 12:50:03 +0530 Subject: [PATCH] Handle extreme case of sender report lagging. (#1892) --- pkg/sfu/buffer/rtpstats.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/pkg/sfu/buffer/rtpstats.go b/pkg/sfu/buffer/rtpstats.go index f5bb0a5d9..075fe72fe 100644 --- a/pkg/sfu/buffer/rtpstats.go +++ b/pkg/sfu/buffer/rtpstats.go @@ -793,7 +793,9 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) { r.logger.Infow( "received sender report, out-of-order, resetting", "prevTSExt", r.srNewest.RTPTimestampExt, + "prevNTP", r.srNewest.NTPTimestamp.Time().String(), "currTSExt", srDataCopy.RTPTimestampExt, + "currNTP", srDataCopy.NTPTimestamp.Time().String(), ) r.srFirst = &srDataCopy r.srNewest = &srDataCopy @@ -897,6 +899,27 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32, calculatedClockRate uint32) } } + if r.srNewest != nil && nowRTPExt < r.srNewest.RTPTimestampExt { + // If report being generated is behind, use the time different and clock rate of codec to produce next report. + // Current report could be behind due to the following + // - Publisher pacing + // - Due to above, report from publisher side is ahead of packet timestamps. + // Note that report will map wall clock to timestamp at capture time and happens before the pacer. + // - Pause/Mute followed by resume, some combination of events that could + // result in this module not having calculated clock rate of publisher side. + // - When the above happens, current will be generated using highestTS which could be behind. + // That could end up behind the last report's timestamp in extreme cases + r.logger.Infow( + "sending sender report, out-of-order, repairing", + "prevTSExt", r.srNewest.RTPTimestampExt, + "prevNTP", r.srNewest.NTPTimestamp.Time().String(), + "currTSExt", nowRTPExt, + "currNTP", nowNTP.Time().String(), + ) + ntpDiffSinceLast := nowNTP.Time().Sub(r.srNewest.NTPTimestamp.Time()) + nowRTPExt = r.srNewest.RTPTimestampExt + uint64(ntpDiffSinceLast.Seconds()*float64(r.params.ClockRate)) + } + // monitor and log RTP timestamp anomalies var ntpDiffSinceLast time.Duration var rtpDiffSinceLast uint32