More time stamp dance. (#1712)

Two things
- Somehow the publisher RTCP sender report time stamp goes back some
  times. Log it differently. Also, use signed type for logging so
  that negative is easy to see.
- On down track, because of silence frame injection on mute, the RTCP
  sender report time stamp might be ahead of timestamp we will use
  on unmute. If so, ensure that next timestamp is also not before
  what was sent in RTCP sender report.
This commit is contained in:
Raja Subramanian
2023-05-16 21:48:10 +05:30
committed by GitHub
parent c79e0ce06f
commit 9395f0b1fb
3 changed files with 39 additions and 25 deletions
+22 -17
View File
@@ -744,29 +744,34 @@ func (r *RTPStats) SetRtcpSenderReportData(srData *RTCPSenderReportData) {
}
// monitor and log RTP timestamp anomalies
isWarped := false
if r.srData != nil {
ntpDiffSinceLast := srData.NTPTimestamp.Time().Sub(r.srData.NTPTimestamp.Time())
rtpDiffSinceLast := srData.RTPTimestamp - r.srData.RTPTimestamp
arrivalDiffSinceLast := srData.ArrivalTime.Sub(r.srData.ArrivalTime)
expectedTimeDiffSinceLast := float64(rtpDiffSinceLast) / float64(r.params.ClockRate)
if math.Abs(expectedTimeDiffSinceLast-ntpDiffSinceLast.Seconds()) > 0.2 {
// more than 200 ms away from expected delta
isWarped = true
var reason string
if (srData.RTPTimestamp - r.srData.RTPTimestamp) > (1 << 31) {
reason = "received sender report, out-of-order"
} else {
if math.Abs(expectedTimeDiffSinceLast-ntpDiffSinceLast.Seconds()) > 0.2 {
// more than 200 ms away from expected delta
reason = "received sender report, time warp"
}
}
if isWarped {
if reason != "" {
timeSinceFirst, rtpDiffSinceFirst, drift, driftMs, sampleRate := r.getDrift()
r.logger.Infow(
"received sender report, time warp",
reason,
"ntp", srData.NTPTimestamp.Time().String(),
"rtp", srData.RTPTimestamp,
"arrival", srData.ArrivalTime.String(),
"ntpDiffSincelast", ntpDiffSinceLast.Seconds(),
"rtpDiffSincelast", rtpDiffSinceLast,
"arrivalDiffSincelast", arrivalDiffSinceLast.Seconds(),
"expectedTimeDiffSincelast", expectedTimeDiffSinceLast,
"ntpDiffSinceLast", ntpDiffSinceLast.Seconds(),
"rtpDiffSinceLast", int32(rtpDiffSinceLast),
"arrivalDiffSinceLast", arrivalDiffSinceLast.Seconds(),
"expectedTimeDiffSinceLast", expectedTimeDiffSinceLast,
"timeSinceFirst", timeSinceFirst.Seconds(),
"rtpDiffSinceFirst", rtpDiffSinceFirst,
"drift", drift,
@@ -792,12 +797,12 @@ func (r *RTPStats) GetRtcpSenderReportData() *RTCPSenderReportData {
return &srDataCopy
}
func (r *RTPStats) GetExpectedRTPTimestamp(at time.Time) (uint32, error) {
func (r *RTPStats) GetExpectedRTPTimestamp(at time.Time) (uint32, uint32, error) {
r.lock.RLock()
defer r.lock.RUnlock()
if !r.initialized {
return 0, errors.New("uninitilaized")
return 0, 0, errors.New("uninitilaized")
}
timeDiff := at.Sub(r.firstTime)
@@ -815,7 +820,7 @@ func (r *RTPStats) GetExpectedRTPTimestamp(at time.Time) (uint32, error) {
"highestTS", r.highestTS,
"highestTime", r.highestTime.String(),
)
return uint32(expectedExtRTP), nil
return uint32(expectedExtRTP), r.lastSRRTP, nil
}
func (r *RTPStats) GetRtcpSenderReport(ssrc uint32) (*rtcp.SenderReport, float64) {
@@ -866,10 +871,10 @@ func (r *RTPStats) GetRtcpSenderReport(ssrc uint32) (*rtcp.SenderReport, float64
"rtp", nowRTP,
"expectedRTP", uint32(expectedExtRTP),
"departure", now.String(),
"ntpDiffSincelast", ntpDiffSinceLast.Seconds(),
"rtpDiffSincelast", rtpDiffSinceLast,
"departureDiffSincelast", departureDiffSinceLast.Seconds(),
"expectedTimeDiffSincelast", expectedTimeDiffSinceLast,
"ntpDiffSinceLast", ntpDiffSinceLast.Seconds(),
"rtpDiffSinceLast", rtpDiffSinceLast,
"departureDiffSinceLast", departureDiffSinceLast.Seconds(),
"expectedTimeDiffSinceLast", expectedTimeDiffSinceLast,
"timeSinceFirst", timeSinceFirst.Seconds(),
"rtpDiffSinceFirst", rtpDiffSinceFirst,
"drift", drift,
+1 -1
View File
@@ -1637,7 +1637,7 @@ func (d *DownTrack) DebugInfo() map[string]interface{} {
}
}
func (d *DownTrack) getExpectedRTPTimestamp(at time.Time) (uint32, error) {
func (d *DownTrack) getExpectedRTPTimestamp(at time.Time) (uint32, uint32, error) {
return d.rtpStats.GetExpectedRTPTimestamp(at)
}
+16 -7
View File
@@ -170,7 +170,7 @@ type Forwarder struct {
kind webrtc.RTPCodecType
logger logger.Logger
getReferenceLayerRTPTimestamp func(ts uint32, layer int32, referenceLayer int32) (uint32, error)
getExpectedRTPTimestamp func(at time.Time) (uint32, error)
getExpectedRTPTimestamp func(at time.Time) (uint32, uint32, error)
muted bool
pubMuted bool
@@ -201,7 +201,7 @@ func NewForwarder(
kind webrtc.RTPCodecType,
logger logger.Logger,
getReferenceLayerRTPTimestamp func(ts uint32, layer int32, referenceLayer int32) (uint32, error),
getExpectedRTPTimestamp func(at time.Time) (uint32, error),
getExpectedRTPTimestamp func(at time.Time) (uint32, uint32, error),
) *Forwarder {
f := &Forwarder{
kind: kind,
@@ -1475,6 +1475,7 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
lastTS := f.rtpMunger.GetLast().LastTS
refTS := lastTS
expectedTS := lastTS
minTS := lastTS
switchingAt := time.Now()
if f.getReferenceLayerRTPTimestamp != nil {
ts, err := f.getReferenceLayerRTPTimestamp(extPkt.Packet.Timestamp, layer, f.referenceLayerSpatial)
@@ -1483,9 +1484,10 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
}
}
if f.getExpectedRTPTimestamp != nil {
ts, err := f.getExpectedRTPTimestamp(switchingAt)
ts, min, err := f.getExpectedRTPTimestamp(switchingAt)
if err == nil {
expectedTS = ts
minTS = min
} else {
rtpDiff := uint32(0)
if !f.preStartTime.IsZero() && f.refTSOffset == 0 {
@@ -1497,13 +1499,14 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i
}
}
refTS += f.refTSOffset
nextTS, explain := getNextTimestamp(lastTS, refTS, expectedTS)
nextTS, explain := getNextTimestamp(lastTS, refTS, expectedTS, minTS)
f.logger.Debugw(
"next timestamp on switch",
"switchingAt", switchingAt.String(),
"lastTS", lastTS,
"refTS", refTS,
"expectedTS", expectedTS,
"minTS", minTS,
"nextTS", nextTS,
"jump", nextTS-lastTS,
"explanation", explain,
@@ -1668,13 +1671,15 @@ func (f *Forwarder) GetSnTsForBlankFrames(frameRate uint32, numPackets int) ([]S
lastTS := f.rtpMunger.GetLast().LastTS
expectedTS := lastTS
minTS := lastTS
if f.getExpectedRTPTimestamp != nil {
ts, err := f.getExpectedRTPTimestamp(time.Now())
ts, min, err := f.getExpectedRTPTimestamp(time.Now())
if err == nil {
expectedTS = ts
minTS = min
}
}
nextTS, _ := getNextTimestamp(lastTS, expectedTS, expectedTS)
nextTS, _ := getNextTimestamp(lastTS, expectedTS, expectedTS, minTS)
snts, err := f.rtpMunger.UpdateAndGetPaddingSnTs(numPackets, f.codec.ClockRate, frameRate, frameEndNeeded, nextTS)
return snts, frameEndNeeded, err
}
@@ -1821,7 +1826,7 @@ done:
return float64(distance) / float64(maxSeenLayer.Temporal+1)
}
func getNextTimestamp(lastTS uint32, refTS uint32, expectedTS uint32) (uint32, string) {
func getNextTimestamp(lastTS uint32, refTS uint32, expectedTS uint32, minTS uint32) (uint32, string) {
isInOrder := func(val1, val2 uint32) bool {
diff := val1 - val2
return diff != 0 && diff < (1<<31)
@@ -1855,5 +1860,9 @@ func getNextTimestamp(lastTS uint32, refTS uint32, expectedTS uint32) (uint32, s
explain = fmt.Sprintf("e < r < l, %d, %d", refTS-expectedTS, lastTS-refTS)
}
if !isInOrder(nextTS, minTS) {
nextTS = minTS + 1
}
return nextTS, explain
}