1
0
Fork 0
forked from External/mediamtx

add parameter listenIP to listen on a specific IP/interface (#166)

This commit is contained in:
aler9 2021-01-15 18:42:53 +01:00
parent e40235966e
commit 3b04ba36c3
6 changed files with 33 additions and 13 deletions

View file

@ -34,6 +34,7 @@ type Conf struct {
ProtocolsParsed map[gortsplib.StreamProtocol]struct{} `yaml:"-" json:"-"` ProtocolsParsed map[gortsplib.StreamProtocol]struct{} `yaml:"-" json:"-"`
Encryption string `yaml:"encryption"` Encryption string `yaml:"encryption"`
EncryptionParsed Encryption `yaml:"-" json:"-"` EncryptionParsed Encryption `yaml:"-" json:"-"`
ListenIP string `yaml:"listenIP"`
RtspPort int `yaml:"rtspPort"` RtspPort int `yaml:"rtspPort"`
RtspsPort int `yaml:"rtspsPort"` RtspsPort int `yaml:"rtspsPort"`
RTPPort int `yaml:"rtpPort"` RTPPort int `yaml:"rtpPort"`

View file

@ -26,7 +26,9 @@ type Server struct {
} }
// New allocates a Server. // New allocates a Server.
func New(port int, func New(
listenIP string,
port int,
readTimeout time.Duration, readTimeout time.Duration,
writeTimeout time.Duration, writeTimeout time.Duration,
readBufferCount uint64, readBufferCount uint64,
@ -42,7 +44,8 @@ func New(port int,
UDPRTCPListener: udpRTCPListener, UDPRTCPListener: udpRTCPListener,
} }
srv, err := conf.Serve(":" + strconv.FormatInt(int64(port), 10)) address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
srv, err := conf.Serve(address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -54,7 +57,7 @@ func New(port int,
done: make(chan struct{}), done: make(chan struct{}),
} }
parent.Log(logger.Info, "[TCP/RTSP listener] opened on :%d", port) parent.Log(logger.Info, "[TCP/RTSP listener] opened on %s", address)
go s.run() go s.run()
return s, nil return s, nil

View file

@ -27,7 +27,9 @@ type Server struct {
} }
// New allocates a Server. // New allocates a Server.
func New(port int, func New(
listenIP string,
port int,
readTimeout time.Duration, readTimeout time.Duration,
writeTimeout time.Duration, writeTimeout time.Duration,
readBufferCount uint64, readBufferCount uint64,
@ -47,7 +49,8 @@ func New(port int,
ReadBufferCount: readBufferCount, ReadBufferCount: readBufferCount,
} }
srv, err := conf.Serve(":" + strconv.FormatInt(int64(port), 10)) address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
srv, err := conf.Serve(address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -59,7 +62,7 @@ func New(port int,
done: make(chan struct{}), done: make(chan struct{}),
} }
parent.Log(logger.Info, "[TCP/TLS/RTSPS listener] opened on :%d", port) parent.Log(logger.Info, "[TCP/TLS/RTSPS listener] opened on %s", address)
go s.run() go s.run()
return s, nil return s, nil

View file

@ -13,11 +13,14 @@ type Parent interface {
} }
// New allocates a gortsplib.ServerUDPListener. // New allocates a gortsplib.ServerUDPListener.
func New(port int, func New(
listenIP string,
port int,
streamType gortsplib.StreamType, streamType gortsplib.StreamType,
parent Parent) (*gortsplib.ServerUDPListener, error) { parent Parent) (*gortsplib.ServerUDPListener, error) {
listener, err := gortsplib.NewServerUDPListener(":" + strconv.FormatInt(int64(port), 10)) address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
listener, err := gortsplib.NewServerUDPListener(address)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -28,7 +31,7 @@ func New(port int,
} }
return "RTCP" return "RTCP"
}() }()
parent.Log(logger.Info, "[UDP/"+label+" listener] opened on :%d", port) parent.Log(logger.Info, "[UDP/"+label+" listener] opened on %s", address)
return listener, nil return listener, nil
} }

16
main.go
View file

