Browse Source

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

pull/190/head
aler9 5 years ago
parent
commit
3b04ba36c3
  1. 1
      internal/conf/conf.go
  2. 9
      internal/serverplain/server.go
  3. 9
      internal/servertls/server.go
  4. 9
      internal/serverudpl/server.go
  5. 16
      main.go
  6. 2
      rtsp-simple-server.yml

1
internal/conf/conf.go

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

9
internal/serverplain/server.go

@ -26,7 +26,9 @@ type Server struct { @@ -26,7 +26,9 @@ type Server struct {
}
// New allocates a Server.
func New(port int,
func New(
listenIP string,
port int,
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount uint64,
@ -42,7 +44,8 @@ func New(port int, @@ -42,7 +44,8 @@ func New(port int,
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 {
return nil, err
}
@ -54,7 +57,7 @@ func New(port int, @@ -54,7 +57,7 @@ func New(port int,
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()
return s, nil

9
internal/servertls/server.go

@ -27,7 +27,9 @@ type Server struct { @@ -27,7 +27,9 @@ type Server struct {
}
// New allocates a Server.
func New(port int,
func New(
listenIP string,
port int,
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount uint64,
@ -47,7 +49,8 @@ func New(port int, @@ -47,7 +49,8 @@ func New(port int,
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 {
return nil, err
}
@ -59,7 +62,7 @@ func New(port int, @@ -59,7 +62,7 @@ func New(port int,
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()
return s, nil

9
internal/serverudpl/server.go

@ -13,11 +13,14 @@ type Parent interface { @@ -13,11 +13,14 @@ type Parent interface {
}
// New allocates a gortsplib.ServerUDPListener.
func New(port int,
func New(
listenIP string,
port int,
streamType gortsplib.StreamType,
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 {
return nil, err
}
@ -28,7 +31,7 @@ func New(port int, @@ -28,7 +31,7 @@ func New(port int,
}
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
}

16
main.go

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

2
rtsp-simple-server.yml

@ -15,6 +15,8 @@ protocols: [udp, tcp] @@ -15,6 +15,8 @@ protocols: [udp, tcp]
# encrypt handshake and TCP streams with TLS (RTSPS).
# available values are "no", "strict", "optional".
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".
rtspPort: 8554
# port of the TCP/TLS/RTSPS listener. This is used only if encryption is "strict" or "optional".

Loading…
Cancel
Save