RTSP server: use one IP for each multicast stream; add multicast parameter to the configuration file

This commit is contained in:
aler9 2021-06-19 19:20:41 +02:00
parent f36951a5c1
commit fbe3d9c67e
6 changed files with 55 additions and 16 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.16
require (
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751 // indirect
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d // indirect
github.com/aler9/gortsplib v0.0.0-20210619140353-eef21cc2b564
github.com/aler9/gortsplib v0.0.0-20210619171656-47637e3ea2e2
github.com/asticode/go-astits v0.0.0-00010101000000-000000000000
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fsnotify/fsnotify v1.4.9

5
go.sum
View File

@ -4,8 +4,8 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d h1:UQZhZ2O0vMHr2c
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
github.com/aler9/go-astits v0.0.0-20210423195926-582b09ed7c04 h1:CXgQLsU4uxWAmsXNOjGLbj0A+0IlRcpZpMgI13fmVwo=
github.com/aler9/go-astits v0.0.0-20210423195926-582b09ed7c04/go.mod h1:DkOWmBNQpnr9mv24KfZjq4JawCFX1FCqjLVGvO0DygQ=
github.com/aler9/gortsplib v0.0.0-20210619140353-eef21cc2b564 h1:GbL9XsieLmqwW+FVaVtaBCLcjgvlV+S+lM3cZ2QMBgs=
github.com/aler9/gortsplib v0.0.0-20210619140353-eef21cc2b564/go.mod h1:ozu0NvgZMhb4AT6VdyV6OfmgPviSiZImRkaTwW1nEKc=
github.com/aler9/gortsplib v0.0.0-20210619171656-47637e3ea2e2 h1:70m23oScJ1Wuj4Kx2B5e5dI7Vko46U1kySCkBnVv8dQ=
github.com/aler9/gortsplib v0.0.0-20210619171656-47637e3ea2e2/go.mod h1:s5FsbPRxJhU/YedvUKAKHVY+lQEdYsiJpuN2CHb89cI=
github.com/aler9/rtmp v0.0.0-20210403095203-3be4a5535927 h1:95mXJ5fUCYpBRdSOnLAQAdJHHKxxxJrVCiaqDi965YQ=
github.com/aler9/rtmp v0.0.0-20210403095203-3be4a5535927/go.mod h1:vzuE21rowz+lT1NGsWbreIvYulgBpCGnQyeTyFblUHc=
github.com/asticode/go-astikit v0.20.0 h1:+7N+J4E4lWx2QOkRdOf6DafWJMv6O4RRfgClwQokrH8=
@ -51,7 +51,6 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20191005200804-aed5e4c7ecf9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44 h1:Bli41pIlzTzf3KEY06n+xnzK/BESIg2ze4Pgfh/aI8c=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da h1:b3NXsE2LusjYGGjL5bxEVZZORm/YEFFrWFjR8eFrw/c=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

View File

@ -82,6 +82,9 @@ type Conf struct {
RTSPSAddress string `yaml:"rtspsAddress"`
RTPAddress string `yaml:"rtpAddress"`
RTCPAddress string `yaml:"rtcpAddress"`
MulticastIPRange string `yaml:"multicastIPRange"`
MulticastRTPPort int `yaml:"multicastRTPPort"`
MulticastRTCPPort int `yaml:"multicastRTCPPort"`
ServerKey string `yaml:"serverKey"`
ServerCert string `yaml:"serverCert"`
AuthMethods []string `yaml:"authMethods"`
@ -217,6 +220,15 @@ func (conf *Conf) fillAndCheck() error {
if conf.RTCPAddress == "" {
conf.RTCPAddress = ":8001"
}
if conf.MulticastIPRange == "" {
conf.MulticastIPRange = "224.1.0.0/16"
}
if conf.MulticastRTPPort == 0 {
conf.MulticastRTPPort = 8002
}
if conf.MulticastRTCPPort == 0 {
conf.MulticastRTCPPort = 8003
}
if conf.ServerKey == "" {
conf.ServerKey = "server.key"

View File

@ -79,8 +79,12 @@ func New(
readBufferCount int,
readBufferSize int,
useUDP bool,
useMulticast bool,
rtpAddress string,
rtcpAddress string,
multicastIPRange string,
multicastRTPPort int,
multicastRTCPPort int,
isTLS bool,
serverCert string,
serverKey string,
@ -120,6 +124,12 @@ func New(
s.srv.UDPRTCPAddress = rtcpAddress
}
if useMulticast {
s.srv.MulticastIPRange = multicastIPRange
s.srv.MulticastRTPPort = multicastRTPPort
s.srv.MulticastRTCPPort = multicastRTCPPort
}
if isTLS {
cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
if err != nil {

12
main.go
View File

@ -215,6 +215,7 @@ func (p *program) createResources(initial bool) error {
p.conf.EncryptionParsed == conf.EncryptionOptional) {
if p.serverRTSPPlain == nil {
_, useUDP := p.conf.ProtocolsParsed[conf.ProtocolUDP]
_, useMulticast := p.conf.ProtocolsParsed[conf.ProtocolMulticast]
p.serverRTSPPlain, err = rtspserver.New(
p.ctx,
p.conf.RTSPAddress,
@ -223,8 +224,12 @@ func (p *program) createResources(initial bool) error {
p.conf.ReadBufferCount,
p.conf.ReadBufferSize,
useUDP,
useMulticast,
p.conf.RTPAddress,
p.conf.RTCPAddress,
p.conf.MulticastIPRange,
p.conf.MulticastRTPPort,
p.conf.MulticastRTCPPort,
false,
"",
"",
@ -253,8 +258,12 @@ func (p *program) createResources(initial bool) error {
p.conf.ReadBufferCount,
p.conf.ReadBufferSize,
false,
false,
"",
"",
"",
0,
0,
true,
p.conf.ServerCert,
p.conf.ServerKey,
@ -365,6 +374,9 @@ func (p *program) closeResources(newConf *conf.Conf) {
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.RTPAddress != p.conf.RTPAddress ||
newConf.RTCPAddress != p.conf.RTCPAddress ||
newConf.MulticastIPRange != p.conf.MulticastIPRange ||
newConf.MulticastRTPPort != p.conf.MulticastRTPPort ||
newConf.MulticastRTCPPort != p.conf.MulticastRTCPPort ||
newConf.RTSPAddress != p.conf.RTSPAddress ||
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.RunOnConnect != p.conf.RunOnConnect ||

View File

@ -1,6 +1,6 @@
###############################################
# General options
# General parameters
# sets the verbosity of the program; available values are "warn", "info", "debug".
logLevel: info
@ -36,7 +36,7 @@ runOnConnect:
runOnConnectRestart: no
###############################################
# RTSP options
# RTSP parameters
# disable support for the RTSP protocol.
rtspDisable: no
@ -50,20 +50,26 @@ protocols: [udp, multicast, tcp]
# encrypt handshake and TCP streams with TLS (RTSPS).
# available values are "no", "strict", "optional".
encryption: no
# address of the TCP/RTSP listener. This is used only if encryption is "no" or "optional".
# address of the TCP/RTSP listener. This is needed only when encryption is "no" or "optional".
rtspAddress: :8554
# address of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional".
# address of the TCP/TLS/RTSPS listener. This is needed only when encryption is "strict" or "optional".
rtspsAddress: :8555
# address of the UDP/RTP listener. This is used only if "udp" is in protocols.
# address of the UDP/RTP listener. This is needed only when "udp" is in protocols.
rtpAddress: :8000
# address of the UDP/RTCP listener. This is used only if "udp" is in protocols.
# address of the UDP/RTCP listener. This is needed only when "udp" is in protocols.
rtcpAddress: :8001
# path to the server key. This is used only if encryption is "strict" or "optional".
# IP range of all UDP-multicast listeners. This is needed only when "udp" is in protocols.
multicastIPRange: 224.1.0.0/16
# port of all UDP-multicast/RTP listeners. This is needed only when "udp" is in protocols.
multicastRTPPort: 8002
# port of all UDP-multicast/RTCP listeners. This is needed only when "udp" is in protocols.
multicastRTCPPort: 8003
# path to the server key. This is needed only when encryption is "strict" or "optional".
# this can be generated with:
# openssl genrsa -out server.key 2048
# openssl req -new -x509 -sha256 -key server.key -out server.crt -days 3650
serverKey: server.key
# path to the server certificate. This is used only if encryption is "strict" or "optional".
# path to the server certificate. This is needed only when encryption is "strict" or "optional".
serverCert: server.crt
# authentication methods.
authMethods: [basic, digest]
@ -73,7 +79,7 @@ authMethods: [basic, digest]
readBufferSize: 2048
###############################################
# RTMP options
# RTMP parameters
# disable support for the RTMP protocol.
rtmpDisable: no
@ -81,7 +87,7 @@ rtmpDisable: no
rtmpAddress: :1935
###############################################
# HLS options
# HLS parameters
# disable support for the HLS protocol.
hlsDisable: no
@ -97,7 +103,7 @@ hlsSegmentCount: 3
hlsSegmentDuration: 1s
###############################################
# Path options
# Path parameters
# these settings are path-dependent.
# it's possible to use regular expressions by using a tilde as prefix.
@ -116,7 +122,7 @@ paths:
source: record
# if the source is an RTSP or RTSPS URL, this is the protocol that will be used to
# pull the stream. available options are "automatic", "udp", "multicast", "tcp".
# pull the stream. available values are "automatic", "udp", "multicast", "tcp".
# the TCP protocol can help to overcome the error "no UDP packets received recently".
sourceProtocol: automatic