diff --git a/conn.go b/conn.go index 8fed8df..df624eb 100644 --- a/conn.go +++ b/conn.go @@ -24,23 +24,24 @@ type localTrackAction struct { } type upTrack struct { - track *webrtc.Track - label string - rate *estimator.Estimator - cache *packetcache.Cache - jitter *jitter.Estimator - maxBitrate uint64 - lastPLI uint64 - lastFIR uint64 - firSeqno uint32 - lastSenderReport uint32 - lastSenderReportTime uint32 + track *webrtc.Track + label string + rate *estimator.Estimator + cache *packetcache.Cache + jitter *jitter.Estimator + maxBitrate uint64 + lastPLI uint64 + lastFIR uint64 + firSeqno uint32 localCh chan localTrackAction // signals that local has changed writerDone chan struct{} // closed when the loop dies - mu sync.Mutex - local []downTrack + mu sync.Mutex + local []downTrack + srTime uint64 + srNTPTime uint64 + srRTPTime uint32 } func (up *upTrack) notifyLocal(add bool, track downTrack) { diff --git a/webclient.go b/webclient.go index 6be50f7..7922ba1 100644 --- a/webclient.go +++ b/webclient.go @@ -558,10 +558,11 @@ func rtcpUpListener(conn *upConnection, track *upTrack, r *webrtc.RTPReceiver) { for _, p := range ps { switch p := p.(type) { case *rtcp.SenderReport: - atomic.StoreUint32(&track.lastSenderReport, - uint32(p.NTPTime>>16)) - atomic.StoreUint32(&track.lastSenderReportTime, - uint32(mono.Now(0x10000))) + track.mu.Lock() + track.srTime = mono.Now(0x10000) + track.srNTPTime = p.NTPTime + track.srRTPTime = p.RTPTime + track.mu.Unlock() case *rtcp.SourceDescription: } } @@ -573,7 +574,7 @@ func sendRR(conn *upConnection) error { return nil } - now := uint32(mono.Now(0x10000)) + now := mono.Now(0x10000) reports := make([]rtcp.ReceptionReport, 0, len(conn.tracks)) for _, t := range conn.tracks { @@ -584,10 +585,15 @@ func sendRR(conn *upConnection) error { if lost >= expected { lost = expected - 1 } - lastSR := atomic.LoadUint32(&t.lastSenderReport) - var delay uint32 - if lastSR != 0 { - delay = now - atomic.LoadUint32(&t.lastSenderReportTime) + + t.mu.Lock() + srTime := t.srTime + srNTPTime := t.srNTPTime + t.mu.Unlock() + + var delay uint64 + if srNTPTime != 0 { + delay = now - srTime } reports = append(reports, rtcp.ReceptionReport{ @@ -596,8 +602,8 @@ func sendRR(conn *upConnection) error { TotalLost: totalLost, LastSequenceNumber: eseqno, Jitter: t.jitter.Jitter(), - LastSenderReport: lastSR, - Delay: delay, + LastSenderReport: uint32(srNTPTime >> 16), + Delay: uint32(delay), }) }