client: add variables to store received and sent bytes

This commit is contained in:
aler9
2022-11-10 22:15:34 +01:00
parent 62f597457b
commit 12c8845fef
2 changed files with 41 additions and 18 deletions

View File

@@ -22,6 +22,7 @@ import (
"github.com/aler9/gortsplib/pkg/auth" "github.com/aler9/gortsplib/pkg/auth"
"github.com/aler9/gortsplib/pkg/base" "github.com/aler9/gortsplib/pkg/base"
"github.com/aler9/gortsplib/pkg/bytecounter"
"github.com/aler9/gortsplib/pkg/conn" "github.com/aler9/gortsplib/pkg/conn"
"github.com/aler9/gortsplib/pkg/headers" "github.com/aler9/gortsplib/pkg/headers"
"github.com/aler9/gortsplib/pkg/liberrors" "github.com/aler9/gortsplib/pkg/liberrors"
@@ -215,6 +216,10 @@ type Client struct {
UserAgent string UserAgent string
// disable automatic RTCP sender reports. // disable automatic RTCP sender reports.
DisableRTCPSenderReports bool DisableRTCPSenderReports bool
// pointer to a variable that stores received bytes.
BytesReceived *uint64
// pointer to a variable that stores sent bytes.
BytesSent *uint64
// //
// system functions (all optional) // system functions (all optional)
@@ -324,6 +329,12 @@ func (c *Client) Start(scheme string, host string) error {
if c.UserAgent == "" { if c.UserAgent == "" {
c.UserAgent = "gortsplib" c.UserAgent = "gortsplib"
} }
if c.BytesReceived == nil {
c.BytesReceived = new(uint64)
}
if c.BytesSent == nil {
c.BytesSent = new(uint64)
}
// system functions // system functions
if c.DialContext == nil { if c.DialContext == nil {
@@ -966,23 +977,22 @@ func (c *Client) connOpen() error {
return err return err
} }
c.nconn = func() net.Conn { if c.scheme == "rtsps" {
if c.scheme == "rtsps" { tlsConfig := c.TLSConfig
tlsConfig := c.TLSConfig
if tlsConfig == nil { if tlsConfig == nil {
tlsConfig = &tls.Config{} tlsConfig = &tls.Config{}
}
host, _, _ := net.SplitHostPort(c.host)
tlsConfig.ServerName = host
return tls.Client(nconn, tlsConfig)
} }
return nconn
}()
c.conn = conn.NewConn(c.nconn) host, _, _ := net.SplitHostPort(c.host)
tlsConfig.ServerName = host
nconn = tls.Client(nconn, tlsConfig)
}
c.nconn = nconn
bc := bytecounter.New(c.nconn, c.BytesReceived, c.BytesSent)
c.conn = conn.NewConn(bc)
c.connCloserStart() c.connCloserStart()
return nil return nil
@@ -1856,6 +1866,8 @@ func (c *Client) runWriter() {
} }
data := tmp.(trackTypePayload) data := tmp.(trackTypePayload)
atomic.AddUint64(c.BytesSent, uint64(len(data.payload)))
writeFunc(data.trackID, data.isRTP, data.payload) writeFunc(data.trackID, data.isRTP, data.payload)
} }
} }

View File

@@ -191,7 +191,11 @@ func (u *clientUDPListener) runReader(forPlay bool) {
} }
func (u *clientUDPListener) processPlayRTP(now time.Time, payload []byte) { func (u *clientUDPListener) processPlayRTP(now time.Time, payload []byte) {
if len(payload) == (maxPacketSize + 1) { plen := len(payload)
atomic.AddUint64(u.c.BytesReceived, uint64(plen))
if plen == (maxPacketSize + 1) {
u.c.OnDecodeError(fmt.Errorf("RTP packet is too big to be read with UDP")) u.c.OnDecodeError(fmt.Errorf("RTP packet is too big to be read with UDP"))
return return
} }
@@ -222,7 +226,11 @@ func (u *clientUDPListener) processPlayRTP(now time.Time, payload []byte) {
} }
func (u *clientUDPListener) processPlayRTCP(now time.Time, payload []byte) { func (u *clientUDPListener) processPlayRTCP(now time.Time, payload []byte) {
if len(payload) == (maxPacketSize + 1) { plen := len(payload)
atomic.AddUint64(u.c.BytesReceived, uint64(plen))
if plen == (maxPacketSize + 1) {
u.c.OnDecodeError(fmt.Errorf("RTCP packet is too big to be read with UDP")) u.c.OnDecodeError(fmt.Errorf("RTCP packet is too big to be read with UDP"))
return return
} }
@@ -243,7 +251,11 @@ func (u *clientUDPListener) processPlayRTCP(now time.Time, payload []byte) {
} }
func (u *clientUDPListener) processRecordRTCP(now time.Time, payload []byte) { func (u *clientUDPListener) processRecordRTCP(now time.Time, payload []byte) {
if len(payload) == (maxPacketSize + 1) { plen := len(payload)
atomic.AddUint64(u.c.BytesReceived, uint64(plen))
if plen == (maxPacketSize + 1) {
u.c.OnDecodeError(fmt.Errorf("RTCP packet is too big to be read with UDP")) u.c.OnDecodeError(fmt.Errorf("RTCP packet is too big to be read with UDP"))
return return
} }
@@ -265,7 +277,6 @@ func (u *clientUDPListener) processRecordRTCP(now time.Time, payload []byte) {
func (u *clientUDPListener) write(payload []byte) error { func (u *clientUDPListener) write(payload []byte) error {
// no mutex is needed here since Write() has an internal lock. // no mutex is needed here since Write() has an internal lock.
// https://github.com/golang/go/issues/27203#issuecomment-534386117 // https://github.com/golang/go/issues/27203#issuecomment-534386117
u.pc.SetWriteDeadline(time.Now().Add(u.c.WriteTimeout)) u.pc.SetWriteDeadline(time.Now().Add(u.c.WriteTimeout))
_, err := u.pc.WriteTo(payload, u.writeAddr) _, err := u.pc.WriteTo(payload, u.writeAddr)
return err return err