2021-07-24 13:55:42 +00:00
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2021-11-10 18:04:07 +00:00
|
|
|
"os/signal"
|
2021-07-24 13:55:42 +00:00
|
|
|
"reflect"
|
|
|
|
|
2021-10-22 16:41:10 +00:00
|
|
|
"github.com/aler9/gortsplib"
|
2021-10-17 15:16:57 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
2021-07-24 13:55:42 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
|
|
|
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/conf"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/confwatcher"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/logger"
|
|
|
|
"github.com/aler9/rtsp-simple-server/internal/rlimit"
|
|
|
|
)
|
|
|
|
|
|
|
|
var version = "v0.0.0"
|
|
|
|
|
|
|
|
// Core is an instance of rtsp-simple-server.
|
|
|
|
type Core struct {
|
2021-07-04 16:13:49 +00:00
|
|
|
ctx context.Context
|
|
|
|
ctxCancel func()
|
|
|
|
confPath string
|
|
|
|
conf *conf.Conf
|
|
|
|
confFound bool
|
|
|
|
logger *logger.Logger
|
|
|
|
metrics *metrics
|
|
|
|
pprof *pprof
|
|
|
|
pathManager *pathManager
|
|
|
|
rtspServer *rtspServer
|
|
|
|
rtspsServer *rtspServer
|
|
|
|
rtmpServer *rtmpServer
|
|
|
|
hlsServer *hlsServer
|
|
|
|
api *api
|
|
|
|
confWatcher *confwatcher.ConfWatcher
|
|
|
|
|
|
|
|
// in
|
|
|
|
apiConfigSet chan *conf.Conf
|
2021-07-24 13:55:42 +00:00
|
|
|
|
|
|
|
// out
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New allocates a core.
|
|
|
|
func New(args []string) (*Core, bool) {
|
|
|
|
k := kingpin.New("rtsp-simple-server",
|
|
|
|
"rtsp-simple-server "+version+"\n\nRTSP server.")
|
|
|
|
|
|
|
|
argVersion := k.Flag("version", "print version").Bool()
|
2021-09-09 21:05:54 +00:00
|
|
|
argConfPath := k.Arg("confpath", "path to a config file. The default is rtsp-simple-server.yml.").
|
|
|
|
Default("rtsp-simple-server.yml").String()
|
2021-07-24 13:55:42 +00:00
|
|
|
|
|
|
|
kingpin.MustParse(k.Parse(args))
|
|
|
|
|
|
|
|
if *argVersion {
|
|
|
|
fmt.Println(version)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2021-10-17 15:16:57 +00:00
|
|
|
gin.SetMode(gin.ReleaseMode)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
ctx, ctxCancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
p := &Core{
|
2021-07-04 16:13:49 +00:00
|
|
|
ctx: ctx,
|
|
|
|
ctxCancel: ctxCancel,
|
|
|
|
confPath: *argConfPath,
|
|
|
|
apiConfigSet: make(chan *conf.Conf),
|
|
|
|
done: make(chan struct{}),
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
p.conf, p.confFound, err = conf.Load(p.confPath)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("ERR: %s\n", err)
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.createResources(true)
|
|
|
|
if err != nil {
|
2021-11-26 13:46:16 +00:00
|
|
|
if p.logger != nil {
|
|
|
|
p.Log(logger.Error, "%s", err)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("ERR: %s\n", err)
|
|
|
|
}
|
2021-09-06 16:39:15 +00:00
|
|
|
p.closeResources(nil, false)
|
2021-07-24 13:55:42 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.confFound {
|
|
|
|
p.confWatcher, err = confwatcher.New(p.confPath)
|
|
|
|
if err != nil {
|
2021-10-27 17:49:57 +00:00
|
|
|
p.Log(logger.Error, "%s", err)
|
2021-09-06 16:39:15 +00:00
|
|
|
p.closeResources(nil, false)
|
2021-07-24 13:55:42 +00:00
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go p.run()
|
|
|
|
|
|
|
|
return p, true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Core) close() {
|
|
|
|
p.ctxCancel()
|
|
|
|
<-p.done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait waits for the Core to exit.
|
|
|
|
func (p *Core) Wait() {
|
|
|
|
<-p.done
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log is the main logging function.
|
|
|
|
func (p *Core) Log(level logger.Level, format string, args ...interface{}) {
|
2021-10-28 15:33:43 +00:00
|
|
|
p.logger.Log(level, format, args...)
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Core) run() {
|
|
|
|
defer close(p.done)
|
|
|
|
|
|
|
|
confChanged := func() chan struct{} {
|
|
|
|
if p.confWatcher != nil {
|
|
|
|
return p.confWatcher.Watch()
|
|
|
|
}
|
|
|
|
return make(chan struct{})
|
|
|
|
}()
|
|
|
|
|
2021-11-10 18:04:07 +00:00
|
|
|
interrupt := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interrupt, os.Interrupt)
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-confChanged:
|
2021-07-04 16:13:49 +00:00
|
|
|
p.Log(logger.Info, "reloading configuration (file changed)")
|
|
|
|
|
|
|
|
newConf, _, err := conf.Load(p.confPath)
|
|
|
|
if err != nil {
|
2021-10-27 17:49:57 +00:00
|
|
|
p.Log(logger.Error, "%s", err)
|
2021-07-04 16:13:49 +00:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:39:15 +00:00
|
|
|
err = p.reloadConf(newConf, false)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
2021-10-27 17:49:57 +00:00
|
|
|
p.Log(logger.Error, "%s", err)
|
2021-07-04 16:13:49 +00:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
case newConf := <-p.apiConfigSet:
|
|
|
|
p.Log(logger.Info, "reloading configuration (API request)")
|
|
|
|
|
2021-09-06 16:39:15 +00:00
|
|
|
err := p.reloadConf(newConf, true)
|
2021-07-24 13:55:42 +00:00
|
|
|
if err != nil {
|
2021-10-27 17:49:57 +00:00
|
|
|
p.Log(logger.Error, "%s", err)
|
2021-07-24 13:55:42 +00:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
2021-11-10 18:04:07 +00:00
|
|
|
case <-interrupt:
|
|
|
|
p.Log(logger.Info, "shutting down gracefully")
|
|
|
|
break outer
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
case <-p.ctx.Done():
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.ctxCancel()
|
|
|
|
|
2021-09-06 16:39:15 +00:00
|
|
|
p.closeResources(nil, false)
|
2021-07-24 13:55:42 +00:00
|
|
|
|
|
|
|
if p.confWatcher != nil {
|
|
|
|
p.confWatcher.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Core) createResources(initial bool) error {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if p.logger == nil {
|
|
|
|
p.logger, err = logger.New(
|
2021-09-27 08:36:28 +00:00
|
|
|
logger.Level(p.conf.LogLevel),
|
|
|
|
p.conf.LogDestinations,
|
2021-07-24 13:55:42 +00:00
|
|
|
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 = newMetrics(
|
|
|
|
p.conf.MetricsAddress,
|
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.conf.PPROF {
|
|
|
|
if p.pprof == nil {
|
|
|
|
p.pprof, err = newPPROF(
|
|
|
|
p.conf.PPROFAddress,
|
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-30 12:49:09 +00:00
|
|
|
if p.pathManager == nil {
|
|
|
|
p.pathManager = newPathManager(
|
2021-07-24 13:55:42 +00:00
|
|
|
p.ctx,
|
|
|
|
p.conf.RTSPAddress,
|
|
|
|
p.conf.ReadTimeout,
|
|
|
|
p.conf.WriteTimeout,
|
|
|
|
p.conf.ReadBufferCount,
|
|
|
|
p.conf.ReadBufferSize,
|
|
|
|
p.conf.Paths,
|
2021-08-12 09:48:47 +00:00
|
|
|
p.metrics,
|
2021-07-24 13:55:42 +00:00
|
|
|
p)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.conf.RTSPDisable &&
|
2021-09-27 08:36:28 +00:00
|
|
|
(p.conf.Encryption == conf.EncryptionNo ||
|
|
|
|
p.conf.Encryption == conf.EncryptionOptional) {
|
2021-07-04 16:13:49 +00:00
|
|
|
if p.rtspServer == nil {
|
2021-10-22 16:41:10 +00:00
|
|
|
_, useUDP := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDP)]
|
|
|
|
_, useMulticast := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDPMulticast)]
|
2021-07-04 16:13:49 +00:00
|
|
|
p.rtspServer, err = newRTSPServer(
|
2021-07-24 13:55:42 +00:00
|
|
|
p.ctx,
|
|
|
|
p.conf.RTSPAddress,
|
2021-09-27 08:36:28 +00:00
|
|
|
p.conf.AuthMethods,
|
2021-07-24 13:55:42 +00:00
|
|
|
p.conf.ReadTimeout,
|
|
|
|
p.conf.WriteTimeout,
|
|
|
|
p.conf.ReadBufferCount,
|
|
|
|
p.conf.ReadBufferSize,
|
|
|
|
useUDP,
|
|
|
|
useMulticast,
|
|
|
|
p.conf.RTPAddress,
|
|
|
|
p.conf.RTCPAddress,
|
|
|
|
p.conf.MulticastIPRange,
|
|
|
|
p.conf.MulticastRTPPort,
|
|
|
|
p.conf.MulticastRTCPPort,
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
p.conf.RTSPAddress,
|
2021-09-27 08:36:28 +00:00
|
|
|
p.conf.Protocols,
|
2021-07-24 13:55:42 +00:00
|
|
|
p.conf.RunOnConnect,
|
|
|
|
p.conf.RunOnConnectRestart,
|
2021-08-12 09:48:47 +00:00
|
|
|
p.metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
p.pathManager,
|
2021-07-24 13:55:42 +00:00
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.conf.RTSPDisable &&
|
2021-09-27 08:36:28 +00:00
|
|
|
(p.conf.Encryption == conf.EncryptionStrict ||
|
|
|
|
p.conf.Encryption == conf.EncryptionOptional) {
|
2021-07-04 16:13:49 +00:00
|
|
|
if p.rtspsServer == nil {
|
|
|
|
p.rtspsServer, err = newRTSPServer(
|
2021-07-24 13:55:42 +00:00
|
|
|
p.ctx,
|
|
|
|
p.conf.RTSPSAddress,
|
2021-09-27 08:36:28 +00:00
|
|
|
p.conf.AuthMethods,
|
2021-07-24 13:55:42 +00:00
|
|
|
p.conf.ReadTimeout,
|
|
|
|
p.conf.WriteTimeout,
|
|
|
|
p.conf.ReadBufferCount,
|
|
|
|
p.conf.ReadBufferSize,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
"",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
true,
|
|
|
|
p.conf.ServerCert,
|
|
|
|
p.conf.ServerKey,
|
|
|
|
p.conf.RTSPAddress,
|
2021-09-27 08:36:28 +00:00
|
|
|
p.conf.Protocols,
|
2021-07-24 13:55:42 +00:00
|
|
|
p.conf.RunOnConnect,
|
|
|
|
p.conf.RunOnConnectRestart,
|
2021-08-12 09:48:47 +00:00
|
|
|
p.metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
p.pathManager,
|
2021-07-24 13:55:42 +00:00
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.conf.RTMPDisable {
|
|
|
|
if p.rtmpServer == nil {
|
|
|
|
p.rtmpServer, err = newRTMPServer(
|
|
|
|
p.ctx,
|
|
|
|
p.conf.RTMPAddress,
|
|
|
|
p.conf.ReadTimeout,
|
|
|
|
p.conf.WriteTimeout,
|
|
|
|
p.conf.ReadBufferCount,
|
|
|
|
p.conf.RTSPAddress,
|
|
|
|
p.conf.RunOnConnect,
|
|
|
|
p.conf.RunOnConnectRestart,
|
2021-08-12 09:48:47 +00:00
|
|
|
p.metrics,
|
2021-07-30 12:49:09 +00:00
|
|
|
p.pathManager,
|
2021-07-24 13:55:42 +00:00
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !p.conf.HLSDisable {
|
|
|
|
if p.hlsServer == nil {
|
|
|
|
p.hlsServer, err = newHLSServer(
|
|
|
|
p.ctx,
|
|
|
|
p.conf.HLSAddress,
|
2021-07-29 14:56:40 +00:00
|
|
|
p.conf.HLSAlwaysRemux,
|
2021-07-24 13:55:42 +00:00
|
|
|
p.conf.HLSSegmentCount,
|
|
|
|
p.conf.HLSSegmentDuration,
|
|
|
|
p.conf.HLSAllowOrigin,
|
|
|
|
p.conf.ReadBufferCount,
|
2021-07-30 12:49:09 +00:00
|
|
|
p.pathManager,
|
2021-11-05 16:29:13 +00:00
|
|
|
p.metrics,
|
2021-07-24 13:55:42 +00:00
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
if p.conf.API {
|
|
|
|
if p.api == nil {
|
|
|
|
p.api, err = newAPI(
|
|
|
|
p.conf.APIAddress,
|
|
|
|
p.conf,
|
|
|
|
p.pathManager,
|
|
|
|
p.rtspServer,
|
|
|
|
p.rtspsServer,
|
|
|
|
p.rtmpServer,
|
2021-11-05 16:14:31 +00:00
|
|
|
p.hlsServer,
|
2021-07-04 16:13:49 +00:00
|
|
|
p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-24 13:55:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:39:15 +00:00
|
|
|
func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
|
2021-07-24 13:55:42 +00:00
|
|
|
closeLogger := false
|
|
|
|
if newConf == nil ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.LogDestinations, p.conf.LogDestinations) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.LogFile != p.conf.LogFile {
|
|
|
|
closeLogger = true
|
|
|
|
}
|
|
|
|
|
|
|
|
closeMetrics := false
|
|
|
|
if newConf == nil ||
|
|
|
|
newConf.Metrics != p.conf.Metrics ||
|
2021-08-12 09:48:47 +00:00
|
|
|
newConf.MetricsAddress != p.conf.MetricsAddress {
|
2021-07-24 13:55:42 +00:00
|
|
|
closeMetrics = true
|
|
|
|
}
|
|
|
|
|
|
|
|
closePPROF := false
|
|
|
|
if newConf == nil ||
|
|
|
|
newConf.PPROF != p.conf.PPROF ||
|
2021-08-12 09:48:47 +00:00
|
|
|
newConf.PPROFAddress != p.conf.PPROFAddress {
|
2021-07-24 13:55:42 +00:00
|
|
|
closePPROF = true
|
|
|
|
}
|
|
|
|
|
2021-07-31 15:45:15 +00:00
|
|
|
closePathManager := false
|
2021-07-24 13:55:42 +00:00
|
|
|
if newConf == nil ||
|
|
|
|
newConf.RTSPAddress != p.conf.RTSPAddress ||
|
|
|
|
newConf.ReadTimeout != p.conf.ReadTimeout ||
|
|
|
|
newConf.WriteTimeout != p.conf.WriteTimeout ||
|
|
|
|
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
|
|
|
|
newConf.ReadBufferSize != p.conf.ReadBufferSize ||
|
2021-08-12 09:48:47 +00:00
|
|
|
closeMetrics {
|
2021-07-31 15:45:15 +00:00
|
|
|
closePathManager = true
|
2021-07-24 13:55:42 +00:00
|
|
|
} else if !reflect.DeepEqual(newConf.Paths, p.conf.Paths) {
|
2021-10-27 19:01:00 +00:00
|
|
|
p.pathManager.onConfReload(newConf.Paths)
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTSPServer := false
|
2021-07-24 13:55:42 +00:00
|
|
|
if newConf == nil ||
|
|
|
|
newConf.RTSPDisable != p.conf.RTSPDisable ||
|
2021-09-27 08:36:28 +00:00
|
|
|
newConf.Encryption != p.conf.Encryption ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.RTSPAddress != p.conf.RTSPAddress ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.ReadTimeout != p.conf.ReadTimeout ||
|
|
|
|
newConf.WriteTimeout != p.conf.WriteTimeout ||
|
|
|
|
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.RTPAddress != p.conf.RTPAddress ||
|
|
|
|
newConf.RTCPAddress != p.conf.RTCPAddress ||
|
|
|
|
newConf.MulticastIPRange != p.conf.MulticastIPRange ||
|
|
|
|
newConf.MulticastRTPPort != p.conf.MulticastRTPPort ||
|
|
|
|
newConf.MulticastRTCPPort != p.conf.MulticastRTCPPort ||
|
|
|
|
newConf.RTSPAddress != p.conf.RTSPAddress ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.RunOnConnect != p.conf.RunOnConnect ||
|
|
|
|
newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart ||
|
2021-08-12 09:48:47 +00:00
|
|
|
closeMetrics ||
|
2021-07-31 15:45:15 +00:00
|
|
|
closePathManager {
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTSPServer = true
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTSPSServer := false
|
2021-07-24 13:55:42 +00:00
|
|
|
if newConf == nil ||
|
|
|
|
newConf.RTSPDisable != p.conf.RTSPDisable ||
|
2021-09-27 08:36:28 +00:00
|
|
|
newConf.Encryption != p.conf.Encryption ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.RTSPSAddress != p.conf.RTSPSAddress ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.ReadTimeout != p.conf.ReadTimeout ||
|
|
|
|
newConf.WriteTimeout != p.conf.WriteTimeout ||
|
|
|
|
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
|
|
|
|
newConf.ServerCert != p.conf.ServerCert ||
|
|
|
|
newConf.ServerKey != p.conf.ServerKey ||
|
|
|
|
newConf.RTSPAddress != p.conf.RTSPAddress ||
|
2021-09-27 08:36:28 +00:00
|
|
|
!reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.RunOnConnect != p.conf.RunOnConnect ||
|
|
|
|
newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart ||
|
2021-08-12 09:48:47 +00:00
|
|
|
closeMetrics ||
|
2021-07-31 15:45:15 +00:00
|
|
|
closePathManager {
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTSPSServer = true
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTMPServer := false
|
2021-07-24 13:55:42 +00:00
|
|
|
if newConf == nil ||
|
|
|
|
newConf.RTMPDisable != p.conf.RTMPDisable ||
|
|
|
|
newConf.RTMPAddress != p.conf.RTMPAddress ||
|
|
|
|
newConf.ReadTimeout != p.conf.ReadTimeout ||
|
|
|
|
newConf.WriteTimeout != p.conf.WriteTimeout ||
|
|
|
|
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
|
|
|
|
newConf.RTSPAddress != p.conf.RTSPAddress ||
|
|
|
|
newConf.RunOnConnect != p.conf.RunOnConnect ||
|
|
|
|
newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart ||
|
2021-08-12 09:48:47 +00:00
|
|
|
closeMetrics ||
|
2021-07-31 15:45:15 +00:00
|
|
|
closePathManager {
|
2021-07-04 16:13:49 +00:00
|
|
|
closeRTMPServer = true
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
closeHLSServer := false
|
2021-07-24 13:55:42 +00:00
|
|
|
if newConf == nil ||
|
|
|
|
newConf.HLSDisable != p.conf.HLSDisable ||
|
|
|
|
newConf.HLSAddress != p.conf.HLSAddress ||
|
2021-07-29 14:56:40 +00:00
|
|
|
newConf.HLSAlwaysRemux != p.conf.HLSAlwaysRemux ||
|
2021-07-24 13:55:42 +00:00
|
|
|
newConf.HLSSegmentCount != p.conf.HLSSegmentCount ||
|
|
|
|
newConf.HLSSegmentDuration != p.conf.HLSSegmentDuration ||
|
|
|
|
newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin ||
|
|
|
|
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
|
2021-11-05 16:29:13 +00:00
|
|
|
closePathManager ||
|
|
|
|
closeMetrics {
|
2021-07-04 16:13:49 +00:00
|
|
|
closeHLSServer = true
|
|
|
|
}
|
|
|
|
|
|
|
|
closeAPI := false
|
|
|
|
if newConf == nil ||
|
|
|
|
newConf.API != p.conf.API ||
|
|
|
|
newConf.APIAddress != p.conf.APIAddress ||
|
|
|
|
closePathManager ||
|
|
|
|
closeRTSPServer ||
|
|
|
|
closeRTSPSServer ||
|
2021-11-05 16:14:31 +00:00
|
|
|
closeRTMPServer ||
|
|
|
|
closeHLSServer {
|
2021-07-04 16:13:49 +00:00
|
|
|
closeAPI = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if p.api != nil {
|
|
|
|
if closeAPI {
|
|
|
|
p.api.close()
|
|
|
|
p.api = nil
|
2021-09-06 16:39:15 +00:00
|
|
|
} else if !calledByAPI { // avoid a loop
|
2021-10-27 19:01:00 +00:00
|
|
|
p.api.onConfReload(newConf)
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
if closeRTSPSServer && p.rtspsServer != nil {
|
|
|
|
p.rtspsServer.close()
|
|
|
|
p.rtspsServer = nil
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
if closeRTSPServer && p.rtspServer != nil {
|
|
|
|
p.rtspServer.close()
|
|
|
|
p.rtspServer = nil
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 15:45:15 +00:00
|
|
|
if closePathManager && p.pathManager != nil {
|
2021-07-30 12:49:09 +00:00
|
|
|
p.pathManager.close()
|
|
|
|
p.pathManager = nil
|
2021-07-24 13:55:42 +00:00
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
if closeHLSServer && p.hlsServer != nil {
|
2021-07-24 13:55:42 +00:00
|
|
|
p.hlsServer.close()
|
|
|
|
p.hlsServer = nil
|
|
|
|
}
|
|
|
|
|
2021-07-04 16:13:49 +00:00
|
|
|
if closeRTMPServer && p.rtmpServer != nil {
|
2021-07-24 13:55:42 +00:00
|
|
|
p.rtmpServer.close()
|
|
|
|
p.rtmpServer = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if closePPROF && p.pprof != nil {
|
|
|
|
p.pprof.close()
|
|
|
|
p.pprof = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if closeMetrics && p.metrics != nil {
|
|
|
|
p.metrics.close()
|
|
|
|
p.metrics = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if closeLogger && p.logger != nil {
|
|
|
|
p.logger.Close()
|
|
|
|
p.logger = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-06 16:39:15 +00:00
|
|
|
func (p *Core) reloadConf(newConf *conf.Conf, calledByAPI bool) error {
|
|
|
|
p.closeResources(newConf, calledByAPI)
|
2021-07-24 13:55:42 +00:00
|
|
|
|
|
|
|
p.conf = newConf
|
|
|
|
return p.createResources(false)
|
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-10-27 19:01:00 +00:00
|
|
|
// onAPIConfigSet is called by api.
|
|
|
|
func (p *Core) onAPIConfigSet(conf *conf.Conf) {
|
2021-07-04 16:13:49 +00:00
|
|
|
select {
|
|
|
|
case p.apiConfigSet <- conf:
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
}
|
|
|
|
}
|