Skip to content

Commit 3ccd0ff

Browse files
authored
Update logging on synchronizer reset (#614)
* update logging on synchronizer reset * info * add trackID and codec to track synchronizer logs
1 parent af54a02 commit 3ccd0ff

File tree

1 file changed

+35
-16
lines changed

1 file changed

+35
-16
lines changed

pkg/synchronizer/track.go

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ type TrackRemote interface {
4949

5050
type TrackSynchronizer struct {
5151
sync.Mutex
52-
sync *Synchronizer
53-
track TrackRemote
52+
sync *Synchronizer
53+
track TrackRemote
54+
logger logger.Logger
5455

5556
// track stats
5657
stats *TrackStats
@@ -74,14 +75,15 @@ type TrackSynchronizer struct {
7475
inserted int64 // number of frames inserted
7576

7677
// offsets
77-
snOffset uint16 // sequence number offset (increases with each blank frame inserted
78+
snOffset uint16 // sequence number offset (increases with each blank frame inserted)
7879
ptsOffset time.Duration // presentation timestamp offset (used for a/v sync)
7980
}
8081

8182
func newTrackSynchronizer(s *Synchronizer, track TrackRemote) *TrackSynchronizer {
8283
t := &TrackSynchronizer{
8384
sync: s,
8485
track: track,
86+
logger: logger.GetLogger().WithValues("trackID", track.ID(), "codec", track.Codec().MimeType),
8587
stats: &TrackStats{},
8688
rtpConverter: newRTPConverter(int64(track.Codec().ClockRate)),
8789
}
@@ -119,7 +121,7 @@ func (t *TrackSynchronizer) GetPTS(pkt *rtp.Packet) (time.Duration, error) {
119121
ts, pts, valid := t.adjust(pkt)
120122
if pts < t.lastPTS {
121123
if t.backwards == 0 {
122-
logger.Warnw("backwards pts", ErrBackwardsPTS,
124+
t.logger.Warnw("backwards pts", ErrBackwardsPTS,
123125
"timestamp", pkt.Timestamp,
124126
"sequence number", pkt.SequenceNumber,
125127
"pts", pts,
@@ -131,9 +133,8 @@ func (t *TrackSynchronizer) GetPTS(pkt *rtp.Packet) (time.Duration, error) {
131133
t.backwards++
132134
return 0, ErrBackwardsPTS
133135
} else if t.backwards > 0 {
134-
logger.Debugw("packet dropped",
136+
t.logger.Warnw("packet dropped", ErrBackwardsPTS,
135137
"count", t.backwards,
136-
"reason", "backwards pts",
137138
)
138139
t.backwards = 0
139140
}
@@ -181,8 +182,7 @@ func (t *TrackSynchronizer) adjust(pkt *rtp.Packet) (int64, time.Duration, bool)
181182
pkt.SequenceNumber = t.lastSN + 1
182183

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

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

@@ -213,12 +216,28 @@ func (t *TrackSynchronizer) getElapsed(ts int64) time.Duration {
213216
return t.rtpConverter.toDuration(ts - t.firstTS)
214217
}
215218

216-
func (t *TrackSynchronizer) resetRTP(pkt *rtp.Packet) (int64, time.Duration) {
217-
frames := int64(time.Since(t.lastPacket) / t.getFrameDuration())
218-
duration := t.getFrameDurationRTP() * frames
219-
ts := t.lastTS + duration
220-
pts := t.lastPTS + t.rtpConverter.toDuration(duration)
221-
t.firstTS += int64(pkt.Timestamp) - ts
219+
func (t *TrackSynchronizer) resetRTP(pkt *rtp.Packet, fields []any) (int64, time.Duration) {
220+
frameDuration := t.getFrameDuration() // avg frame duration
221+
frames := int64(time.Since(t.lastPacket) / frameDuration) // number of frames we expect since the last packet
222+
duration := t.getFrameDurationRTP() * frames // expected increase in RTP time
223+
ts := t.lastTS + duration // expected new RTP time
224+
pts := t.lastPTS + t.rtpConverter.toDuration(duration) // expected new PTS
225+
226+
t.firstTS += int64(pkt.Timestamp) - ts // reset firstTS to align with new ts
227+
228+
fields = append(fields,
229+
"pktTS", pkt.Timestamp,
230+
"pktSN", pkt.SequenceNumber,
231+
"prevTS", t.lastTS,
232+
"prevSN", t.lastSN,
233+
"frameDuration", frameDuration,
234+
"prevPTS", t.lastPTS,
235+
"adjustedTS", ts,
236+
"adjustedPTS", pts,
237+
)
238+
239+
t.logger.Infow("resetting track synchronizer", fields...)
240+
222241
return ts, pts
223242
}
224243

0 commit comments

Comments
 (0)