Skip to content

Commit 74eaa11

Browse files
authored
rtsp: add rtspUDPSourcePortRange param (#5363) (#5397)
1 parent 88a15c7 commit 74eaa11

File tree

8 files changed

+67
-19
lines changed

8 files changed

+67
-19
lines changed

api/openapi.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,13 @@ components:
416416
type: string
417417
rtspRangeStart:
418418
type: string
419+
rtspUDPSourcePortRange:
420+
type: array
421+
minItems: 2
422+
maxItems: 2
423+
items:
424+
type: integer
425+
format: uint64
419426

420427
# RTP source
421428
rtpSDP:

docs/2-usage/02-publish.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ paths:
154154
# This can be increased to mitigate packet losses.
155155
# It defaults to the default value of the operating system.
156156
rtspUDPReadBufferSize: 0
157+
# Range of ports used as source port in outgoing UDP packets.
158+
rtspUDPSourcePortRange: [10000, 65535]
157159
```
158160

159161
All available parameters are listed in the [configuration file](/docs/references/configuration-file).

internal/conf/conf_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ func TestConfFromFile(t *testing.T) {
5252
Source: "publisher",
5353
SourceOnDemandStartTimeout: 10 * Duration(time.Second),
5454
SourceOnDemandCloseAfter: 10 * Duration(time.Second),
55+
OverridePublisher: true,
5556
AlwaysAvailableTracks: []AlwaysAvailableTrack{
5657
{Codec: "H264"},
5758
},
@@ -61,7 +62,7 @@ func TestConfFromFile(t *testing.T) {
6162
RecordMaxPartSize: 50 * 1024 * 1024,
6263
RecordSegmentDuration: 3600000000000,
6364
RecordDeleteAfter: 86400000000000,
64-
OverridePublisher: true,
65+
RTSPUDPSourcePortRange: []uint{10000, 65535},
6566
RPICameraWidth: 1920,
6667
RPICameraHeight: 1080,
6768
RPICameraContrast: 1,

internal/conf/env/env.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,31 @@ func loadEnvInternal(env map[string]string, prefix string, prv reflect.Value) er
188188
}
189189
return nil
190190

191+
case rt.Elem() == reflect.TypeOf(uint(0)):
192+
if ev, ok := env[prefix]; ok {
193+
if ev == "" {
194+
prv.Elem().Set(reflect.MakeSlice(prv.Elem().Type(), 0, 0))
195+
} else {
196+
if prv.IsNil() {
197+
prv.Set(reflect.New(rt))
198+
}
199+
200+
raw := strings.Split(ev, ",")
201+
vals := make([]uint, len(raw))
202+
203+
for i, v := range raw {
204+
tmp, err := strconv.ParseUint(v, 10, 64)
205+
if err != nil {
206+
return err
207+
}
208+
vals[i] = uint(tmp)
209+
}
210+
211+
prv.Elem().Set(reflect.ValueOf(vals))
212+
}
213+
}
214+
return nil
215+
191216
case rt.Elem() == reflect.TypeOf(float64(0)):
192217
if ev, ok := env[prefix]; ok {
193218
if ev == "" {

internal/conf/path.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,14 @@ type Path struct {
180180
SRTPublishPassphrase string `json:"srtPublishPassphrase"`
181181

182182
// RTSP source
183-
RTSPTransport RTSPTransport `json:"rtspTransport"`
184-
RTSPAnyPort bool `json:"rtspAnyPort"`
185-
SourceProtocol *RTSPTransport `json:"sourceProtocol,omitempty"` // deprecated
186-
SourceAnyPortEnable *bool `json:"sourceAnyPortEnable,omitempty"` // deprecated
187-
RTSPRangeType RTSPRangeType `json:"rtspRangeType"`
188-
RTSPRangeStart string `json:"rtspRangeStart"`
189-
RTSPUDPReadBufferSize *uint `json:"rtspUDPReadBufferSize,omitempty"` // deprecated
183+
RTSPTransport RTSPTransport `json:"rtspTransport"`
184+
RTSPAnyPort bool `json:"rtspAnyPort"`
185+
SourceProtocol *RTSPTransport `json:"sourceProtocol,omitempty"` // deprecated
186+
SourceAnyPortEnable *bool `json:"sourceAnyPortEnable,omitempty"` // deprecated
187+
RTSPRangeType RTSPRangeType `json:"rtspRangeType"`
188+
RTSPRangeStart string `json:"rtspRangeStart"`
189+
RTSPUDPReadBufferSize *uint `json:"rtspUDPReadBufferSize,omitempty"` // deprecated
190+
RTSPUDPSourcePortRange []uint `json:"rtspUDPSourcePortRange"`
190191

191192
// MPEG-TS source
192193
MPEGTSUDPReadBufferSize *uint `json:"mpegtsUDPReadBufferSize,omitempty"` // deprecated
@@ -287,6 +288,9 @@ func (pconf *Path) setDefaults() {
287288
// Publisher source
288289
pconf.OverridePublisher = true
289290

291+
// RTSP source
292+
pconf.RTSPUDPSourcePortRange = []uint{10000, 65535}
293+
290294
// Raspberry Pi Camera source
291295
pconf.RPICameraWidth = 1920
292296
pconf.RPICameraHeight = 1080

internal/staticsources/rtsp/source.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ func (s *Source) Run(params defs.StaticSourceRunParams) error {
163163
WriteQueueSize: s.WriteQueueSize,
164164
UDPReadBufferSize: int(udpReadBufferSize),
165165
AnyPortEnable: params.Conf.RTSPAnyPort,
166+
UDPSourcePortRange: [2]uint16{
167+
uint16(params.Conf.RTSPUDPSourcePortRange[0]),
168+
uint16(params.Conf.RTSPUDPSourcePortRange[1]),
169+
},
166170
OnRequest: func(req *base.Request) {
167171
s.Log(logger.Debug, "[c->s] %v", req)
168172
},

internal/staticsources/rtsp/source_test.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,18 @@ func TestSource(t *testing.T) {
141141
defer strm.Close()
142142

143143
var ur string
144-
var cnf *conf.Path
144+
cnf := &conf.Path{
145+
RTSPUDPSourcePortRange: []uint{10000, 65535},
146+
}
145147

146148
if source != "tls" {
147149
ur = "rtsp://testuser:testpass@localhost:8555/teststream"
148150
var sp conf.RTSPTransport
149151
sp.UnmarshalJSON([]byte(`"` + source + `"`)) //nolint:errcheck
150-
cnf = &conf.Path{
151-
RTSPTransport: sp,
152-
}
152+
cnf.RTSPTransport = sp
153153
} else {
154154
ur = "rtsps://testuser:testpass@localhost:8555/teststream"
155-
cnf = &conf.Path{
156-
SourceFingerprint: "33949E05FFFB5FF3E8AA16F8213A6251B4D9363804BA53233C4DA9A46D6F2739",
157-
}
155+
cnf.SourceFingerprint = "33949E05FFFB5FF3E8AA16F8213A6251B4D9363804BA53233C4DA9A46D6F2739"
158156
}
159157

160158
p := &test.StaticSourceParent{}
@@ -286,7 +284,8 @@ func TestNoPassword(t *testing.T) {
286284
Context: ctx,
287285
ResolvedSource: "rtsp://testuser:@127.0.0.1:8555/teststream",
288286
Conf: &conf.Path{
289-
RTSPTransport: sp,
287+
RTSPTransport: sp,
288+
RTSPUDPSourcePortRange: []uint{10000, 65535},
290289
},
291290
})
292291
close(done)
@@ -362,7 +361,9 @@ func TestRange(t *testing.T) {
362361
require.NoError(t, err)
363362
defer strm.Close()
364363

365-
cnf := &conf.Path{}
364+
cnf := &conf.Path{
365+
RTSPUDPSourcePortRange: []uint{10000, 65535},
366+
}
366367

367368
switch ca {
368369
case "clock":
@@ -496,7 +497,8 @@ func TestSkipBackChannel(t *testing.T) {
496497
Context: ctx,
497498
ResolvedSource: "rtsp://127.0.0.1:8555/teststream",
498499
Conf: &conf.Path{
499-
RTSPTransport: conf.RTSPTransport{Protocol: ptrOf(gortsplib.ProtocolTCP)},
500+
RTSPTransport: conf.RTSPTransport{Protocol: ptrOf(gortsplib.ProtocolTCP)},
501+
RTSPUDPSourcePortRange: []uint{10000, 65535},
500502
},
501503
})
502504
close(done)
@@ -571,7 +573,8 @@ func TestOnlyBackChannelsError(t *testing.T) {
571573
Context: ctx,
572574
ResolvedSource: "rtsp://127.0.0.1:8555/teststream",
573575
Conf: &conf.Path{
574-
RTSPTransport: conf.RTSPTransport{Protocol: ptrOf(gortsplib.ProtocolTCP)},
576+
RTSPTransport: conf.RTSPTransport{Protocol: ptrOf(gortsplib.ProtocolTCP)},
577+
RTSPUDPSourcePortRange: []uint{10000, 65535},
575578
},
576579
})
577580

mediamtx.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,8 @@ pathDefaults:
553553
# * npt: duration such as "300ms", "1.5m" or "2h45m", valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"
554554
# * smpte: duration such as "300ms", "1.5m" or "2h45m", valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h"
555555
rtspRangeStart:
556+
# Range of ports used as source port in outgoing UDP packets.
557+
rtspUDPSourcePortRange: [10000, 65535]
556558

557559
###############################################
558560
# Default path settings -> RTP source (when source is RTP)

0 commit comments

Comments
 (0)