Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update logging on synchronizer reset #614

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading