@@ -30,6 +30,7 @@ import (
3030 "github.com/livekit/mediatransportutil"
3131 "github.com/livekit/protocol/logger"
3232 "github.com/livekit/protocol/utils/mono"
33+ "github.com/livekit/protocol/utils/rtputil"
3334)
3435
3536const (
@@ -58,7 +59,7 @@ type TrackSynchronizer struct {
5859 sync * Synchronizer
5960 track TrackRemote
6061 logger logger.Logger
61- * rtpConverter
62+ * rtputil. RTPConverter
6263 startGate startGate
6364
6465 // config
@@ -116,7 +117,7 @@ func newTrackSynchronizer(s *Synchronizer, track TrackRemote) *TrackSynchronizer
116117 sync : s ,
117118 track : track ,
118119 logger : logger .GetLogger ().WithValues ("trackID" , track .ID (), "codec" , track .Codec ().MimeType ),
119- rtpConverter : newRTPConverter (int64 (track .Codec ().ClockRate )),
120+ RTPConverter : rtputil . NewRTPConverter (int64 (track .Codec ().ClockRate )),
120121 maxTsDiff : s .config .MaxTsDiff ,
121122 maxDriftAdjustment : s .config .MaxDriftAdjustment ,
122123 driftAdjustmentWindowPercent : s .config .DriftAdjustmentWindowPercent ,
@@ -304,7 +305,7 @@ func (t *TrackSynchronizer) getPTSWithoutRebase(pkt jitter.ExtPacket) (time.Dura
304305 // start with estimated PTS to absorb any start latency
305306 pts = max (time .Nanosecond , estimatedPTS ) // prevent lastPTS from being stuck at 0
306307 } else {
307- pts = t .lastPTS + t .toDuration (ts - t .lastTS )
308+ pts = t .lastPTS + t .ToDuration (ts - t .lastTS )
308309 }
309310
310311 if pts < t .lastPTS || ! t .acceptable (pts - estimatedPTS ) {
@@ -435,7 +436,7 @@ func (t *TrackSynchronizer) getPTSWithRebase(pkt jitter.ExtPacket) (time.Duratio
435436 // start with estimated PTS to absorb any start latency
436437 pts = max (time .Nanosecond , estimatedPTS ) // prevent lastPTS from being stuck at 0
437438 } else {
438- pts = t .lastPTS + t .toDuration (ts - t .lastTS )
439+ pts = t .lastPTS + t .ToDuration (ts - t .lastTS )
439440 }
440441
441442 if pts < t .lastPTS || ! t .acceptable (pts - estimatedPTS ) {
@@ -543,9 +544,9 @@ func (t *TrackSynchronizer) onSenderReportWithoutRebase(pkt *rtcp.SenderReport)
543544
544545 var pts time.Duration
545546 if pkt .RTPTime > t .lastTS {
546- pts = t .lastPTS + t .toDuration (pkt .RTPTime - t .lastTS )
547+ pts = t .lastPTS + t .ToDuration (pkt .RTPTime - t .lastTS )
547548 } else {
548- pts = t .lastPTS - t .toDuration (t .lastTS - pkt .RTPTime )
549+ pts = t .lastPTS - t .ToDuration (t .lastTS - pkt .RTPTime )
549550 }
550551 if ! t .acceptable (pts - time .Since (t .startTime )) {
551552 t .logger .Infow (
@@ -615,9 +616,9 @@ func (t *TrackSynchronizer) onSenderReportWithRebase(pkt *rtcp.SenderReport) {
615616
616617 var ptsSR time.Duration
617618 if (pkt .RTPTime - t .lastTS ) < (1 << 31 ) {
618- ptsSR = t .lastPTS + t .toDuration (pkt .RTPTime - t .lastTS )
619+ ptsSR = t .lastPTS + t .ToDuration (pkt .RTPTime - t .lastTS )
619620 } else {
620- ptsSR = t .lastPTS - t .toDuration (t .lastTS - pkt .RTPTime )
621+ ptsSR = t .lastPTS - t .ToDuration (t .lastTS - pkt .RTPTime )
621622 }
622623 if ! t .acceptable (ptsSR - time .Since (t .startTime )) {
623624 t .logger .Infow (
@@ -701,7 +702,7 @@ func (t *TrackSynchronizer) maybeAdjustStartTime(asr *augmentedSenderReport) int
701702 // in some network element along the way), push back first time
702703 // to an earlier instance.
703704 timeSinceReceive := time .Duration (nowNano - asr .receivedAtAdjusted )
704- nowTS := asr .RTPTime + t .toRTP (timeSinceReceive )
705+ nowTS := asr .RTPTime + t .ToRTP (timeSinceReceive )
705706 samplesDiff := nowTS - t .startRTP
706707 if int32 (samplesDiff ) < 0 {
707708 // out-of-order, pre-start, skip
@@ -716,7 +717,7 @@ func (t *TrackSynchronizer) maybeAdjustStartTime(asr *augmentedSenderReport) int
716717 return 0
717718 }
718719
719- samplesDuration := t .toDuration (samplesDiff )
720+ samplesDuration := t .ToDuration (samplesDiff )
720721 timeSinceStart := time .Duration (nowNano - startTimeNano )
721722 now := startTimeNano + timeSinceStart .Nanoseconds ()
722723 adjustedStartTimeNano := now - samplesDuration .Nanoseconds ()
@@ -931,33 +932,6 @@ func (t *TrackSynchronizer) MarshalLogObject(e zapcore.ObjectEncoder) error {
931932
932933// ---------------------------
933934
934- type rtpConverter struct {
935- ts uint64
936- rtp uint64
937- }
938-
939- func newRTPConverter (clockRate int64 ) * rtpConverter {
940- ts := time .Second .Nanoseconds ()
941- for _ , i := range []int64 {10 , 3 , 2 } {
942- for ts % i == 0 && clockRate % i == 0 {
943- ts /= i
944- clockRate /= i
945- }
946- }
947-
948- return & rtpConverter {ts : uint64 (ts ), rtp : uint64 (clockRate )}
949- }
950-
951- func (c * rtpConverter ) toDuration (rtpDuration uint32 ) time.Duration {
952- return time .Duration (uint64 (rtpDuration ) * c .ts / c .rtp )
953- }
954-
955- func (c * rtpConverter ) toRTP (duration time.Duration ) uint32 {
956- return uint32 (duration .Nanoseconds () * int64 (c .rtp ) / int64 (c .ts ))
957- }
958-
959- // -----------------------------
960-
961935type augmentedSenderReport struct {
962936 * rtcp.SenderReport
963937 receivedAt int64
0 commit comments