From d3f53e6ada042335d9d15093719b5d8c377d86e9 Mon Sep 17 00:00:00 2001 From: Juliusz Chroboczek Date: Tue, 11 May 2021 22:41:56 +0200 Subject: [PATCH] Move rate estimation into rtpDownTrack.WriteRTP. --- conn/conn.go | 1 - diskwriter/diskwriter.go | 4 ---- rtpconn/rtpconn.go | 14 ++++++++------ rtpconn/rtpwriter.go | 2 -- 4 files changed, 8 insertions(+), 13 deletions(-) diff --git a/conn/conn.go b/conn/conn.go index 2129dae..64ce618 100644 --- a/conn/conn.go +++ b/conn/conn.go @@ -39,7 +39,6 @@ type Down interface { // Type DownTrack represents a track in the server to client direction. type DownTrack interface { WriteRTP(packat *rtp.Packet) error - Accumulate(bytes uint32) SetTimeOffset(ntp uint64, rtp uint32) SetCname(string) GetMaxBitrate() uint64 diff --git a/diskwriter/diskwriter.go b/diskwriter/diskwriter.go index 80dc5b4..db8bd2b 100644 --- a/diskwriter/diskwriter.go +++ b/diskwriter/diskwriter.go @@ -603,7 +603,3 @@ func (conn *diskConn) initWriter(width, height uint32) error { func (t *diskTrack) GetMaxBitrate() uint64 { return ^uint64(0) } - -func (t *diskTrack) Accumulate(bytes uint32) { - return -} diff --git a/rtpconn/rtpconn.go b/rtpconn/rtpconn.go index 9828e87..3729485 100644 --- a/rtpconn/rtpconn.go +++ b/rtpconn/rtpconn.go @@ -90,11 +90,14 @@ type rtpDownTrack struct { } func (down *rtpDownTrack) WriteRTP(packet *rtp.Packet) error { - return down.track.WriteRTP(packet) -} - -func (down *rtpDownTrack) Accumulate(bytes uint32) { - down.rate.Accumulate(bytes) + err := down.track.WriteRTP(packet) + if err == nil { + // we should account for extensions + down.rate.Accumulate( + uint32(12 + 4*len(packet.CSRC) + len(packet.Payload)), + ) + } + return err } func (down *rtpDownTrack) SetTimeOffset(ntp uint64, rtp uint32) { @@ -650,7 +653,6 @@ func gotNACK(conn *rtpDownConnection, track *rtpDownTrack, p *rtcp.TransportLaye log.Printf("WriteRTP: %v", err) return false } - track.rate.Accumulate(uint32(l)) return true }) } diff --git a/rtpconn/rtpwriter.go b/rtpconn/rtpwriter.go index 98de4ff..bc4166d 100644 --- a/rtpconn/rtpwriter.go +++ b/rtpconn/rtpwriter.go @@ -225,7 +225,6 @@ func sendKeyframe(kf []uint16, track conn.DownTrack, cache *packetcache.Cache) { if err != nil { return } - track.Accumulate(uint32(bytes)) } } @@ -321,7 +320,6 @@ func rtpWriterLoop(writer *rtpWriter, up *rtpUpConnection, track *rtpUpTrack) { if err != nil { continue } - l.Accumulate(uint32(bytes)) } } }