Skip to content

Commit d73b480

Browse files
committed
extend udpReadBufferSize to WebRTC UDP sockets
this allows to decrease packet losses without touching system parameters.
1 parent 81bd3d0 commit d73b480

File tree

11 files changed

+91
-26
lines changed

11 files changed

+91
-26
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/asticode/go-astits v1.13.0
1313
github.com/bluenviron/gohlslib/v2 v2.2.3
1414
github.com/bluenviron/gortmplib v0.1.1
15-
github.com/bluenviron/gortsplib/v5 v5.1.1
15+
github.com/bluenviron/gortsplib/v5 v5.1.2-0.20251026101219-59474a8fa3ab
1616
github.com/bluenviron/mediacommon/v2 v2.5.1
1717
github.com/datarhei/gosrt v0.9.0
1818
github.com/fsnotify/fsnotify v1.9.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ github.com/bluenviron/gortmplib v0.1.1 h1:pmR6qfPcJJmE17lWQ/bpuBFZtgGnMrN8KdFj1G
3939
github.com/bluenviron/gortmplib v0.1.1/go.mod h1:XWy2YzbTP1XEEZ8232OG7I1MSwubsbDRKDNhXGgS2kg=
4040
github.com/bluenviron/gortsplib/v5 v5.1.1 h1:GC4sfMFBfx7dNARfSS8m8k3hz37TZ9V4hEMus/LPlQ4=
4141
github.com/bluenviron/gortsplib/v5 v5.1.1/go.mod h1:+4E4JNF7dpDu8LgssZu9fB3Ndh6FNbvGYMKOKR/wvvI=
42+
github.com/bluenviron/gortsplib/v5 v5.1.2-0.20251026101219-59474a8fa3ab h1:9QH6j4y2FMo299Rz/hX5jrhI+THhWgZ/oSiEmwSPavk=
43+
github.com/bluenviron/gortsplib/v5 v5.1.2-0.20251026101219-59474a8fa3ab/go.mod h1:+4E4JNF7dpDu8LgssZu9fB3Ndh6FNbvGYMKOKR/wvvI=
4244
github.com/bluenviron/mediacommon/v2 v2.5.1 h1:qB2fb5c0xyl5OB2gfSfulpEJn7Cdm3vI2n8wjiLMxKI=
4345
github.com/bluenviron/mediacommon/v2 v2.5.1/go.mod h1:zy1fODPuS/kBd93ftgJS1Jhvjq7LFWfAo32KP7By9AE=
4446
github.com/bytedance/sonic v1.14.0 h1:/OfKt8HFw0kh2rj8N0F6C/qPGRESq0BbaNZgcNXXzQQ=

