From 78023108307afaf98eb0275467c3c1af3acb4b4a Mon Sep 17 00:00:00 2001 From: Raja Subramanian Date: Thu, 10 Aug 2023 13:48:39 +0530 Subject: [PATCH] Restrict resume behind check to publisher mute only. (#1951) A publisher mute is when the reference can fall behind because of replaceTrack(nil). On a subscriber mute, should not jump ahead to expected because publisher could still be lagging and behind. Also consolidate logging. --- pkg/sfu/forwarder.go | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/pkg/sfu/forwarder.go b/pkg/sfu/forwarder.go index abfd9a4e..74cb87de 100644 --- a/pkg/sfu/forwarder.go +++ b/pkg/sfu/forwarder.go @@ -188,8 +188,9 @@ type Forwarder struct { getReferenceLayerRTPTimestamp func(ts uint32, layer int32, referenceLayer int32) (uint32, error) getExpectedRTPTimestamp func(at time.Time) (uint64, error) - muted bool - pubMuted bool + muted bool + pubMuted bool + resumeBehindThreshold float64 started bool preStartTime time.Time @@ -1360,6 +1361,9 @@ func (f *Forwarder) Resync() { func (f *Forwarder) resyncLocked() { f.vls.SetCurrent(buffer.InvalidLayer) f.lastSSRC = 0 + if f.pubMuted { + f.resumeBehindThreshold = ResumeBehindThresholdSeconds + } } func (f *Forwarder) CheckSync() (locked bool, layer int32) { @@ -1435,6 +1439,17 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e return nil } + logTransition := func(message string, expectedTS, refTS, lastTS uint32, diffSeconds float64) { + f.logger.Infow( + message, + "layer", layer, + "expectedTS", expectedTS, + "refTS", refTS, + "lastTS", lastTS, + "diffSeconds", math.Abs(diffSeconds), + ) + } + if f.referenceLayerSpatial == buffer.InvalidLayerSpatial { // on a resume, reference layer may not be set, so only set when it is invalid f.referenceLayerSpatial = layer @@ -1517,18 +1532,19 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e // for refTS > expectedTS diffSeconds := float64(int32(expectedTS-refTS)) / float64(f.codec.ClockRate) if diffSeconds >= 0.0 { - if diffSeconds > ResumeBehindThresholdSeconds { - f.logger.Infow("resume, reference too far behind", "expectedTS", expectedTS, "refTS", refTS, "diffSeconds", diffSeconds) + if f.resumeBehindThreshold > 0 && diffSeconds > f.resumeBehindThreshold { + logTransition("resume, reference too far behind", expectedTS, refTS, lastTS, diffSeconds) nextTS = expectedTS } else { nextTS = refTS } } else { if math.Abs(diffSeconds) > SwitchAheadThresholdSeconds { - f.logger.Infow("resume, reference too far ahead", "expectedTS", expectedTS, "refTS", refTS, "diffSeconds", math.Abs(diffSeconds)) + logTransition("resume, reference too far ahead", expectedTS, refTS, lastTS, diffSeconds) } nextTS = refTS } + f.resumeBehindThreshold = 0.0 } else { // switching between layers, check if refTS is too far behind the last sent diffSeconds := float64(int32(refTS-lastTS)) / float64(f.codec.ClockRate) @@ -1537,16 +1553,16 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e // this could be due to pacer trickling out this layer. Error out and wait for a more opportune time. // AVSYNC-TODO: Consider some forcing function to do the switch // (like "have waited for too long for layer switch, nothing available, switch to whatever is available" kind of condition). - f.logger.Infow("layer switch, reference too far behind", "expectedTS", expectedTS, "refTS", refTS, "lastTS", lastTS, "diffSeconds", math.Abs(diffSeconds)) + logTransition("layer switch, reference too far behind", expectedTS, refTS, lastTS, diffSeconds) return errors.New("switch point too far behind") } // use a nominal increase to ensure that timestamp is always moving forward - f.logger.Infow("layer switch, reference is slghtly behind", "expectedTS", expectedTS, "refTS", refTS, "lastTS", lastTS, "diffSeconds", math.Abs(diffSeconds)) + logTransition("layer switch, reference is slightly behind", expectedTS, refTS, lastTS, diffSeconds) nextTS = lastTS + 1 } else { diffSeconds = float64(int32(expectedTS-refTS)) / float64(f.codec.ClockRate) if diffSeconds < 0.0 && math.Abs(diffSeconds) > SwitchAheadThresholdSeconds { - f.logger.Infow("layer switch, reference too far ahead", "expectedTS", expectedTS, "refTS", refTS, "diffSeconds", math.Abs(diffSeconds)) + logTransition("layer switch, reference too far ahead", expectedTS, refTS, lastTS, diffSeconds) nextTS = expectedTS } else { nextTS = refTS @@ -1585,7 +1601,7 @@ func (f *Forwarder) getTranslationParamsCommon(extPkt *buffer.ExtPacket, layer i if f.lastSSRC != extPkt.Packet.SSRC { if err := f.processSourceSwitch(extPkt, layer); err != nil { tp.shouldDrop = true - return tp, err + return tp, nil } f.logger.Debugw("switching feed", "from", f.lastSSRC, "to", extPkt.Packet.SSRC) f.lastSSRC = extPkt.Packet.SSRC