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

@@ -2,6 +2,7 @@ package gortsplib
import (
"sync"
"sync/atomic"
"time"
"github.com/pion/rtcp"
@@ -37,6 +38,7 @@ type ServerStream struct {
activeUnicastReaders map[*ServerSession]struct{}
streamMedias map[*description.Media]*serverStreamMedia
closed bool
bytesSent *uint64
}
// NewServerStream allocates a ServerStream.
@@ -46,6 +48,7 @@ func NewServerStream(s *Server, desc *description.Session) *ServerStream {
desc: desc,
readers: make(map[*ServerSession]struct{}),
activeUnicastReaders: make(map[*ServerSession]struct{}),
bytesSent: new(uint64),
}
st.streamMedias = make(map[*description.Media]*serverStreamMedia, len(desc.Medias))
@@ -71,6 +74,11 @@ func (st *ServerStream) Close() {
}
}
// BytesSent returns the number of written bytes.
func (st *ServerStream) BytesSent() uint64 {
return atomic.LoadUint64(st.bytesSent)
}
// Description returns the description of the stream.
func (st *ServerStream) Description() *description.Session {
return st.desc