internal/core/core.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,7 @@ func (p *Core) createResources(initial bool) error {
596596
TrustedProxies: p.conf.WebRTCTrustedProxies,
597597
ReadTimeout: p.conf.ReadTimeout,
598598
WriteTimeout: p.conf.WriteTimeout,
599+
UDPReadBufferSize: p.conf.UDPReadBufferSize,
599600
LocalUDPAddress: p.conf.WebRTCLocalUDPAddress,
600601
LocalTCPAddress: p.conf.WebRTCLocalTCPAddress,
601602
IPsFromInterfaces: p.conf.WebRTCIPsFromInterfaces,
@@ -877,6 +878,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
877878
!reflect.DeepEqual(newConf.WebRTCTrustedProxies, p.conf.WebRTCTrustedProxies) ||
878879
newConf.ReadTimeout != p.conf.ReadTimeout ||
879880
newConf.WriteTimeout != p.conf.WriteTimeout ||
881+
newConf.UDPReadBufferSize != p.conf.UDPReadBufferSize ||
880882
newConf.WebRTCLocalUDPAddress != p.conf.WebRTCLocalUDPAddress ||
881883
newConf.WebRTCLocalTCPAddress != p.conf.WebRTCLocalTCPAddress ||
882884
newConf.WebRTCIPsFromInterfaces != p.conf.WebRTCIPsFromInterfaces ||

internal/protocols/udp/udp.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -150,24 +150,11 @@ func CreateConn(u *url.URL, udpReadBufferSize int) (net.Conn, error) {
150150
}
151151

152152
if udpReadBufferSize != 0 {
153-
err = pc.SetReadBuffer(udpReadBufferSize)
153+
err = readbuffer.SetReadBuffer(pc, udpReadBufferSize)
154154
if err != nil {
155155
pc.Close()
156156
return nil, err
157157
}
158-
159-
var v int
160-
v, err = readbuffer.ReadBuffer(pc)
161-
if err != nil {
162-
pc.Close()
163-
return nil, err
164-
}
165-
166-
if v != udpReadBufferSize {
167-
pc.Close()
168-
return nil, fmt.Errorf("unable to set read buffer size to %v, check that the operating system allows that",
169-
udpReadBufferSize)
170-
}
171158
}
172159

173160
return &udpConn{pc: pc, sourceIP: sourceIP}, nil

internal/protocols/webrtc/net.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package webrtc
2+
3+
import (
4+
"net"
5+
6+
"github.com/bluenviron/gortsplib/v5/pkg/readbuffer"
7+
"github.com/pion/transport/v3"
8+
"github.com/pion/transport/v3/stdnet"
9+
)
10+
11+
type webrtcNet struct {
12+
udpReadBufferSize int
13+
14+
*stdnet.Net
15+
}
16+
17+
func (n *webrtcNet) initialize() error {
18+
var err error
19+
n.Net, err = stdnet.NewNet()
20+
if err != nil {
21+
return err
22+
}
23+
24+
return nil
25+
}
26+
27+
func (n *webrtcNet) ListenUDP(network string, laddr *net.UDPAddr) (transport.UDPConn, error) {
28+
conn, err := n.Net.ListenUDP(network, laddr)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
if n.udpReadBufferSize != 0 {
34+
err = readbuffer.SetReadBuffer(conn.(*net.UDPConn), n.udpReadBufferSize)
35+
if err != nil {
36+
return nil, err
37+
}
38+
}
39+
40+
return conn, nil
41+
}

internal/protocols/webrtc/peer_connection.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type trackRecvPair struct {
132132

133133
// PeerConnection is a wrapper around webrtc.PeerConnection.
134134
type PeerConnection struct {
135+
UDPReadBufferSize uint
135136
LocalRandomUDP bool
136137
ICEUDPMux ice.UDPMux
137138
ICETCPMux *TCPMuxWrapper
@@ -194,6 +195,15 @@ func (co *PeerConnection) Start() error {
194195

195196
settingsEngine.SetSTUNGatherTimeout(time.Duration(co.STUNGatherTimeout))
196197

198+
webrtcNet := &webrtcNet{
199+
udpReadBufferSize: int(co.UDPReadBufferSize),
200+
}
201+
err := webrtcNet.initialize()
202+
if err != nil {
203+
return err
204+
}
205+
settingsEngine.SetNet(webrtcNet)
206+
197207
mediaEngine := &webrtc.MediaEngine{}
198208

199209
if co.Publish {
@@ -267,7 +277,7 @@ func (co *PeerConnection) Start() error {
267277

268278
interceptorRegistry := &interceptor.Registry{}
269279

270-
err := registerInterceptors(
280+
err = registerInterceptors(
271281
mediaEngine,
272282
interceptorRegistry,
273283
func(s *statsInterceptor) {

internal/protocols/whip/client.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ const (
2626

2727
// Client is a WHIP client.
2828
type Client struct {
29-
URL *url.URL
30-
Publish bool
31-
OutgoingTracks []*webrtc.OutgoingTrack
32-
HTTPClient *http.Client
33-
Log logger.Writer
29+
URL *url.URL
30+
Publish bool
31+
OutgoingTracks []*webrtc.OutgoingTrack
32+
HTTPClient *http.Client
33+
UDPReadBufferSize uint
34+
Log logger.Writer
3435

3536
pc *webrtc.PeerConnection
3637
patchIsSupported bool
@@ -44,6 +45,7 @@ func (c *Client) Initialize(ctx context.Context) error {
4445
}
4546

4647
c.pc = &webrtc.PeerConnection{
48+
UDPReadBufferSize: c.UDPReadBufferSize,
4749
LocalRandomUDP: true,
4850
ICEServers: iceServers,
4951
IPsFromInterfaces: true,

internal/servers/webrtc/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/pion/logging"
2323
pwebrtc "github.com/pion/webrtc/v4"
2424

25+
"github.com/bluenviron/gortsplib/v5/pkg/readbuffer"
2526
"github.com/bluenviron/mediamtx/internal/conf"
2627
"github.com/bluenviron/mediamtx/internal/defs"
2728
"github.com/bluenviron/mediamtx/internal/externalcmd"
@@ -193,6 +194,7 @@ type Server struct {
193194
TrustedProxies conf.IPNetworks
194195
ReadTimeout conf.Duration
195196
WriteTimeout conf.Duration
197+
UDPReadBufferSize uint
196198
LocalUDPAddress string
197199
LocalTCPAddress string
198200
IPsFromInterfaces bool
@@ -272,6 +274,17 @@ func (s *Server) Initialize() error {
272274
ctxCancel()
273275
return err
274276
}
277+
278+
if s.UDPReadBufferSize != 0 {
279+
err = readbuffer.SetReadBuffer(s.udpMuxLn.(*net.UDPConn), int(s.UDPReadBufferSize))
280+
if err != nil {
281+
s.udpMuxLn.Close()
282+
s.httpServer.close()
283+
ctxCancel()
284+
return err
285+
}
286+
}
287+
275288
s.iceUDPMux = pwebrtc.NewICEUDPMux(webrtcNilLogger, s.udpMuxLn)
276289
}
277290

@@ -285,6 +298,7 @@ func (s *Server) Initialize() error {
285298
ctxCancel()
286299
return err
287300
}
301+
288302
s.iceTCPMux = &webrtc.TCPMuxWrapper{
289303
Mux: pwebrtc.NewICETCPMux(webrtcNilLogger, s.tcpMuxLn, 8),
290304
Ln: s.tcpMuxLn,
@@ -336,6 +350,7 @@ outer:
336350
select {
337351
case req := <-s.chNewSession:
338352
sx := &session{
353+
udpReadBufferSize: s.UDPReadBufferSize,
339354
parentCtx: s.ctx,
340355
ipsFromInterfaces: s.IPsFromInterfaces,
341356
ipsFromInterfacesList: s.IPsFromInterfacesList,

internal/servers/webrtc/session.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ type sessionParent interface {
4141
}
4242

4343
type session struct {
44+
udpReadBufferSize uint
4445
parentCtx context.Context
4546
ipsFromInterfaces bool
4647
ipsFromInterfacesList []string
@@ -158,6 +159,7 @@ func (s *session) runPublish() (int, error) {
158159
}
159160

160161
pc := &webrtc.PeerConnection{
162+
UDPReadBufferSize: s.udpReadBufferSize,
161163
ICEUDPMux: s.iceUDPMux,
162164
ICETCPMux: s.iceTCPMux,
163165
ICEServers: iceServers,
@@ -302,6 +304,7 @@ func (s *session) runRead() (int, error) {
302304
}
303305

304306
pc := &webrtc.PeerConnection{
307+
UDPReadBufferSize: s.udpReadBufferSize,
305308
ICEUDPMux: s.iceUDPMux,
306309
ICETCPMux: s.iceTCPMux,
307310
ICEServers: iceServers,

internal/staticsources/handler.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ func (s *Handler) Initialize() {
142142
case strings.HasPrefix(s.Conf.Source, "whep://") ||
143143
strings.HasPrefix(s.Conf.Source, "wheps://"):
144144
s.instance = &sswebrtc.Source{
145-
ReadTimeout: s.ReadTimeout,
146-
Parent: s,
145+
ReadTimeout: s.ReadTimeout,
146+
UDPReadBufferSize: s.UDPReadBufferSize,
147+
Parent: s,
147148
}
148149

149150
case strings.HasPrefix(s.Conf.Source, "udp+rtp://") ||

0 commit comments

Comments
 (0)