Skip to content

Commit

Permalink
Do not use LastTS for dummy offset. (livekit#2700)
Browse files Browse the repository at this point in the history
* Do not use LastTS for dummy offset.

LastTS could be random when using dummy start. That should not be used
in calculating offsets.

Also, do not push padding into sequence before init. Could have heppened
with dummy start.

* apply dummy offset before comparing to last

* refresh ref TS

* initialize codec munger on catch up forwarding
  • Loading branch information
boks1971 authored May 3, 2024
1 parent 674550e commit 26c2f7c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
13 changes: 9 additions & 4 deletions pkg/sfu/forwarder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1589,6 +1589,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
return nil
} else if f.referenceLayerSpatial == buffer.InvalidLayerSpatial {
f.referenceLayerSpatial = layer
f.codecMunger.SetLast(extPkt)
f.logger.Debugw(
"catch up forwarding",
"sequenceNumber", extPkt.Packet.SequenceNumber,
Expand Down Expand Up @@ -1641,8 +1642,9 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
}

// adjust extRefTS to current packet's timestamp mapped to that of reference layer's
extRefTS = (extRefTS & 0xFFFF_FFFF_0000_0000) + uint64(refTS)
extRefTS = (extRefTS & 0xFFFF_FFFF_0000_0000) + uint64(refTS) + f.dummyStartTSOffset
lastTS := uint32(extLastTS)
refTS = uint32(extRefTS)
if (refTS-lastTS) < 1<<31 && refTS < lastTS {
extRefTS += (1 << 32)
}
Expand All @@ -1660,21 +1662,23 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
rtpDiff := uint64(timeSinceFirst.Nanoseconds() * int64(f.codec.ClockRate) / 1e9)
extExpectedTS = f.extFirstTS + rtpDiff
if f.dummyStartTSOffset == 0 {
f.dummyStartTSOffset = extExpectedTS - extRefTS
f.dummyStartTSOffset = extExpectedTS - uint64(refTS)
extRefTS = extExpectedTS
f.logger.Infow(
"calculating dummyStartTSOffset",
"preStartTime", f.preStartTime.String(),
"extFirstTS", f.extFirstTS,
"timeSinceFirst", timeSinceFirst,
"timeSinceFirst", timeSinceFirst.String(),
"rtpDiff", rtpDiff,
"extRefTS", extRefTS,
"incomingTS", extPkt.Packet.Timestamp,
"referenceLayerSpatial", f.referenceLayerSpatial,
"dummyStartTSOffset", f.dummyStartTSOffset,
)
}
}
}
}
extRefTS += f.dummyStartTSOffset

var extNextTS uint64
if f.lastSSRC == 0 {
Expand Down Expand Up @@ -1759,6 +1763,7 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
"switchingAt", switchingAt.String(),
"layer", layer,
"extLastTS", extLastTS,
"lastMarker", rtpMungerState.LastMarker,
"extRefTS", extRefTS,
"dummyStartTSOffset", f.dummyStartTSOffset,
"referenceLayerSpatial", f.referenceLayerSpatial,
Expand Down
2 changes: 1 addition & 1 deletion pkg/sfu/sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (s *sequencer) pushPadding(extStartSNInclusive uint64, extEndSNInclusive ui
s.Lock()
defer s.Unlock()

if s.snRangeMap == nil {
if s.snRangeMap == nil || !s.initialized {
return
}

Expand Down

0 comments on commit 26c2f7c

Please sign in to comment.