Browse Source

merge serverudpl into serverrtsp

pull/235/head
aler9 4 years ago
parent
commit
6a61202459
  1. 73
      internal/serverrtsp/server.go
  2. 37
      internal/serverudpl/server.go
  3. 156
      main.go

73
internal/serverrtsp/server.go

@ -1,7 +1,9 @@ @@ -1,7 +1,9 @@
package serverrtsp
import (
"crypto/tls"
"strconv"
"time"
"github.com/aler9/gortsplib"
@ -17,7 +19,9 @@ type Parent interface { @@ -17,7 +19,9 @@ type Parent interface {
type Server struct {
parent Parent
srv *gortsplib.Server
srv *gortsplib.Server
udpRTPListener *gortsplib.ServerUDPListener
udpRTCPListener *gortsplib.ServerUDPListener
// out
accept chan *gortsplib.ServerConn
@ -28,9 +32,49 @@ type Server struct { @@ -28,9 +32,49 @@ type Server struct {
func New(
listenIP string,
port int,
conf gortsplib.ServerConf,
readTimeout time.Duration,
writeTimeout time.Duration,
readBufferCount int,
useUDP bool,
rtpPort int,
rtcpPort int,
useTLS bool,
serverCert string,
serverKey string,
parent Parent) (*Server, error) {
conf := gortsplib.ServerConf{
ReadTimeout: readTimeout,
WriteTimeout: writeTimeout,
ReadBufferCount: readBufferCount,
}
if useUDP {
address := listenIP + ":" + strconv.FormatInt(int64(rtpPort), 10)
var err error
conf.UDPRTPListener, err = gortsplib.NewServerUDPListener(address)
if err != nil {
return nil, err
}
parent.Log(logger.Info, "[RTSP/UDP/RTP listener] opened on %s", address)
address = listenIP + ":" + strconv.FormatInt(int64(rtcpPort), 10)
conf.UDPRTCPListener, err = gortsplib.NewServerUDPListener(address)
if err != nil {
return nil, err
}
parent.Log(logger.Info, "[RTSP/UDP/RTCP listener] opened on %s", address)
}
if useTLS {
cert, err := tls.LoadX509KeyPair(serverCert, serverKey)
if err != nil {
return nil, err
}
conf.TLSConfig = &tls.Config{Certificates: []tls.Certificate{cert}}
}
address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
srv, err := conf.Serve(address)
if err != nil {
@ -38,19 +82,20 @@ func New( @@ -38,19 +82,20 @@ func New(
}
s := &Server{
parent: parent,
srv: srv,
accept: make(chan *gortsplib.ServerConn),
done: make(chan struct{}),
parent: parent,
srv: srv,
udpRTPListener: conf.UDPRTPListener,
udpRTCPListener: conf.UDPRTCPListener,
accept: make(chan *gortsplib.ServerConn),
done: make(chan struct{}),
}
label := func() string {
if conf.TLSConfig != nil {
return "TCP/TLS/RTSPS"
return "RTSP/TLS"
}
return "TCP/RTSP"
return "RTSP/TCP"
}()
parent.Log(logger.Info, "[%s listener] opened on %s", label, address)
go s.run()
@ -65,7 +110,17 @@ func (s *Server) Close() { @@ -65,7 +110,17 @@ func (s *Server) Close() {
co.Close()
}
}()
s.srv.Close()
if s.udpRTPListener != nil {
s.udpRTPListener.Close()
}
if s.udpRTCPListener != nil {
s.udpRTCPListener.Close()
}
<-s.done
}

37
internal/serverudpl/server.go

@ -1,37 +0,0 @@ @@ -1,37 +0,0 @@
package serverudpl
import (
"strconv"
"github.com/aler9/gortsplib"
"github.com/aler9/rtsp-simple-server/internal/logger"
)
// Parent is implemented by program.
type Parent interface {
Log(logger.Level, string, ...interface{})
}
// New allocates a gortsplib.ServerUDPListener.
func New(
listenIP string,
port int,
streamType gortsplib.StreamType,
parent Parent) (*gortsplib.ServerUDPListener, error) {
address := listenIP + ":" + strconv.FormatInt(int64(port), 10)
listener, err := gortsplib.NewServerUDPListener(address)
if err != nil {
return nil, err
}
label := func() string {
if streamType == gortsplib.StreamTypeRTP {
return "RTP"
}
return "RTCP"
}()
parent.Log(logger.Info, "[UDP/"+label+" listener] opened on %s", address)
return listener, nil
}

156
main.go

@ -1,7 +1,6 @@ @@ -1,7 +1,6 @@
package main
import (
"crypto/tls"
"fmt"
"os"
"reflect"
@ -19,28 +18,25 @@ import ( @@ -19,28 +18,25 @@ import (
"github.com/aler9/rtsp-simple-server/internal/pprof"
"github.com/aler9/rtsp-simple-server/internal/serverrtmp"
"github.com/aler9/rtsp-simple-server/internal/serverrtsp"
"github.com/aler9/rtsp-simple-server/internal/serverudpl"
"github.com/aler9/rtsp-simple-server/internal/stats"
)
var version = "v0.0.0"
type program struct {
confPath string
conf *conf.Conf
confFound bool
stats *stats.Stats
logger *logger.Logger
metrics *metrics.Metrics
pprof *pprof.Pprof
serverUDPRTP *gortsplib.ServerUDPListener
serverUDPRTCP *gortsplib.ServerUDPListener
serverPlain *serverrtsp.Server
serverTLS *serverrtsp.Server
serverRTMP *serverrtmp.Server
pathMan *pathman.PathManager
clientMan *clientman.ClientManager
confWatcher *confwatcher.ConfWatcher
confPath string
conf *conf.Conf
confFound bool
stats *stats.Stats
logger *logger.Logger
metrics *metrics.Metrics
pprof *pprof.Pprof
serverRTSPPlain *serverrtsp.Server
serverRTSPTLS *serverrtsp.Server
serverRTMP *serverrtmp.Server
pathMan *pathman.PathManager
clientMan *clientman.ClientManager
confWatcher *confwatcher.ConfWatcher
terminate chan struct{}
done chan struct{}
@ -187,45 +183,22 @@ func (p *program) createResources(initial bool) error { @@ -187,45 +183,22 @@ 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)
if err != nil {
return err
}
}
if p.serverUDPRTCP == nil {
p.serverUDPRTCP, err = serverudpl.New(
p.conf.ListenIP,
p.conf.RTCPPort,
gortsplib.StreamTypeRTCP,
p)
if err != nil {
return err
}
}
}
if p.conf.EncryptionParsed == conf.EncryptionNo ||
p.conf.EncryptionParsed == conf.EncryptionOptional {
if p.serverPlain == nil {
conf := gortsplib.ServerConf{
ReadTimeout: p.conf.ReadTimeout,
WriteTimeout: p.conf.WriteTimeout,
ReadBufferCount: p.conf.ReadBufferCount,
UDPRTPListener: p.serverUDPRTP,
UDPRTCPListener: p.serverUDPRTCP,
}
p.serverPlain, err = serverrtsp.New(
if p.serverRTSPPlain == nil {
_, useUDP := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]
p.serverRTSPPlain, err = serverrtsp.New(
p.conf.ListenIP,
p.conf.RTSPPort,
conf,
p.conf.ReadTimeout,
p.conf.WriteTimeout,
p.conf.ReadBufferCount,
useUDP,
p.conf.RTPPort,
p.conf.RTCPPort,
false,
"",
"",
p)
if err != nil {
return err
@ -235,23 +208,19 @@ func (p *program) createResources(initial bool) error { @@ -235,23 +208,19 @@ func (p *program) createResources(initial bool) error {
if p.conf.EncryptionParsed == conf.EncryptionStrict ||
p.conf.EncryptionParsed == conf.EncryptionOptional {
if p.serverTLS == nil {
cert, err := tls.LoadX509KeyPair(p.conf.ServerCert, p.conf.ServerKey)
if err != nil {
return err
}
conf := gortsplib.ServerConf{
TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}},
ReadTimeout: p.conf.ReadTimeout,
WriteTimeout: p.conf.WriteTimeout,
ReadBufferCount: p.conf.ReadBufferCount,
}
p.serverTLS, err = serverrtsp.New(
if p.serverRTSPTLS == nil {
p.serverRTSPTLS, err = serverrtsp.New(
p.conf.ListenIP,
p.conf.RTSPSPort,
conf,
p.conf.ReadTimeout,
p.conf.WriteTimeout,
p.conf.ReadBufferCount,
false,
0,
0,
true,
p.conf.ServerCert,
p.conf.ServerKey,
p)
if err != nil {
return err
@ -293,8 +262,8 @@ func (p *program) createResources(initial bool) error { @@ -293,8 +262,8 @@ func (p *program) createResources(initial bool) error {
p.conf.ProtocolsParsed,
p.stats,
p.pathMan,
p.serverPlain,
p.serverTLS,
p.serverRTSPPlain,
p.serverRTSPTLS,
p.serverRTMP,
p)
}
@ -324,24 +293,6 @@ func (p *program) closeResources(newConf *conf.Conf) { @@ -324,24 +293,6 @@ func (p *program) closeResources(newConf *conf.Conf) {
closePprof = true
}
closeServerUDPRTP := false
if newConf == nil ||
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
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.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 ||
@ -350,8 +301,9 @@ func (p *program) closeResources(newConf *conf.Conf) { @@ -350,8 +301,9 @@ func (p *program) closeResources(newConf *conf.Conf) {
newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.WriteTimeout != p.conf.WriteTimeout ||
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closeServerUDPRTP ||
closeServerUDPRTCP {
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.RTPPort != p.conf.RTPPort ||
newConf.RTCPPort != p.conf.RTCPPort {
closeServerPlain = true
}
@ -362,7 +314,9 @@ func (p *program) closeResources(newConf *conf.Conf) { @@ -362,7 +314,9 @@ func (p *program) closeResources(newConf *conf.Conf) {
newConf.RTSPSPort != p.conf.RTSPSPort ||
newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.WriteTimeout != p.conf.WriteTimeout ||
newConf.ReadBufferCount != p.conf.ReadBufferCount {
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
newConf.ServerCert != p.conf.ServerCert ||
newConf.ServerKey != p.conf.ServerKey {
closeServerTLS = true
}
@ -422,24 +376,14 @@ func (p *program) closeResources(newConf *conf.Conf) { @@ -422,24 +376,14 @@ func (p *program) closeResources(newConf *conf.Conf) {
p.serverRTMP = nil
}
if closeServerTLS && p.serverTLS != nil {
p.serverTLS.Close()
p.serverTLS = nil
}
if closeServerPlain && p.serverPlain != nil {
p.serverPlain.Close()
p.serverPlain = nil
}
if closeServerUDPRTCP && p.serverUDPRTCP != nil {
p.serverUDPRTCP.Close()
p.serverUDPRTCP = nil
if closeServerTLS && p.serverRTSPTLS != nil {
p.serverRTSPTLS.Close()
p.serverRTSPTLS = nil
}
if closeServerUDPRTP && p.serverUDPRTP != nil {
p.serverUDPRTP.Close()
p.serverUDPRTP = nil
if closeServerPlain && p.serverRTSPPlain != nil {
p.serverRTSPPlain.Close()
p.serverRTSPPlain = nil
}
if closePprof && p.pprof != nil {

Loading…
Cancel
Save