From a1a56ff2038fe697f59d1baa868dcccba8957fc6 Mon Sep 17 00:00:00 2001 From: aler9 <46489434+aler9@users.noreply.github.com> Date: Sat, 24 Apr 2021 18:25:19 +0200 Subject: [PATCH] allow to set a different listen IP for each listener (#343) --- Makefile | 6 ++-- README.md | 4 +-- bench/proxy/start.sh | 6 ++-- go.mod | 2 +- go.sum | 4 +-- internal/clientman/clientman.go | 12 +++---- internal/clientrtmp/client.go | 13 ++++---- internal/clientrtsp/client.go | 15 +++++---- internal/conf/conf.go | 57 +++++++++++++++------------------ internal/conf/conf_test.go | 6 ++-- internal/metrics/metrics.go | 4 +-- internal/path/path.go | 14 ++++---- internal/pathman/pathman.go | 8 ++--- internal/pprof/pprof.go | 6 ++-- internal/serverhls/server.go | 6 ++-- internal/serverrtmp/server.go | 5 +-- internal/serverrtsp/server.go | 13 +++----- main.go | 56 +++++++++++++------------------- main_clientrtsp_test.go | 2 +- main_sourcertsp_test.go | 12 +++---- rtsp-simple-server.yml | 34 +++++++++----------- 21 files changed, 129 insertions(+), 156 deletions(-) diff --git a/Makefile b/Makefile index e4e9bb2f..ec0ebe75 100644 --- a/Makefile +++ b/Makefile @@ -84,9 +84,9 @@ endef export DOCKERFILE_RUN define CONFIG_RUN -#rtspPort: 8555 -#rtpPort: 8002 -#rtcpPort: 8003 +#rtspAddress: :8555 +#rtpAddress: :8002 +#rtcpAddress: :8003 #metrics: yes #pprof: yes diff --git a/README.md b/README.md index 41867ace..1c611cff 100644 --- a/README.md +++ b/README.md @@ -127,10 +127,10 @@ There are two ways to change the configuration: docker run --rm -it --network=host -v $PWD/rtsp-simple-server.yml:/rtsp-simple-server.yml aler9/rtsp-simple-server ``` -* By overriding configuration parameters with environment variables, in the format `RTSP_PARAMNAME`, where `PARAMNAME` is the uppercase name of a parameter. For instance, the `rtspPort` parameter can be overridden in the following way: +* By overriding configuration parameters with environment variables, in the format `RTSP_PARAMNAME`, where `PARAMNAME` is the uppercase name of a parameter. For instance, the `rtspAddress` parameter can be overridden in the following way: ``` - RTSP_RTSPPORT=8555 ./rtsp-simple-server + RTSP_RTSPADDRESS="127.0.0.1:8554" ./rtsp-simple-server ``` Parameters in maps can be overridden by using underscores, in the following way: diff --git a/bench/proxy/start.sh b/bench/proxy/start.sh index 339cf218..1fc9b729 100644 --- a/bench/proxy/start.sh +++ b/bench/proxy/start.sh @@ -7,9 +7,9 @@ PROXY_PROTOCOL=tcp # source CONF="" -CONF="${CONF}rtspPort: 8555\n" -CONF="${CONF}rtpPort: 8002\n" -CONF="${CONF}rtcpPort: 8003\n" +CONF="${CONF}rtspAddress: :8555\n" +CONF="${CONF}rtpAddress: :8002\n" +CONF="${CONF}rtcpAddress: :8003\n" echo -e "$CONF" > /source.conf RTSP_RTMPDISABLE=yes /rtsp-simple-server /source.conf & diff --git a/go.mod b/go.mod index 73bc8e2f..aa7894d2 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.15 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-20210424151845-5480da6c83d7 + github.com/aler9/gortsplib v0.0.0-20210424164934-262f28340026 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 diff --git a/go.sum b/go.sum index a7215480..b9b7137e 100644 --- a/go.sum +++ b/go.sum @@ -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-20210424151845-5480da6c83d7 h1:Fynhg1I1cm7yTj1IzBjmWyqcRVe9iYbQDqa72u8f2HM= -github.com/aler9/gortsplib v0.0.0-20210424151845-5480da6c83d7/go.mod h1:zVCg+TQX445hh1pC5QgAuuBvvXZMWLY1XYz626dGFqY= +github.com/aler9/gortsplib v0.0.0-20210424164934-262f28340026 h1:KQ8G/yC8r1aPSMvto+L0UQEgHWgU6d6H1pCk5JVm8w4= +github.com/aler9/gortsplib v0.0.0-20210424164934-262f28340026/go.mod h1:zVCg+TQX445hh1pC5QgAuuBvvXZMWLY1XYz626dGFqY= 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= diff --git a/internal/clientman/clientman.go b/internal/clientman/clientman.go index d870b5d4..b25c4ae5 100644 --- a/internal/clientman/clientman.go +++ b/internal/clientman/clientman.go @@ -36,7 +36,7 @@ type Parent interface { type ClientManager struct { hlsSegmentCount int hlsSegmentDuration time.Duration - rtspPort int + rtspAddress string readTimeout time.Duration writeTimeout time.Duration readBufferCount int @@ -67,7 +67,7 @@ type ClientManager struct { func New( hlsSegmentCount int, hlsSegmentDuration time.Duration, - rtspPort int, + rtspAddress string, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount int, @@ -85,7 +85,7 @@ func New( cm := &ClientManager{ hlsSegmentCount: hlsSegmentCount, hlsSegmentDuration: hlsSegmentDuration, - rtspPort: rtspPort, + rtspAddress: rtspAddress, readTimeout: readTimeout, writeTimeout: writeTimeout, readBufferCount: readBufferCount, @@ -159,7 +159,7 @@ outer: case conn := <-tcpAccept: c := clientrtsp.New( false, - cm.rtspPort, + cm.rtspAddress, cm.readTimeout, cm.runOnConnect, cm.runOnConnectRestart, @@ -174,7 +174,7 @@ outer: case conn := <-tlsAccept: c := clientrtsp.New( true, - cm.rtspPort, + cm.rtspAddress, cm.readTimeout, cm.runOnConnect, cm.runOnConnectRestart, @@ -188,7 +188,7 @@ outer: case nconn := <-rtmpAccept: c := clientrtmp.New( - cm.rtspPort, + cm.rtspAddress, cm.readTimeout, cm.writeTimeout, cm.readBufferCount, diff --git a/internal/clientrtmp/client.go b/internal/clientrtmp/client.go index 30c54353..0836bed9 100644 --- a/internal/clientrtmp/client.go +++ b/internal/clientrtmp/client.go @@ -5,7 +5,6 @@ import ( "io" "net" "net/url" - "strconv" "strings" "sync" "sync/atomic" @@ -81,7 +80,7 @@ type Parent interface { // Client is a RTMP client. type Client struct { - rtspPort int + rtspAddress string readTimeout time.Duration writeTimeout time.Duration readBufferCount int @@ -102,7 +101,7 @@ type Client struct { // New allocates a Client. func New( - rtspPort int, + rtspAddress string, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount int, @@ -115,7 +114,7 @@ func New( parent Parent) *Client { c := &Client{ - rtspPort: rtspPort, + rtspAddress: rtspAddress, readTimeout: readTimeout, writeTimeout: writeTimeout, readBufferCount: readBufferCount, @@ -163,9 +162,10 @@ func (c *Client) run() { defer c.log(logger.Info, "disconnected") if c.runOnConnect != "" { + _, port, _ := net.SplitHostPort(c.rtspAddress) onConnectCmd := externalcmd.New(c.runOnConnect, c.runOnConnectRestart, externalcmd.Environment{ Path: "", - Port: strconv.FormatInt(int64(c.rtspPort), 10), + Port: port, }) defer onConnectCmd.Close() } @@ -480,10 +480,11 @@ func (c *Client) runPublish() { var onPublishCmd *externalcmd.Cmd if path.Conf().RunOnPublish != "" { + _, port, _ := net.SplitHostPort(c.rtspAddress) onPublishCmd = externalcmd.New(path.Conf().RunOnPublish, path.Conf().RunOnPublishRestart, externalcmd.Environment{ Path: path.Name(), - Port: strconv.FormatInt(int64(c.rtspPort), 10), + Port: port, }) } diff --git a/internal/clientrtsp/client.go b/internal/clientrtsp/client.go index 2cea2787..d62f20de 100644 --- a/internal/clientrtsp/client.go +++ b/internal/clientrtsp/client.go @@ -60,7 +60,7 @@ type Parent interface { // Client is a RTSP client. type Client struct { - rtspPort int + rtspAddress string readTimeout time.Duration runOnConnect string runOnConnectRestart bool @@ -92,7 +92,7 @@ type Client struct { // New allocates a Client. func New( isTLS bool, - rtspPort int, + rtspAddress string, readTimeout time.Duration, runOnConnect string, runOnConnectRestart bool, @@ -104,7 +104,7 @@ func New( parent Parent) *Client { c := &Client{ - rtspPort: rtspPort, + rtspAddress: rtspAddress, readTimeout: readTimeout, runOnConnect: runOnConnect, runOnConnectRestart: runOnConnectRestart, @@ -158,9 +158,10 @@ func (c *Client) run() { defer c.log(logger.Info, "disconnected") if c.runOnConnect != "" { + _, port, _ := net.SplitHostPort(c.rtspAddress) onConnectCmd := externalcmd.New(c.runOnConnect, c.runOnConnectRestart, externalcmd.Environment{ Path: "", - Port: strconv.FormatInt(int64(c.rtspPort), 10), + Port: port, }) defer onConnectCmd.Close() } @@ -588,9 +589,10 @@ func (c *Client) playStart() client.PlayRes { *c.conn.StreamProtocol()) if c.path.Conf().RunOnRead != "" { + _, port, _ := net.SplitHostPort(c.rtspAddress) c.onReadCmd = externalcmd.New(c.path.Conf().RunOnRead, c.path.Conf().RunOnReadRestart, externalcmd.Environment{ Path: c.path.Name(), - Port: strconv.FormatInt(int64(c.rtspPort), 10), + Port: port, }) } @@ -628,9 +630,10 @@ func (c *Client) recordStart() error { *c.conn.StreamProtocol()) if c.path.Conf().RunOnPublish != "" { + _, port, _ := net.SplitHostPort(c.rtspAddress) c.onPublishCmd = externalcmd.New(c.path.Conf().RunOnPublish, c.path.Conf().RunOnPublishRestart, externalcmd.Environment{ Path: c.path.Name(), - Port: strconv.FormatInt(int64(c.rtspPort), 10), + Port: port, }) } diff --git a/internal/conf/conf.go b/internal/conf/conf.go index ac409685..e5a35b90 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -53,14 +53,13 @@ type Conf struct { LogDestinations []string `yaml:"logDestinations"` LogDestinationsParsed map[logger.Destination]struct{} `yaml:"-" json:"-"` LogFile string `yaml:"logFile"` - ListenIP string `yaml:"listenIP"` ReadTimeout time.Duration `yaml:"readTimeout"` WriteTimeout time.Duration `yaml:"writeTimeout"` ReadBufferCount int `yaml:"readBufferCount"` Metrics bool `yaml:"metrics"` - MetricsPort int `yaml:"metricsPort"` + MetricsAddress string `yaml:"metricsAddress"` PPROF bool `yaml:"pprof"` - PPROFPort int `yaml:"pprofPort"` + PPROFAddress string `yaml:"pprofAddress"` RunOnConnect string `yaml:"runOnConnect"` RunOnConnectRestart bool `yaml:"runOnConnectRestart"` @@ -70,10 +69,10 @@ type Conf struct { ProtocolsParsed map[gortsplib.StreamProtocol]struct{} `yaml:"-" json:"-"` Encryption string `yaml:"encryption"` EncryptionParsed Encryption `yaml:"-" json:"-"` - RTSPPort int `yaml:"rtspPort"` - RTSPSPort int `yaml:"rtspsPort"` - RTPPort int `yaml:"rtpPort"` - RTCPPort int `yaml:"rtcpPort"` + RTSPAddress string `yaml:"rtspAddress"` + RTSPSAddress string `yaml:"rtspsAddress"` + RTPAddress string `yaml:"rtpAddress"` + RTCPAddress string `yaml:"rtcpAddress"` ServerKey string `yaml:"serverKey"` ServerCert string `yaml:"serverCert"` AuthMethods []string `yaml:"authMethods"` @@ -81,12 +80,12 @@ type Conf struct { ReadBufferSize int `yaml:"readBufferSize"` // rtmp - RTMPDisable bool `yaml:"rtmpDisable"` - RTMPPort int `yaml:"rtmpPort"` + RTMPDisable bool `yaml:"rtmpDisable"` + RTMPAddress string `yaml:"rtmpAddress"` // hls HLSDisable bool `yaml:"hlsDisable"` - HLSPort int `yaml:"hlsPort"` + HLSAddress string `yaml:"hlsAddress"` HLSSegmentCount int `yaml:"hlsSegmentCount"` HLSSegmentDuration time.Duration `yaml:"hlsSegmentDuration"` @@ -145,12 +144,12 @@ func (conf *Conf) fillAndCheck() error { conf.ReadBufferCount = 512 } - if conf.MetricsPort == 0 { - conf.MetricsPort = 9998 + if conf.MetricsAddress == "" { + conf.MetricsAddress = ":9998" } - if conf.PPROFPort == 0 { - conf.MetricsPort = 9999 + if conf.PPROFAddress == "" { + conf.MetricsAddress = ":9999" } if len(conf.Protocols) == 0 { @@ -194,23 +193,17 @@ func (conf *Conf) fillAndCheck() error { return fmt.Errorf("unsupported encryption value: '%s'", conf.Encryption) } - if conf.RTSPPort == 0 { - conf.RTSPPort = 8554 + if conf.RTSPAddress == "" { + conf.RTSPAddress = ":8554" } - if conf.RTSPSPort == 0 { - conf.RTSPSPort = 8555 + if conf.RTSPSAddress == "" { + conf.RTSPSAddress = ":8555" } - if conf.RTPPort == 0 { - conf.RTPPort = 8000 + if conf.RTPAddress == "" { + conf.RTPAddress = ":8000" } - if (conf.RTPPort % 2) != 0 { - return fmt.Errorf("rtp port must be even") - } - if conf.RTCPPort == 0 { - conf.RTCPPort = 8001 - } - if conf.RTCPPort != (conf.RTPPort + 1) { - return fmt.Errorf("rtcp and rtp ports must be consecutive") + if conf.RTCPAddress == "" { + conf.RTCPAddress = ":8001" } if conf.ServerKey == "" { @@ -236,12 +229,12 @@ func (conf *Conf) fillAndCheck() error { } } - if conf.RTMPPort == 0 { - conf.RTMPPort = 1935 + if conf.RTMPAddress == "" { + conf.RTMPAddress = ":1935" } - if conf.HLSPort == 0 { - conf.HLSPort = 8888 + if conf.HLSAddress == "" { + conf.HLSAddress = ":8888" } if conf.HLSSegmentCount == 0 { conf.HLSSegmentCount = 5 diff --git a/internal/conf/conf_test.go b/internal/conf/conf_test.go index c51f9a33..1733192d 100644 --- a/internal/conf/conf_test.go +++ b/internal/conf/conf_test.go @@ -36,8 +36,8 @@ func TestEnvironment(t *testing.T) { defer os.Unsetenv("RTSP_RUNONCONNECT") // int - os.Setenv("RTSP_RTSPPORT", "8555") - defer os.Unsetenv("RTSP_RTSPPORT") + os.Setenv("RTSP_READBUFFERCOUNT", "3000") + defer os.Unsetenv("RTSP_READBUFFERCOUNT") // bool os.Setenv("RTSP_METRICS", "yes") @@ -79,7 +79,7 @@ func TestEnvironment(t *testing.T) { require.Equal(t, "test=cmd", conf.RunOnConnect) - require.Equal(t, 8555, conf.RTSPPort) + require.Equal(t, 3000, conf.ReadBufferCount) require.Equal(t, true, conf.Metrics) diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index c9915a12..95e05577 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -34,13 +34,11 @@ type Metrics struct { // New allocates a metrics. func New( - listenIP string, - port int, + address string, stats *stats.Stats, parent Parent, ) (*Metrics, error) { - address := listenIP + ":" + strconv.FormatInt(int64(port), 10) listener, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/path/path.go b/internal/path/path.go index b1ad2a5f..cd110740 100644 --- a/internal/path/path.go +++ b/internal/path/path.go @@ -2,7 +2,7 @@ package path import ( "fmt" - "strconv" + "net" "strings" "sync" "sync/atomic" @@ -59,7 +59,7 @@ const ( // Path is a path. type Path struct { - rtspPort int + rtspAddress string readTimeout time.Duration writeTimeout time.Duration readBufferCount int @@ -105,7 +105,7 @@ type Path struct { // New allocates a Path. func New( - rtspPort int, + rtspAddress string, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount int, @@ -118,7 +118,7 @@ func New( parent Parent) *Path { pa := &Path{ - rtspPort: rtspPort, + rtspAddress: rtspAddress, readTimeout: readTimeout, writeTimeout: writeTimeout, readBufferCount: readBufferCount, @@ -175,9 +175,10 @@ func (pa *Path) run() { var onInitCmd *externalcmd.Cmd if pa.conf.RunOnInit != "" { pa.Log(logger.Info, "on init command started") + _, port, _ := net.SplitHostPort(pa.rtspAddress) onInitCmd = externalcmd.New(pa.conf.RunOnInit, pa.conf.RunOnInitRestart, externalcmd.Environment{ Path: pa.name, - Port: strconv.FormatInt(int64(pa.rtspPort), 10), + Port: port, }) } @@ -536,9 +537,10 @@ func (pa *Path) fixedPublisherStart() { // start on-demand command if pa.onDemandCmd == nil { pa.Log(logger.Info, "on demand command started") + _, port, _ := net.SplitHostPort(pa.rtspAddress) pa.onDemandCmd = externalcmd.New(pa.conf.RunOnDemand, pa.conf.RunOnDemandRestart, externalcmd.Environment{ Path: pa.name, - Port: strconv.FormatInt(int64(pa.rtspPort), 10), + Port: port, }) if pa.sourceState != sourceStateWaitingDescribe { diff --git a/internal/pathman/pathman.go b/internal/pathman/pathman.go index 2b426ac2..5eff1e38 100644 --- a/internal/pathman/pathman.go +++ b/internal/pathman/pathman.go @@ -21,7 +21,7 @@ type Parent interface { // PathManager is a path.Path manager. type PathManager struct { - rtspPort int + rtspAddress string readTimeout time.Duration writeTimeout time.Duration readBufferCount int @@ -49,7 +49,7 @@ type PathManager struct { // New allocates a PathManager. func New( - rtspPort int, + rtspAddress string, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount int, @@ -60,7 +60,7 @@ func New( parent Parent) *PathManager { pm := &PathManager{ - rtspPort: rtspPort, + rtspAddress: rtspAddress, readTimeout: readTimeout, writeTimeout: writeTimeout, readBufferCount: readBufferCount, @@ -280,7 +280,7 @@ outer: func (pm *PathManager) createPath(confName string, conf *conf.PathConf, name string) { pm.paths[name] = path.New( - pm.rtspPort, + pm.rtspAddress, pm.readTimeout, pm.writeTimeout, pm.readBufferCount, diff --git a/internal/pprof/pprof.go b/internal/pprof/pprof.go index 38cb5920..14089166 100644 --- a/internal/pprof/pprof.go +++ b/internal/pprof/pprof.go @@ -4,7 +4,6 @@ import ( "context" "net" "net/http" - "strconv" // start pprof _ "net/http/pprof" @@ -25,11 +24,10 @@ type PPROF struct { // New allocates a PPROF. func New( - listenIP string, - port int, + address string, parent Parent, ) (*PPROF, error) { - address := listenIP + ":" + strconv.FormatInt(int64(port), 10) + listener, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/serverhls/server.go b/internal/serverhls/server.go index d96d6fee..a4178047 100644 --- a/internal/serverhls/server.go +++ b/internal/serverhls/server.go @@ -5,7 +5,6 @@ import ( "io" "net" "net/http" - "strconv" "strings" "github.com/aler9/rtsp-simple-server/internal/logger" @@ -38,11 +37,10 @@ type Server struct { // New allocates a Server. func New( - listenIP string, - port int, + address string, parent Parent, ) (*Server, error) { - address := listenIP + ":" + strconv.FormatInt(int64(port), 10) + ln, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/serverrtmp/server.go b/internal/serverrtmp/server.go index d035d23c..ebeb3e5b 100644 --- a/internal/serverrtmp/server.go +++ b/internal/serverrtmp/server.go @@ -2,7 +2,6 @@ package serverrtmp import ( "net" - "strconv" "sync/atomic" "github.com/aler9/rtsp-simple-server/internal/logger" @@ -27,11 +26,9 @@ type Server struct { // New allocates a Server. func New( - listenIP string, - port int, + address string, parent Parent) (*Server, error) { - address := listenIP + ":" + strconv.FormatInt(int64(port), 10) l, err := net.Listen("tcp", address) if err != nil { return nil, err diff --git a/internal/serverrtsp/server.go b/internal/serverrtsp/server.go index ad951e59..e472a62c 100644 --- a/internal/serverrtsp/server.go +++ b/internal/serverrtsp/server.go @@ -2,7 +2,6 @@ package serverrtsp import ( "crypto/tls" - "strconv" "sync/atomic" "time" @@ -31,15 +30,14 @@ type Server struct { // New allocates a Server. func New( - listenIP string, - port int, + address string, readTimeout time.Duration, writeTimeout time.Duration, readBufferCount int, readBufferSize int, useUDP bool, - rtpPort int, - rtcpPort int, + rtpAddress string, + rtcpAddress string, useTLS bool, serverCert string, serverKey string, @@ -53,8 +51,8 @@ func New( } if useUDP { - conf.UDPRTPAddress = listenIP + ":" + strconv.FormatInt(int64(rtpPort), 10) - conf.UDPRTCPAddress = listenIP + ":" + strconv.FormatInt(int64(rtcpPort), 10) + conf.UDPRTPAddress = rtpAddress + conf.UDPRTCPAddress = rtcpAddress } if useTLS { @@ -66,7 +64,6 @@ func New( conf.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}} } - address := listenIP + ":" + strconv.FormatInt(int64(port), 10) srv, err := conf.Serve(address) if err != nil { return nil, err diff --git a/main.go b/main.go index 5d83921e..40defea7 100644 --- a/main.go +++ b/main.go @@ -171,8 +171,7 @@ func (p *program) createResources(initial bool) error { if p.conf.Metrics { if p.metrics == nil { p.metrics, err = metrics.New( - p.conf.ListenIP, - p.conf.MetricsPort, + p.conf.MetricsAddress, p.stats, p) if err != nil { @@ -184,8 +183,7 @@ func (p *program) createResources(initial bool) error { if p.conf.PPROF { if p.pprof == nil { p.pprof, err = pprof.New( - p.conf.ListenIP, - p.conf.PPROFPort, + p.conf.PPROFAddress, p) if err != nil { return err @@ -199,15 +197,14 @@ func (p *program) createResources(initial bool) error { if p.serverRTSPPlain == nil { _, useUDP := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP] p.serverRTSPPlain, err = serverrtsp.New( - p.conf.ListenIP, - p.conf.RTSPPort, + p.conf.RTSPAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, p.conf.ReadBufferCount, p.conf.ReadBufferSize, useUDP, - p.conf.RTPPort, - p.conf.RTCPPort, + p.conf.RTPAddress, + p.conf.RTCPAddress, false, "", "", @@ -223,15 +220,14 @@ func (p *program) createResources(initial bool) error { p.conf.EncryptionParsed == conf.EncryptionOptional) { if p.serverRTSPTLS == nil { p.serverRTSPTLS, err = serverrtsp.New( - p.conf.ListenIP, - p.conf.RTSPSPort, + p.conf.RTSPSAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, p.conf.ReadBufferCount, p.conf.ReadBufferSize, false, - 0, - 0, + "", + "", true, p.conf.ServerCert, p.conf.ServerKey, @@ -245,8 +241,7 @@ func (p *program) createResources(initial bool) error { if !p.conf.RTMPDisable { if p.serverRTMP == nil { p.serverRTMP, err = serverrtmp.New( - p.conf.ListenIP, - p.conf.RTMPPort, + p.conf.RTMPAddress, p) if err != nil { return err @@ -257,8 +252,7 @@ func (p *program) createResources(initial bool) error { if !p.conf.HLSDisable { if p.serverHLS == nil { p.serverHLS, err = serverhls.New( - p.conf.ListenIP, - p.conf.HLSPort, + p.conf.HLSAddress, p) if err != nil { return err @@ -268,7 +262,7 @@ func (p *program) createResources(initial bool) error { if p.pathMan == nil { p.pathMan = pathman.New( - p.conf.RTSPPort, + p.conf.RTSPAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, p.conf.ReadBufferCount, @@ -283,7 +277,7 @@ func (p *program) createResources(initial bool) error { p.clientMan = clientman.New( p.conf.HLSSegmentCount, p.conf.HLSSegmentDuration, - p.conf.RTSPPort, + p.conf.RTSPAddress, p.conf.ReadTimeout, p.conf.WriteTimeout, p.conf.ReadBufferCount, @@ -313,16 +307,14 @@ func (p *program) closeResources(newConf *conf.Conf) { closeMetrics := false if newConf == nil || newConf.Metrics != p.conf.Metrics || - newConf.ListenIP != p.conf.ListenIP || - newConf.MetricsPort != p.conf.MetricsPort { + newConf.MetricsAddress != p.conf.MetricsAddress { closeMetrics = true } closePPROF := false if newConf == nil || newConf.PPROF != p.conf.PPROF || - newConf.ListenIP != p.conf.ListenIP || - newConf.PPROFPort != p.conf.PPROFPort { + newConf.PPROFAddress != p.conf.PPROFAddress { closePPROF = true } @@ -330,14 +322,13 @@ func (p *program) closeResources(newConf *conf.Conf) { if newConf == nil || newConf.RTSPDisable != p.conf.RTSPDisable || newConf.EncryptionParsed != p.conf.EncryptionParsed || - newConf.ListenIP != p.conf.ListenIP || - newConf.RTSPPort != p.conf.RTSPPort || + newConf.RTSPAddress != p.conf.RTSPAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ReadBufferCount != p.conf.ReadBufferCount || !reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) || - newConf.RTPPort != p.conf.RTPPort || - newConf.RTCPPort != p.conf.RTCPPort { + newConf.RTPAddress != p.conf.RTPAddress || + newConf.RTCPAddress != p.conf.RTCPAddress { closeServerPlain = true } @@ -345,8 +336,7 @@ func (p *program) closeResources(newConf *conf.Conf) { if newConf == nil || newConf.RTSPDisable != p.conf.RTSPDisable || newConf.EncryptionParsed != p.conf.EncryptionParsed || - newConf.ListenIP != p.conf.ListenIP || - newConf.RTSPSPort != p.conf.RTSPSPort || + newConf.RTSPSAddress != p.conf.RTSPSAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ReadBufferCount != p.conf.ReadBufferCount || @@ -358,8 +348,7 @@ func (p *program) closeResources(newConf *conf.Conf) { closeServerRTMP := false if newConf == nil || newConf.RTMPDisable != p.conf.RTMPDisable || - newConf.ListenIP != p.conf.ListenIP || - newConf.RTMPPort != p.conf.RTMPPort || + newConf.RTMPAddress != p.conf.RTMPAddress || newConf.ReadTimeout != p.conf.ReadTimeout { closeServerRTMP = true } @@ -367,14 +356,13 @@ func (p *program) closeResources(newConf *conf.Conf) { closeServerHLS := false if newConf == nil || newConf.HLSDisable != p.conf.HLSDisable || - newConf.ListenIP != p.conf.ListenIP || - newConf.HLSPort != p.conf.HLSPort { + newConf.HLSAddress != p.conf.HLSAddress { closeServerHLS = true } closePathMan := false if newConf == nil || - newConf.RTSPPort != p.conf.RTSPPort || + newConf.RTSPAddress != p.conf.RTSPAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ReadBufferCount != p.conf.ReadBufferCount || @@ -394,7 +382,7 @@ func (p *program) closeResources(newConf *conf.Conf) { closePathMan || newConf.HLSSegmentCount != p.conf.HLSSegmentCount || newConf.HLSSegmentDuration != p.conf.HLSSegmentDuration || - newConf.RTSPPort != p.conf.RTSPPort || + newConf.RTSPAddress != p.conf.RTSPAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ReadBufferCount != p.conf.ReadBufferCount || diff --git a/main_clientrtsp_test.go b/main_clientrtsp_test.go index 345a0714..615a1d86 100644 --- a/main_clientrtsp_test.go +++ b/main_clientrtsp_test.go @@ -596,7 +596,7 @@ func TestClientRTSPNonCompliantFrameSize(t *testing.T) { "hlsDisable: yes\n" + "protocols: [tcp]\n" + "readBufferSize: 4500\n" + - "rtspPort: 8555\n" + + "rtspAddress: :8555\n" + "paths:\n" + " teststream:\n" + " source: rtsp://" + ownDockerIP + ":8554/teststream\n" + diff --git a/main_sourcertsp_test.go b/main_sourcertsp_test.go index 03458a1b..1faed701 100644 --- a/main_sourcertsp_test.go +++ b/main_sourcertsp_test.go @@ -26,9 +26,9 @@ func TestSourceRTSP(t *testing.T) { case "udp", "tcp": p1, ok := testProgram("rtmpDisable: yes\n" + "hlsDisable: yes\n" + - "rtspPort: 8555\n" + - "rtpPort: 8100\n" + - "rtcpPort: 8101\n" + + "rtspAddress: :8555\n" + + "rtpAddress: :8100\n" + + "rtcpAddress: :8101\n" + "paths:\n" + " all:\n" + " readUser: testuser\n" + @@ -69,9 +69,9 @@ func TestSourceRTSP(t *testing.T) { p, ok := testProgram("rtmpDisable: yes\n" + "hlsDisable: yes\n" + - "rtspPort: 8555\n" + - "rtpPort: 8100\n" + - "rtcpPort: 8101\n" + + "rtspAddress: :8555\n" + + "rtpAddress: :8100\n" + + "rtcpAddress: :8101\n" + "readTimeout: 20s\n" + "protocols: [tcp]\n" + "encryption: yes\n" + diff --git a/rtsp-simple-server.yml b/rtsp-simple-server.yml index fbb4b533..6796851c 100644 --- a/rtsp-simple-server.yml +++ b/rtsp-simple-server.yml @@ -9,8 +9,6 @@ logDestinations: [stdout] # if "file" is in logDestinations, this is the file which will receive the logs. logFile: rtsp-simple-server.log -# listen IP. If provided, all listeners will listen on this specific IP. -listenIP: # timeout of read operations. readTimeout: 10s # timeout of write operations. @@ -22,13 +20,13 @@ readBufferCount: 512 # enable Prometheus-compatible metrics. metrics: no -# port of the metrics listener. -metricsPort: 9998 +# address of the metrics listener. +metricsAddress: :9998 # enable pprof-compatible endpoint to monitor performances. pprof: no -# port of the pprof listener. -pprofPort: 9999 +# address of the pprof listener. +pprofAddress: :9999 # command to run when a client connects to the server. # this is terminated with SIGINT when a client disconnects from the server. @@ -51,14 +49,14 @@ protocols: [udp, tcp] # encrypt handshake and TCP streams with TLS (RTSPS). # available values are "no", "strict", "optional". encryption: no -# port of the TCP/RTSP listener. This is used only if encryption is "no" or "optional". -rtspPort: 8554 -# port of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional". -rtspsPort: 8555 -# port of the UDP/RTP listener. This is used only if "udp" is in protocols. -rtpPort: 8000 -# port of the UDP/RTCP listener. This is used only if "udp" is in protocols. -rtcpPort: 8001 +# address of the TCP/RTSP listener. This is used only if encryption is "no" or "optional". +rtspAddress: :8554 +# address of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional". +rtspsAddress: :8555 +# address of the UDP/RTP listener. This is used only if "udp" is in protocols. +rtpAddress: :8000 +# address of the UDP/RTCP listener. This is used only if "udp" is in protocols. +rtcpAddress: :8001 # path to the server key. This is used only if encryption is "strict" or "optional". # this can be generated with: # openssl genrsa -out server.key 2048 @@ -78,16 +76,16 @@ readBufferSize: 2048 # disable support for the RTMP protocol. rtmpDisable: no -# port of the RTMP listener. -rtmpPort: 1935 +# address of the RTMP listener. +rtmpAddress: :1935 ############################################### # HLS options # disable support for the HLS protocol. hlsDisable: no -# port of the HLS listener. -hlsPort: 8888 +# address of the HLS listener. +hlsAddress: :8888 # number of HLS segments to generate. # increasing segments allows more buffering, # decreasing segments decrease latency.