@ -183,6 +183,7 @@ func (p *program) createResources(initial bool) error {
if _, ok := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]; ok { if _, ok := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]; ok {
if p.serverUDPRTP == nil { if p.serverUDPRTP == nil {
p.serverUDPRTP, err = serverudpl.New( p.serverUDPRTP, err = serverudpl.New(
p.conf.ListenIP,
p.conf.RTPPort, p.conf.RTPPort,
gortsplib.StreamTypeRTP, gortsplib.StreamTypeRTP,
p) p)
@ -193,6 +194,7 @@ func (p *program) createResources(initial bool) error {
if p.serverUDPRTCP == nil { if p.serverUDPRTCP == nil {
p.serverUDPRTCP, err = serverudpl.New( p.serverUDPRTCP, err = serverudpl.New(
p.conf.ListenIP,
p.conf.RTCPPort, p.conf.RTCPPort,
gortsplib.StreamTypeRTCP, gortsplib.StreamTypeRTCP,
p) p)
@ -205,6 +207,7 @@ func (p *program) createResources(initial bool) error {
if p.serverPlain == nil { if p.serverPlain == nil {
if p.conf.EncryptionParsed == conf.EncryptionNo || p.conf.EncryptionParsed == conf.EncryptionOptional { if p.conf.EncryptionParsed == conf.EncryptionNo || p.conf.EncryptionParsed == conf.EncryptionOptional {
p.serverPlain, err = serverplain.New( p.serverPlain, err = serverplain.New(
p.conf.ListenIP,
p.conf.RtspPort, p.conf.RtspPort,
p.conf.ReadTimeout, p.conf.ReadTimeout,
p.conf.WriteTimeout, p.conf.WriteTimeout,
@ -221,6 +224,7 @@ func (p *program) createResources(initial bool) error {
if p.serverTLS == nil { if p.serverTLS == nil {
if p.conf.EncryptionParsed == conf.EncryptionStrict || p.conf.EncryptionParsed == conf.EncryptionOptional { if p.conf.EncryptionParsed == conf.EncryptionStrict || p.conf.EncryptionParsed == conf.EncryptionOptional {
p.serverTLS, err = servertls.New( p.serverTLS, err = servertls.New(
p.conf.ListenIP,
p.conf.RtspsPort, p.conf.RtspsPort,
p.conf.ReadTimeout, p.conf.ReadTimeout,
p.conf.WriteTimeout, p.conf.WriteTimeout,
@ -286,22 +290,25 @@ func (p *program) closeResources(newConf *conf.Conf) {
closeServerUDPRTP := false closeServerUDPRTP := false
if newConf == nil || if newConf == nil ||
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) || !reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ListenIP != p.conf.ListenIP ||
newConf.RTPPort != p.conf.RTPPort { newConf.RTPPort != p.conf.RTPPort ||
newConf.WriteTimeout != p.conf.WriteTimeout {
closeServerUDPRTP = true closeServerUDPRTP = true
} }
closeServerUDPRTCP := false closeServerUDPRTCP := false
if newConf == nil || if newConf == nil ||
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) || !reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.WriteTimeout != p.conf.WriteTimeout || newConf.ListenIP != p.conf.ListenIP ||
newConf.RTCPPort != p.conf.RTCPPort { newConf.RTCPPort != p.conf.RTCPPort ||
newConf.WriteTimeout != p.conf.WriteTimeout {
closeServerUDPRTCP = true closeServerUDPRTCP = true
} }
closeServerPlain := false closeServerPlain := false
if newConf == nil || if newConf == nil ||
newConf.EncryptionParsed != p.conf.EncryptionParsed || newConf.EncryptionParsed != p.conf.EncryptionParsed ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.RtspPort != p.conf.RtspPort || newConf.RtspPort != p.conf.RtspPort ||
newConf.ReadTimeout != p.conf.ReadTimeout || newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteTimeout != p.conf.WriteTimeout ||
@ -314,6 +321,7 @@ func (p *program) closeResources(newConf *conf.Conf) {
closeServerTLS := false closeServerTLS := false
if newConf == nil || if newConf == nil ||
newConf.EncryptionParsed != p.conf.EncryptionParsed || newConf.EncryptionParsed != p.conf.EncryptionParsed ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.RtspsPort != p.conf.RtspsPort || newConf.RtspsPort != p.conf.RtspsPort ||
newConf.ReadTimeout != p.conf.ReadTimeout || newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteTimeout != p.conf.WriteTimeout ||

View file

@ -15,6 +15,8 @@ protocols: [udp, tcp]
# encrypt handshake and TCP streams with TLS (RTSPS). # encrypt handshake and TCP streams with TLS (RTSPS).
# available values are "no", "strict", "optional". # available values are "no", "strict", "optional".
encryption: no encryption: no
# listen IP. If provided, all listeners will listen on this specific IP.
listenIP:
# port of the TCP/RTSP listener. This is used only if encryption is "no" or "optional". # port of the TCP/RTSP listener. This is used only if encryption is "no" or "optional".
rtspPort: 8554 rtspPort: 8554
# port of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional". # port of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional".