add option MaxPacketSize to Client and Server

This commit is contained in:
aler9
2023-08-12 19:34:21 +02:00
parent 68d4bf8da0
commit 7532484041
7 changed files with 28 additions and 10 deletions

View File

@@ -243,6 +243,10 @@ type Client struct {
// It allows to queue packets before sending them.
// It defaults to 256.
WriteBufferCount int
// maximum size of outgoing RTP / RTCP packets.
// This must be less than the UDP MTU (1472 bytes).
// It defaults to 1472.
MaxPacketSize int
// user agent header.
// It defaults to "gortsplib"
UserAgent string
@@ -343,10 +347,14 @@ func (c *Client) Start(scheme string, host string) error {
}
if c.WriteBufferCount == 0 {
c.WriteBufferCount = 256
}
if (c.WriteBufferCount & (c.WriteBufferCount - 1)) != 0 {
} else if (c.WriteBufferCount & (c.WriteBufferCount - 1)) != 0 {
return fmt.Errorf("WriteBufferCount must be a power of two")
}
if c.MaxPacketSize == 0 {
c.MaxPacketSize = udpMaxPayloadSize
} else if c.MaxPacketSize > udpMaxPayloadSize {
return fmt.Errorf("MaxPacketSize must be less than %d", udpMaxPayloadSize)
}
if c.UserAgent == "" {
c.UserAgent = "gortsplib"
}
@@ -1650,7 +1658,7 @@ func (c *Client) WritePacketRTP(medi *media.Media, pkt *rtp.Packet) error {
// WritePacketRTPWithNTP writes a RTP packet to the media stream.
func (c *Client) WritePacketRTPWithNTP(medi *media.Media, pkt *rtp.Packet, ntp time.Time) error {
byts := make([]byte, udpMaxPayloadSize)
byts := make([]byte, c.MaxPacketSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return err

View File

@@ -118,7 +118,7 @@ func (cm *clientMedia) start() {
cm.tcpRTPFrame = &base.InterleavedFrame{Channel: cm.tcpChannel}
cm.tcpRTCPFrame = &base.InterleavedFrame{Channel: cm.tcpChannel + 1}
cm.tcpBuffer = make([]byte, udpMaxPayloadSize+4)
cm.tcpBuffer = make([]byte, cm.c.MaxPacketSize+4)
}
for _, ct := range cm.formats {

View File

@@ -90,6 +90,10 @@ type Server struct {
// It allows to queue packets before sending them.
// It defaults to 256.
WriteBufferCount int
// maximum size of outgoing RTP / RTCP packets.
// This must be less than the UDP MTU (1472 bytes).
// It defaults to 1472.
MaxPacketSize int
// disable automatic RTCP sender reports.
DisableRTCPSenderReports bool
@@ -154,10 +158,14 @@ func (s *Server) Start() error {
}
if s.WriteBufferCount == 0 {
s.WriteBufferCount = 256
}
if (s.WriteBufferCount & (s.WriteBufferCount - 1)) != 0 {
} else if (s.WriteBufferCount & (s.WriteBufferCount - 1)) != 0 {
return fmt.Errorf("WriteBufferCount must be a power of two")
}
if s.MaxPacketSize == 0 {
s.MaxPacketSize = udpMaxPayloadSize
} else if s.MaxPacketSize > udpMaxPayloadSize {
return fmt.Errorf("MaxPacketSize must be less than %d", udpMaxPayloadSize)
}
// system functions
if s.Listen == nil {

View File

@@ -520,7 +520,7 @@ func TestServerRecord(t *testing.T) {
}, nil, nil
},
onRecord: func(ctx *ServerHandlerOnRecordCtx) (*base.Response, error) {
// send RTCP packets directly to the session.
// queue sending of RTCP packets.
// these are sent after the response, only if onRecord returns StatusOK.
err := ctx.Session.WritePacketRTCP(ctx.Session.AnnouncedMedias()[0], &testRTCPPacket)
require.NoError(t, err)

View File

@@ -1161,10 +1161,12 @@ func (ss *ServerSession) writePacketRTP(medi *media.Media, byts []byte) {
// WritePacketRTP writes a RTP packet to the session.
func (ss *ServerSession) WritePacketRTP(medi *media.Media, pkt *rtp.Packet) error {
byts, err := pkt.Marshal()
byts := make([]byte, ss.s.MaxPacketSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return err
}
byts = byts[:n]
ss.writePacketRTP(medi, byts)
return nil

View File

@@ -93,7 +93,7 @@ func (sm *serverSessionMedia) start() {
sm.tcpRTPFrame = &base.InterleavedFrame{Channel: sm.tcpChannel}
sm.tcpRTCPFrame = &base.InterleavedFrame{Channel: sm.tcpChannel + 1}
sm.tcpBuffer = make([]byte, udpMaxPayloadSize+4)
sm.tcpBuffer = make([]byte, sm.ss.s.MaxPacketSize+4)
}
}

View File

@@ -251,7 +251,7 @@ func (st *ServerStream) WritePacketRTP(medi *media.Media, pkt *rtp.Packet) error
// ntp is the absolute time of the packet, and is needed to generate RTCP sender reports
// that allows the receiver to reconstruct the absolute time of the packet.
func (st *ServerStream) WritePacketRTPWithNTP(medi *media.Media, pkt *rtp.Packet, ntp time.Time) error {
byts := make([]byte, udpMaxPayloadSize)
byts := make([]byte, st.s.MaxPacketSize)
n, err := pkt.MarshalTo(byts)
if err != nil {
return err