From 8ba50bd2ca7b61261e08827de6da4f3fc5050e55 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Thu, 4 Jun 2020 19:12:52 +0200 Subject: [PATCH] Tweak SR sending policy. Don't send SRs for tracks for which we have no time offset yet. Send an unscheduled SR when we get our first time offset. --- webclient.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/webclient.go b/webclient.go index f99888a..e9d8234 100644 --- a/webclient.go +++ b/webclient.go @@ -547,6 +547,7 @@ func writeLoop(conn *upConnection, track *upTrack, ch <-chan packetIndex) { func rtcpUpListener(conn *upConnection, track *upTrack, r *webrtc.RTPReceiver) { for { + firstSR := false ps, err := r.ReadRTCP() if err != nil { if err != io.EOF { @@ -555,17 +556,37 @@ func rtcpUpListener(conn *upConnection, track *upTrack, r *webrtc.RTPReceiver) { return } + now := rtptime.Jiffies() + for _, p := range ps { switch p := p.(type) { case *rtcp.SenderReport: track.mu.Lock() - track.srTime = rtptime.Jiffies() + if track.srTime == 0 { + firstSR = true + } + track.srTime = now track.srNTPTime = p.NTPTime track.srRTPTime = p.RTPTime track.mu.Unlock() case *rtcp.SourceDescription: } } + + if(firstSR) { + // this is the first SR we got for at least one track, + // quickly propagate the time offsets downstream + local := conn.getLocal() + for _, l := range local { + l, ok := l.(*rtpDownConnection) + if ok { + err := sendSR(l) + if err != nil { + log.Printf("sendSR: %v", err) + } + } + } + } } } @@ -638,11 +659,18 @@ func sendSR(conn *rtpDownConnection) error { for _, t := range conn.tracks { clockrate := t.track.Codec().ClockRate remote := t.remote + remote.mu.Lock() + lastTime := remote.srTime srNTPTime := remote.srNTPTime srRTPTime := remote.srRTPTime remote.mu.Unlock() + if lastTime == 0 { + // we never got a remote SR, skip this track + continue + } + nowRTP := srRTPTime if srNTPTime != 0 { srTime := rtptime.NTPToTime(srNTPTime) @@ -666,6 +694,10 @@ func sendSR(conn *rtpDownConnection) error { atomic.StoreUint64(&t.srNTPTime, nowNTP) } + if len(packets) == 0 { + return nil + } + return conn.pc.WriteRTCP(packets) }