@@ -638,13 +638,14 @@ func (t *TrackSynchronizer) maybeAdjustStartTime(asr *augmentedSenderReport) int
638638 timeSinceStart := time .Duration (nowNano - startTimeNano )
639639 now := startTimeNano + timeSinceStart .Nanoseconds ()
640640 adjustedStartTimeNano := now - samplesDuration .Nanoseconds ()
641+ requestedAdjustment := startTimeNano - adjustedStartTimeNano
641642
642643 getLoggingFields := func () []interface {} {
643644 return []interface {}{
644645 "nowTime" , time .Unix (0 , now ),
645- "before" , t . startTime ,
646+ "before" , time . Unix ( 0 , startTimeNano ) ,
646647 "after" , time .Unix (0 , adjustedStartTimeNano ),
647- "adjustment " , time .Duration (startTimeNano - adjustedStartTimeNano ),
648+ "requestedAdjustment " , time .Duration (requestedAdjustment ),
648649 "nowTS" , nowTS ,
649650 "timeSinceReceive" , timeSinceReceive ,
650651 "timeSinceStart" , timeSinceStart ,
@@ -656,18 +657,18 @@ func (t *TrackSynchronizer) maybeAdjustStartTime(asr *augmentedSenderReport) int
656657 }
657658
658659 if adjustedStartTimeNano < startTimeNano {
659- if startTimeNano - adjustedStartTimeNano > cStartTimeAdjustThreshold .Nanoseconds () {
660+ if requestedAdjustment > cStartTimeAdjustThreshold .Nanoseconds () {
660661 t .logger .Warnw (
661662 "adjusting start time, too big, ignoring" , nil ,
662663 getLoggingFields ()... ,
663664 )
664665 } else {
665- applied := t .applyQuantizedStartTimeAdvance (time .Duration (startTimeNano - adjustedStartTimeNano ))
666- t .logger .Infow ("adjusting start time" , append (getLoggingFields (), "applied " , applied )... )
666+ applied := t .applyQuantizedStartTimeAdvance (time .Duration (requestedAdjustment ))
667+ t .logger .Infow ("adjusting start time" , append (getLoggingFields (), "appliedAdjustment " , applied )... )
667668 }
668669 }
669670
670- return startTimeNano - adjustedStartTimeNano
671+ return requestedAdjustment
671672}
672673
673674func (t * TrackSynchronizer ) acceptable (d time.Duration ) bool {
@@ -696,6 +697,9 @@ func (t *TrackSynchronizer) isPacketTooOld(packetTime time.Time) bool {
696697 return t .oldPacketThreshold != 0 && mono .Now ().Sub (packetTime ) > t .oldPacketThreshold
697698}
698699
700+ // avoid applying small changes to start time as it will cause subsequent PTSes
701+ // to have micro jumps potentially causing audible distortion,
702+ // the bet is more infrequent larger jumps is better than more frequent smaller jumps
699703func (t * TrackSynchronizer ) applyQuantizedStartTimeAdvance (deltaTotal time.Duration ) time.Duration {
700704 // include any prior residual
701705 deltaTotal += t .startTimeAdjustResidual
@@ -738,6 +742,7 @@ func (t *TrackSynchronizer) MarshalLogObject(e zapcore.ObjectEncoder) error {
738742 e .AddTime ("nextPTSAdjustmentAt" , t .nextPTSAdjustmentAt )
739743 e .AddObject ("propagationDelayEstimator" , t .propagationDelayEstimator )
740744 e .AddDuration ("totalStartTimeAdjustment" , t .totalStartTimeAdjustment )
745+ e .AddDuration ("startTimeAdjustResidual" , t .startTimeAdjustResidual )
741746 e .AddUint32 ("numEmitted" , t .numEmitted )
742747 e .AddUint32 ("numDroppedOld" , t .numDroppedOld )
743748 e .AddUint32 ("numDroppedOutOfOrder" , t .numDroppedOutOfOrder )
0 commit comments