Skip to content

Commit dd3b698

Browse files
authored
Set initial PTS to estimated PTS to absorb pipeline start latency. (#752)
* Set initial PTS to estimated PTS to absord pipeline start latency. * fix compile * make initialPTS non-zero
1 parent d41bafa commit dd3b698

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

pkg/synchronizer/track.go

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ func (t *TrackSynchronizer) getPTSWithoutRebase(pkt jitter.ExtPacket) (time.Dura
172172
t.Lock()
173173
defer t.Unlock()
174174

175+
now := mono.Now()
175176
if t.startTime.IsZero() {
176177
if t.preJitterBufferReceiveTimeEnabled {
177178
t.startTime = pkt.ReceivedAt
178179
} else {
179-
t.startTime = mono.Now()
180+
t.startTime = now
180181
}
181182
t.logger.Infow(
182183
"starting track synchronizer",
@@ -213,19 +214,21 @@ func (t *TrackSynchronizer) getPTSWithoutRebase(pkt jitter.ExtPacket) (time.Dura
213214
}
214215
}
215216

216-
lastTS := t.lastTS
217-
if lastTS == 0 {
218-
lastTS = ts
217+
estimatedPTS := now.Sub(t.startTime)
218+
219+
var pts time.Duration
220+
if t.lastPTS == 0 {
221+
// start with estimated PTS to absorb any start latency
222+
pts = max(time.Nanosecond, estimatedPTS) // prevent lastPTS from being stuck at 0
223+
} else {
224+
pts = t.lastPTS + t.toDuration(ts-t.lastTS)
219225
}
220226

221-
pts := t.lastPTS + t.toDuration(ts-lastTS)
222-
estimatedPTS := time.Since(t.startTime)
223227
if pts < t.lastPTS || !t.acceptable(pts-estimatedPTS) {
224228
newStartRTP := ts - t.toRTP(estimatedPTS)
225229
t.logger.Infow(
226230
"correcting PTS",
227231
"currentTS", ts,
228-
"lastTS", lastTS,
229232
"PTS", pts,
230233
"estimatedPTS", estimatedPTS,
231234
"offset", pts-estimatedPTS,
@@ -276,7 +279,7 @@ func (t *TrackSynchronizer) getPTSWithoutRebase(pkt jitter.ExtPacket) (time.Dura
276279

277280
// update previous values
278281
t.lastTS = ts
279-
t.lastTime = mono.Now()
282+
t.lastTime = now
280283
t.lastPTS = pts
281284
t.lastPTSAdjusted = adjusted
282285

@@ -332,19 +335,21 @@ func (t *TrackSynchronizer) getPTSWithRebase(pkt jitter.ExtPacket) (time.Duratio
332335
return 0, ErrPacketTooOld
333336
}
334337

335-
lastTS := t.lastTS
336-
if lastTS == 0 {
337-
lastTS = ts
338+
now := mono.Now()
339+
estimatedPTS := max(time.Nanosecond, now.Sub(t.startTime))
340+
341+
var pts time.Duration
342+
if t.lastPTS == 0 {
343+
// start with estimated PTS to absorb any start latency
344+
pts = max(time.Nanosecond, estimatedPTS) // prevent lastPTS from being stuck at 0
345+
} else {
346+
pts = t.lastPTS + t.toDuration(ts-t.lastTS)
338347
}
339348

340-
pts := t.lastPTS + t.toDuration(ts-lastTS)
341-
now := mono.Now()
342-
estimatedPTS := now.Sub(t.startTime)
343349
if pts < t.lastPTS || !t.acceptable(pts-estimatedPTS) {
344350
t.logger.Infow(
345351
"correcting PTS",
346352
"currentTS", ts,
347-
"lastTS", lastTS,
348353
"PTS", pts,
349354
"estimatedPTS", estimatedPTS,
350355
"offset", pts-estimatedPTS,

0 commit comments

Comments
 (0)