Prevent large spikes in propagation delay (#2615)

* Prevent large spikes in propagation delay

A few tweaks
- Large spike in propagation delay due to congested channel results in
  long term estimate getting high value. Ignore outliers in long term
  estimate.
- Introduce a new field for adjusted arrival time as adjusting the
  arrival time in place meant it got applied again across the relay and
  that caused different propagation delay on remote nodes.
- Reset path change counters as long as there is any sample that is not
  higher than the multiple of long term. There was a case of
  o Sample with high value that triggered path change start.
  o Then some samples with high enough delta, but did not meet the
    criteria for increasing counter further.
  o Some time later, another sample met the threshold and that triggered
    a path change re-init.

* do not adapt to large delta
This commit is contained in:
Raja Subramanian
2024-04-02 14:21:20 +05:30
committed by GitHub
parent 3c8980b443
commit 860702e9dc
3 changed files with 31 additions and 20 deletions
+4 -2
View File
@@ -114,6 +114,7 @@ type RTCPSenderReportData struct {
RTPTimestampExt uint64 RTPTimestampExt uint64
NTPTimestamp mediatransportutil.NtpTime NTPTimestamp mediatransportutil.NtpTime
At time.Time At time.Time
AtAdjusted time.Time
} }
func (r *RTCPSenderReportData) ToString() string { func (r *RTCPSenderReportData) ToString() string {
@@ -121,7 +122,7 @@ func (r *RTCPSenderReportData) ToString() string {
return "" return ""
} }
return fmt.Sprintf("ntp: %s, rtp: %d, extRtp: %d, at: %s", r.NTPTimestamp.Time().String(), r.RTPTimestamp, r.RTPTimestampExt, r.At.String()) return fmt.Sprintf("ntp: %s, rtp: %d, extRtp: %d, at: %s, atAdj: %s", r.NTPTimestamp.Time().String(), r.RTPTimestamp, r.RTPTimestampExt, r.At.String(), r.AtAdjusted.String())
} }
func (r *RTCPSenderReportData) MarshalLogObject(e zapcore.ObjectEncoder) error { func (r *RTCPSenderReportData) MarshalLogObject(e zapcore.ObjectEncoder) error {
@@ -133,6 +134,7 @@ func (r *RTCPSenderReportData) MarshalLogObject(e zapcore.ObjectEncoder) error {
e.AddUint32("RTPTimestamp", r.RTPTimestamp) e.AddUint32("RTPTimestamp", r.RTPTimestamp)
e.AddUint64("RTPTimestampExt", r.RTPTimestampExt) e.AddUint64("RTPTimestampExt", r.RTPTimestampExt)
e.AddTime("At", r.At) e.AddTime("At", r.At)
e.AddTime("AtAdjusted", r.AtAdjusted)
return nil return nil
} }
@@ -495,7 +497,7 @@ func (r *rtpStatsBase) maybeAdjustFirstPacketTime(srData *RTCPSenderReportData,
// abnormal delay (maybe due to pacing or maybe due to queuing // abnormal delay (maybe due to pacing or maybe due to queuing
// in some network element along the way), push back first time // in some network element along the way), push back first time
// to an earlier instance. // to an earlier instance.
timeSinceReceive := time.Since(srData.At) timeSinceReceive := time.Since(srData.AtAdjusted)
extNowTS := srData.RTPTimestampExt - tsOffset + uint64(timeSinceReceive.Nanoseconds()*int64(r.params.ClockRate)/1e9) extNowTS := srData.RTPTimestampExt - tsOffset + uint64(timeSinceReceive.Nanoseconds()*int64(r.params.ClockRate)/1e9)
samplesDiff := int64(extNowTS - extStartTS) samplesDiff := int64(extNowTS - extStartTS)
if samplesDiff < 0 { if samplesDiff < 0 {
+24 -16
View File
@@ -43,20 +43,19 @@ const (
cPropagationDelaySpikeAdaptationFactor = float64(0.5) cPropagationDelaySpikeAdaptationFactor = float64(0.5)
cPropagationDelayDeltaMaxInterval = 10 * time.Second
// To account for path changes mid-stream, if the delta of the propagation delay is consistently higher, reset. // To account for path changes mid-stream, if the delta of the propagation delay is consistently higher, reset.
// Reset at whichever of the below happens later. // Reset at whichever of the below happens later.
// 1. 10 seconds of persistent high delta. // 1. 10 seconds of persistent high delta.
// 2. at least 2 reports with high delta. // 2. at least 2 consecutive reports with high delta.
// //
// A long term version of delta of propagation delay is maintained and delta propagation delay exceeding // A long term estimate of delta of propagation delay is maintained and delta propagation delay exceeding
// a factor of the long term version is considered a sharp increase. That will trigger the start of the // a factor of the long term estimate is considered a sharp increase. That will trigger the start of the
// path change condition and if it persists, propagation delay will be reset. // path change condition and if it persists, propagation delay will be reset.
cPropagationDelayDeltaThresholdMin = 10 * time.Millisecond cPropagationDelayDeltaThresholdMin = 10 * time.Millisecond
cPropagationDelayDeltaThresholdMaxFactor = 2 cPropagationDelayDeltaThresholdMaxFactor = 2
cPropagationDelayDeltaHighResetNumReports = 2 cPropagationDelayDeltaHighResetNumReports = 2
cPropagationDelayDeltaHighResetWait = 10 * time.Second cPropagationDelayDeltaHighResetWait = 10 * time.Second
cPropagationDelayDeltaLongTermAdaptationThreshold = 50 * time.Millisecond
) )
type RTPFlowState struct { type RTPFlowState struct {
@@ -369,6 +368,7 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
"receivedDeltaPropagationDelay", deltaPropagationDelay.String(), "receivedDeltaPropagationDelay", deltaPropagationDelay.String(),
"deltaHighCount", r.propagationDelayDeltaHighCount, "deltaHighCount", r.propagationDelayDeltaHighCount,
"sinceDeltaHighStart", time.Since(r.propagationDelayDeltaHighStartTime).String(), "sinceDeltaHighStart", time.Since(r.propagationDelayDeltaHighStartTime).String(),
"propagationDelaySpike", r.propagationDelaySpike.String(),
"first", r.srFirst, "first", r.srFirst,
"last", r.srNewest, "last", r.srNewest,
"current", &srDataCopy, "current", &srDataCopy,
@@ -395,9 +395,11 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
r.logger.Debugw("initializing propagation delay", getPropagationFields()...) r.logger.Debugw("initializing propagation delay", getPropagationFields()...)
} else { } else {
deltaPropagationDelay = propagationDelay - r.propagationDelay deltaPropagationDelay = propagationDelay - r.propagationDelay
if deltaPropagationDelay.Abs() > cPropagationDelayDeltaThresholdMin { // ignore small changes for path change consideration if deltaPropagationDelay > cPropagationDelayDeltaThresholdMin { // ignore small changes for path change consideration
if r.longTermDeltaPropagationDelay != 0 && deltaPropagationDelay > 0 && deltaPropagationDelay > r.longTermDeltaPropagationDelay*time.Duration(cPropagationDelayDeltaThresholdMaxFactor) { if r.longTermDeltaPropagationDelay != 0 &&
r.logger.Debugw("sharp increase in propagation delay, skipping", getPropagationFields()...) // TODO-REMOVE deltaPropagationDelay > 0 &&
deltaPropagationDelay > r.longTermDeltaPropagationDelay*time.Duration(cPropagationDelayDeltaThresholdMaxFactor) {
r.logger.Debugw("sharp increase in propagation delay", getPropagationFields()...) // TODO-REMOVE
r.propagationDelayDeltaHighCount++ r.propagationDelayDeltaHighCount++
if r.propagationDelayDeltaHighStartTime.IsZero() { if r.propagationDelayDeltaHighStartTime.IsZero() {
r.propagationDelayDeltaHighStartTime = time.Now() r.propagationDelayDeltaHighStartTime = time.Now()
@@ -412,6 +414,8 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
r.logger.Debugw("re-initializing propagation delay", append(getPropagationFields(), "newPropagationDelay", propagationDelay.String())...) r.logger.Debugw("re-initializing propagation delay", append(getPropagationFields(), "newPropagationDelay", propagationDelay.String())...)
initPropagationDelay(r.propagationDelaySpike) initPropagationDelay(r.propagationDelaySpike)
} }
} else {
resetDelta()
} }
} else { } else {
resetDelta() resetDelta()
@@ -432,13 +436,17 @@ func (r *RTPStatsReceiver) SetRtcpSenderReportData(srData *RTCPSenderReportData)
if r.longTermDeltaPropagationDelay == 0 { if r.longTermDeltaPropagationDelay == 0 {
r.longTermDeltaPropagationDelay = deltaPropagationDelay r.longTermDeltaPropagationDelay = deltaPropagationDelay
} else { } else {
sinceLastReport := srDataCopy.NTPTimestamp.Time().Sub(r.srNewest.NTPTimestamp.Time()) if deltaPropagationDelay < cPropagationDelayDeltaLongTermAdaptationThreshold {
adaptationFactor := min(1.0, float64(sinceLastReport)/float64(cPropagationDelayDeltaMaxInterval)) // do not adapt to large +ve spikes, can happen when channel is congested and reports are delivered very late
r.longTermDeltaPropagationDelay += time.Duration(adaptationFactor * float64(deltaPropagationDelay-r.longTermDeltaPropagationDelay)) // if the spike is in fact a path change, it will persist and handled by path change detection above
sinceLastReport := srDataCopy.NTPTimestamp.Time().Sub(r.srNewest.NTPTimestamp.Time())
adaptationFactor := min(1.0, float64(sinceLastReport)/float64(cPropagationDelayDeltaHighResetWait))
r.longTermDeltaPropagationDelay += time.Duration(adaptationFactor * float64(deltaPropagationDelay-r.longTermDeltaPropagationDelay))
}
} }
} }
// adjust receive time to estimated propagation delay // adjust receive time to estimated propagation delay
srDataCopy.At = ntpTime.Add(r.propagationDelay) srDataCopy.AtAdjusted = ntpTime.Add(r.propagationDelay)
r.srNewest = &srDataCopy r.srNewest = &srDataCopy
r.maybeAdjustFirstPacketTime(r.srNewest, 0, r.timestamp.GetExtendedStart()) r.maybeAdjustFirstPacketTime(r.srNewest, 0, r.timestamp.GetExtendedStart())
+3 -2
View File
@@ -633,8 +633,8 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, publisherSRData *RTCPS
return nil return nil
} }
timeSincePublisherSR := time.Since(publisherSRData.At) timeSincePublisherSR := time.Since(publisherSRData.AtAdjusted)
now := publisherSRData.At.Add(timeSincePublisherSR) now := publisherSRData.AtAdjusted.Add(timeSincePublisherSR)
nowNTP := mediatransportutil.ToNtpTime(now) nowNTP := mediatransportutil.ToNtpTime(now)
nowRTPExt := publisherSRData.RTPTimestampExt - tsOffset + uint64(timeSincePublisherSR.Nanoseconds()*int64(r.params.ClockRate)/1e9) nowRTPExt := publisherSRData.RTPTimestampExt - tsOffset + uint64(timeSincePublisherSR.Nanoseconds()*int64(r.params.ClockRate)/1e9)
@@ -643,6 +643,7 @@ func (r *RTPStatsSender) GetRtcpSenderReport(ssrc uint32, publisherSRData *RTCPS
RTPTimestamp: uint32(nowRTPExt), RTPTimestamp: uint32(nowRTPExt),
RTPTimestampExt: nowRTPExt, RTPTimestampExt: nowRTPExt,
At: now, At: now,
AtAdjusted: now,
} }
getFields := func() []interface{} { getFields := func() []interface{} {