mediamtx/main.go

450 lines
9.9 KiB
Go
Raw Normal View History

2019-12-28 21:07:03 +00:00
package main
import (
"fmt"
"os"
"reflect"
2020-09-03 14:24:39 +00:00
"sync/atomic"
2019-12-28 21:07:03 +00:00
"github.com/aler9/gortsplib"
2019-12-28 21:07:03 +00:00
"gopkg.in/alecthomas/kingpin.v2"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/clientman"
"github.com/aler9/rtsp-simple-server/internal/conf"
"github.com/aler9/rtsp-simple-server/internal/confwatcher"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/metrics"
"github.com/aler9/rtsp-simple-server/internal/pathman"
"github.com/aler9/rtsp-simple-server/internal/pprof"
"github.com/aler9/rtsp-simple-server/internal/rlimit"
2021-01-31 15:58:57 +00:00
"github.com/aler9/rtsp-simple-server/internal/serverrtmp"
"github.com/aler9/rtsp-simple-server/internal/serverrtsp"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/stats"
2019-12-28 21:07:03 +00:00
)
2020-11-24 20:45:44 +00:00
var version = "v0.0.0"
2019-12-28 21:07:03 +00:00
2020-05-10 13:33:42 +00:00
type program struct {
2021-03-06 08:11:46 +00:00
confPath string
conf *conf.Conf
confFound bool
stats *stats.Stats
logger *logger.Logger
metrics *metrics.Metrics
2021-03-27 11:23:19 +00:00
pprof *pprof.PPROF
2021-03-06 08:11:46 +00:00
serverRTSPPlain *serverrtsp.Server
serverRTSPTLS *serverrtsp.Server
serverRTMP *serverrtmp.Server
pathMan *pathman.PathManager
clientMan *clientman.ClientManager
confWatcher *confwatcher.ConfWatcher
2020-10-19 20:17:48 +00:00
terminate chan struct{}
done chan struct{}
2020-05-10 13:33:42 +00:00
}
func newProgram(args []string) (*program, bool) {
2020-06-27 19:22:50 +00:00
k := kingpin.New("rtsp-simple-server",
2020-11-24 20:45:44 +00:00
"rtsp-simple-server "+version+"\n\nRTSP server.")
2020-05-11 07:31:56 +00:00
2020-06-27 19:22:50 +00:00
argVersion := k.Flag("version", "print version").Bool()
argConfPath := k.Arg("confpath", "path to a config file. The default is rtsp-simple-server.yml.").Default("rtsp-simple-server.yml").String()
2020-05-11 07:31:56 +00:00
kingpin.MustParse(k.Parse(args))
2020-05-11 07:31:56 +00:00
2020-12-05 19:42:59 +00:00
if *argVersion {
2020-11-24 20:45:44 +00:00
fmt.Println(version)
2020-05-10 19:32:40 +00:00
os.Exit(0)
2020-01-26 11:58:56 +00:00
}
// on Linux, try to raise the number of file descriptors that can be opened
// to allow the maximum possible number of clients
// do not check for errors
rlimit.Raise()
2020-10-19 20:17:48 +00:00
p := &program{
confPath: *argConfPath,
2020-10-19 20:17:48 +00:00
terminate: make(chan struct{}),
done: make(chan struct{}),
2020-09-18 22:19:55 +00:00
}
var err error
p.conf, p.confFound, err = conf.Load(p.confPath)
2020-10-19 20:17:48 +00:00
if err != nil {
fmt.Printf("ERR: %s\n", err)
return nil, false
}
2020-12-13 22:43:31 +00:00
err = p.createResources(true)
2020-08-29 17:48:41 +00:00
if err != nil {
p.Log(logger.Info, "ERR: %s", err)
2020-12-13 22:43:31 +00:00
p.closeResources(nil)
return nil, false
}
if p.confFound {
p.confWatcher, err = confwatcher.New(p.confPath)
if err != nil {
p.Log(logger.Info, "ERR: %s", err)
2020-12-13 22:43:31 +00:00
p.closeResources(nil)
return nil, false
}
2020-08-29 17:48:41 +00:00
}
2020-10-19 20:17:48 +00:00
go p.run()
return p, true
2019-12-28 21:07:03 +00:00
}
func (p *program) close() {
close(p.terminate)
<-p.done
}
func (p *program) Log(level logger.Level, format string, args ...interface{}) {
2020-10-19 20:17:48 +00:00
countClients := atomic.LoadInt64(p.stats.CountClients)
countPublishers := atomic.LoadInt64(p.stats.CountPublishers)
countReaders := atomic.LoadInt64(p.stats.CountReaders)
2020-09-03 14:24:39 +00:00
p.logger.Log(level, "[%d/%d/%d] "+format, append([]interface{}{countClients,
2020-12-05 19:42:59 +00:00
countPublishers, countReaders}, args...)...)
}
2020-06-27 11:38:35 +00:00
func (p *program) run() {
defer close(p.done)
confChanged := func() chan struct{} {
if p.confWatcher != nil {
return p.confWatcher.Watch()
}
return make(chan struct{})
}()
2020-06-27 11:38:35 +00:00
outer:
for {
select {
case <-confChanged:
err := p.reloadConf()
if err != nil {
p.Log(logger.Info, "ERR: %s", err)
break outer
}
2020-08-31 22:01:17 +00:00
case <-p.terminate:
break outer
2020-06-27 11:38:35 +00:00
}
}
2020-12-13 22:43:31 +00:00
p.closeResources(nil)
if p.confWatcher != nil {
p.confWatcher.Close()
}
2020-10-19 20:17:48 +00:00
}
2020-06-27 11:38:35 +00:00
2020-12-13 22:43:31 +00:00
func (p *program) createResources(initial bool) error {
var err error
if p.stats == nil {
p.stats = stats.New()
}
if p.logger == nil {
2021-01-06 11:40:18 +00:00
p.logger, err = logger.New(
p.conf.LogLevelParsed,
p.conf.LogDestinationsParsed,
p.conf.LogFile)
if err != nil {
return err
}
}
if initial {
p.Log(logger.Info, "rtsp-simple-server %s", version)
if !p.confFound {
p.Log(logger.Warn, "configuration file not found, using the default one")
}
}
if p.conf.Metrics {
if p.metrics == nil {
p.metrics, err = metrics.New(
p.conf.ListenIP,
p.conf.MetricsPort,
p.stats,
p)
if err != nil {
return err
}
}
}
2021-03-27 11:23:19 +00:00
if p.conf.PPROF {
if p.pprof == nil {
p.pprof, err = pprof.New(
p.conf.ListenIP,
2021-03-27 11:23:19 +00:00
p.conf.PPROFPort,
p)
if err != nil {
return err
}
}
}
if !p.conf.RTSPDisable &&
(p.conf.EncryptionParsed == conf.EncryptionNo ||
p.conf.EncryptionParsed == conf.EncryptionOptional) {
2021-03-06 08:11:46 +00:00
if p.serverRTSPPlain == nil {
_, useUDP := p.conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]
p.serverRTSPPlain, err = serverrtsp.New(
p.conf.ListenIP,
p.conf.RTSPPort,
2021-03-06 08:11:46 +00:00
p.conf.ReadTimeout,
p.conf.WriteTimeout,
p.conf.ReadBufferCount,
p.conf.ReadBufferSize,
2021-03-06 08:11:46 +00:00
useUDP,
p.conf.RTPPort,
p.conf.RTCPPort,
false,
"",
"",
2021-01-06 11:40:18 +00:00
p)
2020-12-13 22:43:31 +00:00
if err != nil {
return err
}
}
}
if !p.conf.RTSPDisable &&
(p.conf.EncryptionParsed == conf.EncryptionStrict ||
p.conf.EncryptionParsed == conf.EncryptionOptional) {
2021-03-06 08:11:46 +00:00
if p.serverRTSPTLS == nil {
p.serverRTSPTLS, err = serverrtsp.New(
p.conf.ListenIP,
p.conf.RTSPSPort,
2021-03-06 08:11:46 +00:00
p.conf.ReadTimeout,
p.conf.WriteTimeout,
p.conf.ReadBufferCount,
p.conf.ReadBufferSize,
2021-03-06 08:11:46 +00:00
false,
0,
0,
true,
p.conf.ServerCert,
p.conf.ServerKey,
2021-01-06 11:40:18 +00:00
p)
2020-12-13 22:43:31 +00:00
if err != nil {
return err
}
}
}
if !p.conf.RTMPDisable {
2021-01-31 15:58:57 +00:00
if p.serverRTMP == nil {
p.serverRTMP, err = serverrtmp.New(
p.conf.ListenIP,
p.conf.RTMPPort,
p)
if err != nil {
return err
}
}
}
if p.pathMan == nil {
2021-01-06 11:40:18 +00:00
p.pathMan = pathman.New(
p.conf.RTSPPort,
2021-01-06 11:40:18 +00:00
p.conf.ReadTimeout,
p.conf.WriteTimeout,
2021-01-10 11:55:53 +00:00
p.conf.ReadBufferCount,
p.conf.ReadBufferSize,
2021-01-06 11:40:18 +00:00
p.conf.AuthMethodsParsed,
p.conf.Paths,
p.stats,
p)
}
if p.clientMan == nil {
2021-01-06 11:40:18 +00:00
p.clientMan = clientman.New(
p.conf.RTSPPort,
2021-01-06 11:40:18 +00:00
p.conf.ReadTimeout,
2021-03-10 14:06:45 +00:00
p.conf.WriteTimeout,
p.conf.ReadBufferCount,
2021-01-06 11:40:18 +00:00
p.conf.RunOnConnect,
p.conf.RunOnConnectRestart,
p.conf.ProtocolsParsed,
p.stats,
p.pathMan,
2021-03-06 08:11:46 +00:00
p.serverRTSPPlain,
p.serverRTSPTLS,
p.serverRTMP,
2021-01-06 11:40:18 +00:00
p)
}
return nil
}
2020-12-13 22:43:31 +00:00
func (p *program) closeResources(newConf *conf.Conf) {
closeLogger := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
!reflect.DeepEqual(newConf.LogDestinationsParsed, p.conf.LogDestinationsParsed) ||
newConf.LogFile != p.conf.LogFile {
closeLogger = true
}
closeMetrics := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
newConf.Metrics != p.conf.Metrics ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.MetricsPort != p.conf.MetricsPort {
closeMetrics = true
}
2021-03-27 11:23:19 +00:00
closePPROF := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
2021-03-27 11:23:19 +00:00
newConf.PPROF != p.conf.PPROF ||
newConf.ListenIP != p.conf.ListenIP ||
2021-03-27 11:23:19 +00:00
newConf.PPROFPort != p.conf.PPROFPort {
closePPROF = true
}
closeServerPlain := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
newConf.RTSPDisable != p.conf.RTSPDisable ||
2020-12-13 22:43:31 +00:00
newConf.EncryptionParsed != p.conf.EncryptionParsed ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.RTSPPort != p.conf.RTSPPort ||
2020-12-13 22:43:31 +00:00
newConf.ReadTimeout != p.conf.ReadTimeout ||
2021-01-06 11:40:18 +00:00
newConf.WriteTimeout != p.conf.WriteTimeout ||
2021-01-10 11:55:53 +00:00
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
2021-03-06 08:11:46 +00:00
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) ||
newConf.RTPPort != p.conf.RTPPort ||
newConf.RTCPPort != p.conf.RTCPPort {
closeServerPlain = true
}
2020-12-13 22:43:31 +00:00
closeServerTLS := false
if newConf == nil ||
newConf.RTSPDisable != p.conf.RTSPDisable ||
2020-12-13 22:43:31 +00:00
newConf.EncryptionParsed != p.conf.EncryptionParsed ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.RTSPSPort != p.conf.RTSPSPort ||
2020-12-13 22:43:31 +00:00
newConf.ReadTimeout != p.conf.ReadTimeout ||
2021-01-10 11:55:53 +00:00
newConf.WriteTimeout != p.conf.WriteTimeout ||
2021-03-06 08:11:46 +00:00
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
newConf.ServerCert != p.conf.ServerCert ||
newConf.ServerKey != p.conf.ServerKey {
2020-12-13 22:43:31 +00:00
closeServerTLS = true
}
closeServerRTMP := false
if newConf == nil ||
newConf.RTMPDisable != p.conf.RTMPDisable ||
newConf.ListenIP != p.conf.ListenIP ||
newConf.RTMPPort != p.conf.RTMPPort ||
newConf.ReadTimeout != p.conf.ReadTimeout {
closeServerRTMP = true
}
closePathMan := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
newConf.RTSPPort != p.conf.RTSPPort ||
2020-12-13 22:43:31 +00:00
newConf.ReadTimeout != p.conf.ReadTimeout ||
newConf.WriteTimeout != p.conf.WriteTimeout ||
2021-01-10 11:55:53 +00:00
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
newConf.ReadBufferSize != p.conf.ReadBufferSize ||
2020-12-13 22:43:31 +00:00
!reflect.DeepEqual(newConf.AuthMethodsParsed, p.conf.AuthMethodsParsed) {
closePathMan = true
2020-12-13 22:43:31 +00:00
} else if !reflect.DeepEqual(newConf.Paths, p.conf.Paths) {
p.pathMan.OnProgramConfReload(newConf.Paths)
}
closeClientMan := false
2020-12-13 22:43:31 +00:00
if newConf == nil ||
closeServerPlain ||
2020-12-13 22:43:31 +00:00
closeServerTLS ||
closeServerRTMP ||
closePathMan ||
newConf.RTSPPort != p.conf.RTSPPort ||
2020-12-13 22:43:31 +00:00
newConf.ReadTimeout != p.conf.ReadTimeout ||
2021-03-10 14:06:45 +00:00
newConf.WriteTimeout != p.conf.WriteTimeout ||
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
2020-12-13 22:43:31 +00:00
newConf.RunOnConnect != p.conf.RunOnConnect ||
newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart ||
!reflect.DeepEqual(newConf.ProtocolsParsed, p.conf.ProtocolsParsed) {
closeClientMan = true
}
2020-12-13 22:43:31 +00:00
closeStats := false
if newConf == nil {
closeStats = true
}
if closeClientMan && p.clientMan != nil {
p.clientMan.Close()
p.clientMan = nil
}
2020-12-13 22:43:31 +00:00
if closePathMan && p.pathMan != nil {
p.pathMan.Close()
p.pathMan = nil
}
if closeServerRTMP && p.serverRTMP != nil {
p.serverRTMP.Close()
p.serverRTMP = nil
}
2021-03-06 08:11:46 +00:00
if closeServerTLS && p.serverRTSPTLS != nil {
p.serverRTSPTLS.Close()
p.serverRTSPTLS = nil
}
2021-03-06 08:11:46 +00:00
if closeServerPlain && p.serverRTSPPlain != nil {
p.serverRTSPPlain.Close()
p.serverRTSPPlain = nil
}
2021-03-27 11:23:19 +00:00
if closePPROF && p.pprof != nil {
p.pprof.Close()
p.pprof = nil
}
if closeMetrics && p.metrics != nil {
p.metrics.Close()
p.metrics = nil
}
2020-12-13 22:43:31 +00:00
if closeLogger && p.logger != nil {
p.logger.Close()
p.logger = nil
}
2020-12-13 22:43:31 +00:00
if closeStats && p.stats != nil {
p.stats.Close()
}
}
func (p *program) reloadConf() error {
p.Log(logger.Info, "reloading configuration")
newConf, _, err := conf.Load(p.confPath)
if err != nil {
return err
}
p.closeResources(newConf)
p.conf = newConf
return p.createResources(false)
2020-06-27 11:38:35 +00:00
}
2019-12-28 21:07:03 +00:00
func main() {
p, ok := newProgram(os.Args[1:])
if !ok {
os.Exit(1)
2019-12-28 21:07:03 +00:00
}
<-p.done
2019-12-28 21:07:03 +00:00
}