Use start time stamp to calculate down stream sender report. (#2564)

* Use start time stamp to calculate down stream sender report.

With first packet time adjustment, using the first time stamp is more
accurate.

This still suffers if the up stream clock rate changes (happens in cases
like noise suppression which is not well understood). Will be looking at
pass through of sender report from publisher to subscriber.

* similar log strings

* avoid early sender reports

* log messages

* Reduce first packet adjustment threshold to 15 seconds
This commit is contained in:
Raja Subramanian
2024-03-10 23:18:54 +05:30
committed by GitHub
parent a08b058abc
commit bdbc9dcbc7
3 changed files with 47 additions and 66 deletions
+12 -12
View File
@@ -34,7 +34,7 @@ const (
cFirstSnapshotID = 1 cFirstSnapshotID = 1
cFirstPacketTimeAdjustWindow = 2 * time.Minute cFirstPacketTimeAdjustWindow = 2 * time.Minute
cFirstPacketTimeAdjustThreshold = 5 * time.Minute cFirstPacketTimeAdjustThreshold = 15 * time.Second
) )
// ------------------------------------------------------- // -------------------------------------------------------
@@ -506,6 +506,17 @@ func (r *rtpStatsBase) maybeAdjustFirstPacketTime(ts uint32, startTS uint32) {
now := r.firstTime.Add(timeSinceFirst) now := r.firstTime.Add(timeSinceFirst)
firstTime := now.Add(-samplesDuration) firstTime := now.Add(-samplesDuration)
if firstTime.Before(r.firstTime) { if firstTime.Before(r.firstTime) {
if r.firstTime.Sub(firstTime) > cFirstPacketTimeAdjustThreshold {
r.logger.Infow("adjusting first packet time, too big, ignoring",
"startTime", r.startTime.String(),
"nowTime", now.String(),
"before", r.firstTime.String(),
"after", firstTime.String(),
"adjustment", r.firstTime.Sub(firstTime).String(),
"nowTS", ts,
"startTS", startTS,
)
} else {
r.logger.Debugw( r.logger.Debugw(
"adjusting first packet time", "adjusting first packet time",
"startTime", r.startTime.String(), "startTime", r.startTime.String(),
@@ -516,17 +527,6 @@ func (r *rtpStatsBase) maybeAdjustFirstPacketTime(ts uint32, startTS uint32) {
"nowTS", ts, "nowTS", ts,
"startTS", startTS, "startTS", startTS,
) )
if r.firstTime.Sub(firstTime) > cFirstPacketTimeAdjustThreshold {
r.logger.Infow("first packet time adjustment too big, ignoring",
"startTime", r.startTime.String(),
"nowTime", now.String(),
"before", r.firstTime.String(),
"after", firstTime.String(),
"adjustment", r.firstTime.Sub(firstTime).String(),
"nowTS", ts,
"startTS", startTS,
)
} else {
r.firstTime = firstTime r.firstTime = firstTime
} }
} }
+2 -2
View File
@@ -251,7 +251,7 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
// prevent against extreme case of anachronous sender reports // prevent against extreme case of anachronous sender reports
if r.srNewest != nil && r.srNewest.NTPTimestamp > srData.NTPTimestamp { if r.srNewest != nil && r.srNewest.NTPTimestamp > srData.NTPTimestamp {
r.logger.Infow( r.logger.Infow(
"received anachronous sender report", "received sender report, anachronous, dropping",
"first", r.srFirst, "first", r.srFirst,
"last", r.srNewest, "last", r.srNewest,
"current", srData, "current", srData,
@@ -311,7 +311,7 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
(timeSinceFirst > 0.2 && math.Abs(float64(r.params.ClockRate)-calculatedClockRateFromFirst) > 0.2*float64(r.params.ClockRate)) { (timeSinceFirst > 0.2 && math.Abs(float64(r.params.ClockRate)-calculatedClockRateFromFirst) > 0.2*float64(r.params.ClockRate)) {
if r.clockSkewCount%100 == 0 { if r.clockSkewCount%100 == 0 {
r.logger.Infow( r.logger.Infow(
"clock rate skew", "received sender report, clock skew",
"first", r.srFirst, "first", r.srFirst,
"last", r.srNewest, "last", r.srNewest,
"current", &srDataCopy, "current", &srDataCopy,
+15 -34
View File
@@ -29,6 +29,8 @@ import (
const ( const (
cSnInfoSize = 4096 cSnInfoSize = 4096
cSnInfoMask = cSnInfoSize - 1 cSnInfoMask = cSnInfoSize - 1
cSenderReportInitialWait = time.Second
) )
type snInfoFlag byte type snInfoFlag byte
@@ -158,7 +160,6 @@ type RTPStatsSender struct {
senderSnapshots []senderSnapshot senderSnapshots []senderSnapshot
clockSkewCount int clockSkewCount int
outOfOrderSenderReportCount int
metadataCacheOverflowCount int metadataCacheOverflowCount int
srFeedFirst *RTCPSenderReportData srFeedFirst *RTCPSenderReportData
@@ -648,13 +649,13 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
// construct current time based on monotonic clock // construct current time based on monotonic clock
timeSinceFirst := time.Since(r.firstTime) timeSinceFirst := time.Since(r.firstTime)
if timeSinceFirst < cSenderReportInitialWait {
return nil
}
now := r.firstTime.Add(timeSinceFirst) now := r.firstTime.Add(timeSinceFirst)
nowNTP := mediatransportutil.ToNtpTime(now) nowNTP := mediatransportutil.ToNtpTime(now)
nowRTPExtUsingTime := r.extStartTS + uint64(timeSinceFirst.Nanoseconds()*int64(r.params.ClockRate)/1e9)
timeSinceHighest := now.Sub(r.highestTime) nowRTPExt := nowRTPExtUsingTime
nowRTPExt := r.extHighestTS + uint64(timeSinceHighest.Nanoseconds()*int64(r.params.ClockRate)/1e9)
nowRTPExtUsingTime := nowRTPExt
nowRTP := uint32(nowRTPExt)
// It is possible that publisher is pacing at a slower rate. // It is possible that publisher is pacing at a slower rate.
// That would make `highestTS` to be lagging the RTP time stamp in the RTCP Sender Report from publisher. // That would make `highestTS` to be lagging the RTP time stamp in the RTCP Sender Report from publisher.
@@ -664,13 +665,12 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
nowRTPExtUsingRate = r.extStartTS + uint64(float64(calculatedClockRate)*timeSinceFirst.Seconds()) nowRTPExtUsingRate = r.extStartTS + uint64(float64(calculatedClockRate)*timeSinceFirst.Seconds())
if nowRTPExtUsingRate > nowRTPExt { if nowRTPExtUsingRate > nowRTPExt {
nowRTPExt = nowRTPExtUsingRate nowRTPExt = nowRTPExtUsingRate
nowRTP = uint32(nowRTPExt)
} }
} }
srData := &RTCPSenderReportData{ srData := &RTCPSenderReportData{
NTPTimestamp: nowNTP, NTPTimestamp: nowNTP,
RTPTimestamp: nowRTP, RTPTimestamp: uint32(nowRTPExt),
RTPTimestampExt: nowRTPExt, RTPTimestampExt: nowRTPExt,
At: now, At: now,
} }
@@ -691,7 +691,7 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
"extStartTS", r.extStartTS, "extStartTS", r.extStartTS,
"extHighestTS", r.extHighestTS, "extHighestTS", r.extHighestTS,
"highestTime", r.highestTime.String(), "highestTime", r.highestTime.String(),
"timeSinceHighest", timeSinceHighest.String(), "timeSinceHighest", now.Sub(r.highestTime).String(),
"firstTime", r.firstTime.String(), "firstTime", r.firstTime.String(),
"timeSinceFirst", timeSinceFirst.String(), "timeSinceFirst", timeSinceFirst.String(),
"nowRTPExtUsingTime", nowRTPExtUsingTime, "nowRTPExtUsingTime", nowRTPExtUsingTime,
@@ -708,20 +708,10 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
} }
if r.srNewest != nil && nowRTPExt < r.srNewest.RTPTimestampExt { if r.srNewest != nil && nowRTPExt < r.srNewest.RTPTimestampExt {
// If report being generated is behind, use the time difference and // If report being generated is behind the last report, skip it.
// clock rate of codec to produce next report. // Should not happen.
//
// 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
if r.outOfOrderSenderReportCount%10 == 0 {
r.logger.Infow( r.logger.Infow(
"sending sender report, out-of-order, repairing", "sending sender report, out-of-order, skipping",
"first", r.srFirst, "first", r.srFirst,
"last", r.srNewest, "last", r.srNewest,
"curr", srData, "curr", srData,
@@ -731,23 +721,14 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
"extStartTS", r.extStartTS, "extStartTS", r.extStartTS,
"extHighestTS", r.extHighestTS, "extHighestTS", r.extHighestTS,
"highestTime", r.highestTime.String(), "highestTime", r.highestTime.String(),
"timeSinceHighest", timeSinceHighest.String(), "timeSinceHighest", now.Sub(r.highestTime).String(),
"firstTime", r.firstTime.String(), "firstTime", r.firstTime.String(),
"timeSinceFirst", timeSinceFirst.String(), "timeSinceFirst", timeSinceFirst.String(),
"nowRTPExtUsingTime", nowRTPExtUsingTime, "nowRTPExtUsingTime", nowRTPExtUsingTime,
"calculatedClockRate", calculatedClockRate, "calculatedClockRate", calculatedClockRate,
"nowRTPExtUsingRate", nowRTPExtUsingRate, "nowRTPExtUsingRate", nowRTPExtUsingRate,
"count", r.outOfOrderSenderReportCount,
) )
} return nil
r.outOfOrderSenderReportCount++
ntpDiffSinceLast := nowNTP.Time().Sub(r.srNewest.NTPTimestamp.Time())
nowRTPExt = r.srNewest.RTPTimestampExt + uint64(ntpDiffSinceLast.Seconds()*float64(r.params.ClockRate))
nowRTP = uint32(nowRTPExt)
srData.RTPTimestamp = nowRTP
srData.RTPTimestampExt = nowRTPExt
} }
r.srNewest = srData r.srNewest = srData
@@ -758,7 +739,7 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, calculatedClockRate ui
return &rtcp.SenderReport{ return &rtcp.SenderReport{
SSRC: ssrc, SSRC: ssrc,
NTPTime: uint64(nowNTP), NTPTime: uint64(nowNTP),
RTPTime: nowRTP, RTPTime: uint32(nowRTPExt),
PacketCount: uint32(r.getTotalPacketsPrimary(r.extStartSN, r.extHighestSN) + r.packetsDuplicate + r.packetsPadding), PacketCount: uint32(r.getTotalPacketsPrimary(r.extStartSN, r.extHighestSN) + r.packetsDuplicate + r.packetsPadding),
OctetCount: uint32(r.bytes + r.bytesDuplicate + r.bytesPadding), OctetCount: uint32(r.bytes + r.bytesDuplicate + r.bytesPadding),
} }