Skip to content

Commit

Permalink
update logging on synchronizer reset
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbyte73 committed Feb 19, 2025
1 parent af54a02 commit b592d21
Showing 1 changed file with 28 additions and 10 deletions.
38 changes: 28 additions & 10 deletions pkg/synchronizer/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,7 @@ func (t *TrackSynchronizer) adjust(pkt *rtp.Packet) (int64, time.Duration, bool)
pkt.SequenceNumber = t.lastSN + 1

// reset RTP timestamps
logger.Debugw("resetting track synchronizer", "reason", "SN gap", "lastSN", t.lastSN, "SN", pkt.SequenceNumber)
ts, pts := t.resetRTP(pkt)
ts, pts := t.resetRTP(pkt, []any{"reason", "SN gap"})
return ts, pts, false
}

Expand All @@ -201,8 +200,11 @@ func (t *TrackSynchronizer) adjust(pkt *rtp.Packet) (int64, time.Duration, bool)
pts := t.getElapsed(ts) + t.ptsOffset
if expected := time.Since(t.startedAt.Add(t.ptsOffset)); pts > expected+maxTSDiff {
// reset RTP timestamps
logger.Debugw("resetting track synchronizer", "reason", "pts out of bounds", "pts", pts, "expected", expected)
ts, pts = t.resetRTP(pkt)
ts, pts = t.resetRTP(pkt, []any{
"reason", "pts out of bounds",
"pts", pts,
"expectedPTS", expected,
})
return ts, pts, false
}

Expand All @@ -213,12 +215,28 @@ func (t *TrackSynchronizer) getElapsed(ts int64) time.Duration {
return t.rtpConverter.toDuration(ts - t.firstTS)
}

func (t *TrackSynchronizer) resetRTP(pkt *rtp.Packet) (int64, time.Duration) {
frames := int64(time.Since(t.lastPacket) / t.getFrameDuration())
duration := t.getFrameDurationRTP() * frames
ts := t.lastTS + duration
pts := t.lastPTS + t.rtpConverter.toDuration(duration)
t.firstTS += int64(pkt.Timestamp) - ts
func (t *TrackSynchronizer) resetRTP(pkt *rtp.Packet, fields []any) (int64, time.Duration) {
frameDuration := t.getFrameDuration() // avg frame duration
frames := int64(time.Since(t.lastPacket) / frameDuration) // number of frames we expect since the last packet
duration := t.getFrameDurationRTP() * frames // expected increase in RTP time
ts := t.lastTS + duration // expected new RTP time
pts := t.lastPTS + t.rtpConverter.toDuration(duration) // expected new PTS

t.firstTS += int64(pkt.Timestamp) - ts // reset firstTS to align with new ts

fields = append(fields,
"pktTS", pkt.Timestamp,
"pktSN", pkt.SequenceNumber,
"prevTS", t.lastTS,
"prevSN", t.lastSN,
"frameDuration", frameDuration,
"prevPTS", t.lastPTS,
"adjustedTS", ts,
"adjustedPTS", pts,
)

logger.Debugw("resetting track synchronizer", fields...)

return ts, pts
}

Expand Down

0 comments on commit b592d21

Please sign in to comment.