add BytesSent to ServerStream (#457)

* add BytesSent to ServerStream (to allow MediaMTX to gather bytes sent on all paths)

* improve performance

* add test

---------

Co-authored-by: aler9 <46489434+aler9@users.noreply.github.com>
This commit is contained in:
Dr. Ralf S. Engelschall
2023-11-04 17:46:22 +01:00
committed by GitHub
parent 35bf96c5ec
commit 0933bf9975
3 changed files with 88 additions and 1 deletions

View File

@@ -1,6 +1,7 @@
package gortsplib
import (
"sync/atomic"
"time"
"github.com/pion/rtcp"
@@ -39,6 +40,8 @@ func newServerStreamFormat(sm *serverStreamMedia, forma format.Format) *serverSt
func (sf *serverStreamFormat) writePacketRTP(byts []byte, pkt *rtp.Packet, ntp time.Time) error {
sf.rtcpSender.ProcessPacket(pkt, ntp, sf.format.PTSEqualsDTS(pkt))
le := uint64(len(byts))
// send unicast
for r := range sf.sm.st.activeUnicastReaders {
sm, ok := r.setuppedMedias[sf.sm.media]
@@ -46,6 +49,8 @@ func (sf *serverStreamFormat) writePacketRTP(byts []byte, pkt *rtp.Packet, ntp t
err := sm.writePacketRTP(byts)
if err != nil {
r.onStreamWriteError(err)
} else {
atomic.AddUint64(sf.sm.st.bytesSent, le)
}
}
}
@@ -56,6 +61,7 @@ func (sf *serverStreamFormat) writePacketRTP(byts []byte, pkt *rtp.Packet, ntp t
if err != nil {
return err
}
atomic.AddUint64(sf.sm.st.bytesSent, le)
}
return nil