// Package core contains the main struct of the software. package core import ( "context" "fmt" "os" "os/signal" "path/filepath" "reflect" "sort" "strings" "time" "github.com/alecthomas/kong" "github.com/bluenviron/gortsplib/v4" "github.com/gin-gonic/gin" "github.com/bluenviron/mediamtx/internal/api" "github.com/bluenviron/mediamtx/internal/conf" "github.com/bluenviron/mediamtx/internal/confwatcher" "github.com/bluenviron/mediamtx/internal/externalcmd" "github.com/bluenviron/mediamtx/internal/logger" "github.com/bluenviron/mediamtx/internal/metrics" "github.com/bluenviron/mediamtx/internal/pprof" "github.com/bluenviron/mediamtx/internal/record" "github.com/bluenviron/mediamtx/internal/rlimit" "github.com/bluenviron/mediamtx/internal/servers/hls" "github.com/bluenviron/mediamtx/internal/servers/rtmp" "github.com/bluenviron/mediamtx/internal/servers/rtsp" "github.com/bluenviron/mediamtx/internal/servers/srt" "github.com/bluenviron/mediamtx/internal/servers/webrtc" ) var version = "v0.0.0" var defaultConfPaths = []string{ "rtsp-simple-server.yml", "mediamtx.yml", "/usr/local/etc/mediamtx.yml", "/usr/etc/mediamtx.yml", "/etc/mediamtx/mediamtx.yml", } func gatherCleanerEntries(paths map[string]*conf.Path) []record.CleanerEntry { out := make(map[record.CleanerEntry]struct{}) for _, pa := range paths { if pa.Record && pa.RecordDeleteAfter != 0 { entry := record.CleanerEntry{ PathFormat: pa.RecordPath, Format: pa.RecordFormat, DeleteAfter: time.Duration(pa.RecordDeleteAfter), } out[entry] = struct{}{} } } out2 := make([]record.CleanerEntry, len(out)) i := 0 for v := range out { out2[i] = v i++ } sort.Slice(out2, func(i, j int) bool { if out2[i].PathFormat != out2[j].PathFormat { return out2[i].PathFormat < out2[j].PathFormat } return out2[i].DeleteAfter < out2[j].DeleteAfter }) return out2 } var cli struct { Version bool `help:"print version"` Confpath string `arg:"" default:""` } // Core is an instance of MediaMTX. type Core struct { ctx context.Context ctxCancel func() confPath string conf *conf.Conf logger *logger.Logger externalCmdPool *externalcmd.Pool metrics *metrics.Metrics pprof *pprof.PPROF recordCleaner *record.Cleaner pathManager *pathManager rtspServer *rtsp.Server rtspsServer *rtsp.Server rtmpServer *rtmp.Server rtmpsServer *rtmp.Server hlsServer *hls.Server webRTCServer *webrtc.Server srtServer *srt.Server api *api.API confWatcher *confwatcher.ConfWatcher // in chAPIConfigSet chan *conf.Conf // out done chan struct{} } // New allocates a Core. func New(args []string) (*Core, bool) { parser, err := kong.New(&cli, kong.Description("MediaMTX "+version), kong.UsageOnError(), kong.ValueFormatter(func(value *kong.Value) string { switch value.Name { case "confpath": return "path to a config file. The default is mediamtx.yml." default: return kong.DefaultHelpValueFormatter(value) } })) if err != nil { panic(err) } _, err = parser.Parse(args) parser.FatalIfErrorf(err) if cli.Version { fmt.Println(version) os.Exit(0) } ctx, ctxCancel := context.WithCancel(context.Background()) p := &Core{ ctx: ctx, ctxCancel: ctxCancel, chAPIConfigSet: make(chan *conf.Conf), done: make(chan struct{}), } p.conf, p.confPath, err = conf.Load(cli.Confpath, defaultConfPaths) if err != nil { fmt.Printf("ERR: %s\n", err) return nil, false } err = p.createResources(true) if err != nil { if p.logger != nil { p.Log(logger.Error, "%s", err) } else { fmt.Printf("ERR: %s\n", err) } p.closeResources(nil, false) return nil, false } go p.run() return p, true } // Close closes Core and waits for all goroutines to return. func (p *Core) Close() { p.ctxCancel() <-p.done } // Wait waits for the Core to exit. func (p *Core) Wait() { <-p.done } // Log implements logger.Writer. func (p *Core) Log(level logger.Level, format string, args ...interface{}) { p.logger.Log(level, format, args...) } func (p *Core) run() { defer close(p.done) confChanged := func() chan struct{} { if p.confWatcher != nil { return p.confWatcher.Watch() } return make(chan struct{}) }() interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) outer: for { select { case <-confChanged: p.Log(logger.Info, "reloading configuration (file changed)") newConf, _, err := conf.Load(p.confPath, nil) if err != nil { p.Log(logger.Error, "%s", err) break outer } err = p.reloadConf(newConf, false) if err != nil { p.Log(logger.Error, "%s", err) break outer } case newConf := <-p.chAPIConfigSet: p.Log(logger.Info, "reloading configuration (API request)") err := p.reloadConf(newConf, true) if err != nil { p.Log(logger.Error, "%s", err) break outer } case <-interrupt: p.Log(logger.Info, "shutting down gracefully") break outer case <-p.ctx.Done(): break outer } } p.ctxCancel() p.closeResources(nil, false) } func (p *Core) createResources(initial bool) error { var err error if p.logger == nil { p.logger, err = logger.New( logger.Level(p.conf.LogLevel), p.conf.LogDestinations, p.conf.LogFile, ) if err != nil { return err } } if initial { p.Log(logger.Info, "MediaMTX %s", version) if p.confPath != "" { a, _ := filepath.Abs(p.confPath) p.Log(logger.Info, "configuration loaded from %s", a) } else { list := make([]string, len(defaultConfPaths)) for i, pa := range defaultConfPaths { a, _ := filepath.Abs(pa) list[i] = a } p.Log(logger.Warn, "configuration file not found (looked in %s), using an empty configuration", strings.Join(list, ", ")) } // on Linux, try to raise the number of file descriptors that can be opened // to allow the maximum possible number of clients. rlimit.Raise() //nolint:errcheck gin.SetMode(gin.ReleaseMode) p.externalCmdPool = externalcmd.NewPool() } if p.conf.Metrics && p.metrics == nil { p.metrics = &metrics.Metrics{ Address: p.conf.MetricsAddress, ReadTimeout: p.conf.ReadTimeout, Parent: p, } err := p.metrics.Initialize() if err != nil { return err } } if p.conf.PPROF && p.pprof == nil { p.pprof = &pprof.PPROF{ Address: p.conf.PPROFAddress, ReadTimeout: p.conf.ReadTimeout, Parent: p, } err := p.pprof.Initialize() if err != nil { return err } } cleanerEntries := gatherCleanerEntries(p.conf.Paths) if len(cleanerEntries) != 0 && p.recordCleaner == nil { p.recordCleaner = &record.Cleaner{ Entries: cleanerEntries, Parent: p, } p.recordCleaner.Initialize() } if p.pathManager == nil { p.pathManager = newPathManager( p.conf.LogLevel, p.conf.ExternalAuthenticationURL, p.conf.RTSPAddress, p.conf.AuthMethods, p.conf.ReadTimeout, p.conf.WriteTimeout, p.conf.WriteQueueSize, p.conf.UDPMaxPayloadSize, p.conf.Paths, p.externalCmdPool, p, ) if p.metrics != nil { p.metrics.SetPathManager(p.pathManager) } } if p.conf.RTSP && (p.conf.Encryption == conf.EncryptionNo || p.conf.Encryption == conf.EncryptionOptional) && p.rtspServer == nil { _, useUDP := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDP)] _, useMulticast := p.conf.Protocols[conf.Protocol(gortsplib.TransportUDPMulticast)] p.rtspServer = &rtsp.Server{ Address: p.conf.RTSPAddress, AuthMethods: p.conf.AuthMethods, ReadTimeout: p.conf.ReadTimeout, WriteTimeout: p.conf.WriteTimeout, WriteQueueSize: p.conf.WriteQueueSize, UseUDP: useUDP, UseMulticast: useMulticast, RTPAddress: p.conf.RTPAddress, RTCPAddress: p.conf.RTCPAddress, MulticastIPRange: p.conf.MulticastIPRange, MulticastRTPPort: p.conf.MulticastRTPPort, MulticastRTCPPort: p.conf.MulticastRTCPPort, IsTLS: false, ServerCert: "", ServerKey: "", RTSPAddress: p.conf.RTSPAddress, Protocols: p.conf.Protocols, RunOnConnect: p.conf.RunOnConnect, RunOnConnectRestart: p.conf.RunOnConnectRestart, RunOnDisconnect: p.conf.RunOnDisconnect, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.rtspServer.Initialize() if err != nil { return err } if p.metrics != nil { p.metrics.SetRTSPServer(p.rtspServer) } } if p.conf.RTSP && (p.conf.Encryption == conf.EncryptionStrict || p.conf.Encryption == conf.EncryptionOptional) && p.rtspsServer == nil { p.rtspsServer = &rtsp.Server{ Address: p.conf.RTSPSAddress, AuthMethods: p.conf.AuthMethods, ReadTimeout: p.conf.ReadTimeout, WriteTimeout: p.conf.WriteTimeout, WriteQueueSize: p.conf.WriteQueueSize, UseUDP: false, UseMulticast: false, RTPAddress: "", RTCPAddress: "", MulticastIPRange: "", MulticastRTPPort: 0, MulticastRTCPPort: 0, IsTLS: true, ServerCert: p.conf.ServerCert, ServerKey: p.conf.ServerKey, RTSPAddress: p.conf.RTSPAddress, Protocols: p.conf.Protocols, RunOnConnect: p.conf.RunOnConnect, RunOnConnectRestart: p.conf.RunOnConnectRestart, RunOnDisconnect: p.conf.RunOnDisconnect, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.rtspsServer.Initialize() if err != nil { return err } if p.metrics != nil { p.metrics.SetRTSPSServer(p.rtspsServer) } } if p.conf.RTMP && (p.conf.RTMPEncryption == conf.EncryptionNo || p.conf.RTMPEncryption == conf.EncryptionOptional) && p.rtmpServer == nil { p.rtmpServer = &rtmp.Server{ Address: p.conf.RTMPAddress, ReadTimeout: p.conf.ReadTimeout, WriteTimeout: p.conf.WriteTimeout, WriteQueueSize: p.conf.WriteQueueSize, IsTLS: false, ServerCert: "", ServerKey: "", RTSPAddress: p.conf.RTSPAddress, RunOnConnect: p.conf.RunOnConnect, RunOnConnectRestart: p.conf.RunOnConnectRestart, RunOnDisconnect: p.conf.RunOnDisconnect, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.rtmpServer.Initialize() if err != nil { return err } if p.metrics != nil { p.metrics.SetRTMPServer(p.rtmpServer) } } if p.conf.RTMP && (p.conf.RTMPEncryption == conf.EncryptionStrict || p.conf.RTMPEncryption == conf.EncryptionOptional) && p.rtmpsServer == nil { p.rtmpsServer = &rtmp.Server{ Address: p.conf.RTMPSAddress, ReadTimeout: p.conf.ReadTimeout, WriteTimeout: p.conf.WriteTimeout, WriteQueueSize: p.conf.WriteQueueSize, IsTLS: true, ServerCert: p.conf.RTMPServerCert, ServerKey: p.conf.RTMPServerKey, RTSPAddress: p.conf.RTSPAddress, RunOnConnect: p.conf.RunOnConnect, RunOnConnectRestart: p.conf.RunOnConnectRestart, RunOnDisconnect: p.conf.RunOnDisconnect, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.rtmpsServer.Initialize() if err != nil { return err } if p.metrics != nil { p.metrics.SetRTMPSServer(p.rtmpsServer) } } if p.conf.HLS && p.hlsServer == nil { p.hlsServer = &hls.Server{ Address: p.conf.HLSAddress, Encryption: p.conf.HLSEncryption, ServerKey: p.conf.HLSServerKey, ServerCert: p.conf.HLSServerCert, ExternalAuthenticationURL: p.conf.ExternalAuthenticationURL, AlwaysRemux: p.conf.HLSAlwaysRemux, Variant: p.conf.HLSVariant, SegmentCount: p.conf.HLSSegmentCount, SegmentDuration: p.conf.HLSSegmentDuration, PartDuration: p.conf.HLSPartDuration, SegmentMaxSize: p.conf.HLSSegmentMaxSize, AllowOrigin: p.conf.HLSAllowOrigin, TrustedProxies: p.conf.HLSTrustedProxies, Directory: p.conf.HLSDirectory, ReadTimeout: p.conf.ReadTimeout, WriteQueueSize: p.conf.WriteQueueSize, PathManager: p.pathManager, Parent: p, } err := p.hlsServer.Initialize() if err != nil { return err } p.pathManager.setHLSServer(p.hlsServer) if p.metrics != nil { p.metrics.SetHLSServer(p.hlsServer) } } if p.conf.WebRTC && p.webRTCServer == nil { p.webRTCServer = &webrtc.Server{ Address: p.conf.WebRTCAddress, Encryption: p.conf.WebRTCEncryption, ServerKey: p.conf.WebRTCServerKey, ServerCert: p.conf.WebRTCServerCert, AllowOrigin: p.conf.WebRTCAllowOrigin, TrustedProxies: p.conf.WebRTCTrustedProxies, ReadTimeout: p.conf.ReadTimeout, WriteQueueSize: p.conf.WriteQueueSize, LocalUDPAddress: p.conf.WebRTCLocalUDPAddress, LocalTCPAddress: p.conf.WebRTCLocalTCPAddress, IPsFromInterfaces: p.conf.WebRTCIPsFromInterfaces, IPsFromInterfacesList: p.conf.WebRTCIPsFromInterfacesList, AdditionalHosts: p.conf.WebRTCAdditionalHosts, ICEServers: p.conf.WebRTCICEServers2, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.webRTCServer.Initialize() if err != nil { p.webRTCServer = nil return err } if p.metrics != nil { p.metrics.SetWebRTCServer(p.webRTCServer) } } if p.conf.SRT && p.srtServer == nil { p.srtServer = &srt.Server{ Address: p.conf.SRTAddress, RTSPAddress: p.conf.RTSPAddress, ReadTimeout: p.conf.ReadTimeout, WriteTimeout: p.conf.WriteTimeout, WriteQueueSize: p.conf.WriteQueueSize, UDPMaxPayloadSize: p.conf.UDPMaxPayloadSize, RunOnConnect: p.conf.RunOnConnect, RunOnConnectRestart: p.conf.RunOnConnectRestart, RunOnDisconnect: p.conf.RunOnDisconnect, ExternalCmdPool: p.externalCmdPool, PathManager: p.pathManager, Parent: p, } err := p.srtServer.Initialize() if err != nil { return err } if p.metrics != nil { p.metrics.SetSRTServer(p.srtServer) } } if p.conf.API && p.api == nil { p.api = &api.API{ Address: p.conf.APIAddress, ReadTimeout: p.conf.ReadTimeout, Conf: p.conf, PathManager: p.pathManager, RTSPServer: p.rtspServer, RTSPSServer: p.rtspsServer, RTMPServer: p.rtmpServer, RTMPSServer: p.rtmpsServer, HLSServer: p.hlsServer, WebRTCServer: p.webRTCServer, SRTServer: p.srtServer, Parent: p, } err := p.api.Initialize() if err != nil { return err } } if initial && p.confPath != "" { p.confWatcher, err = confwatcher.New(p.confPath) if err != nil { return err } } return nil } func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) { closeLogger := newConf == nil || newConf.LogLevel != p.conf.LogLevel || !reflect.DeepEqual(newConf.LogDestinations, p.conf.LogDestinations) || newConf.LogFile != p.conf.LogFile closeMetrics := newConf == nil || newConf.Metrics != p.conf.Metrics || newConf.MetricsAddress != p.conf.MetricsAddress || newConf.ReadTimeout != p.conf.ReadTimeout || closeLogger closePPROF := newConf == nil || newConf.PPROF != p.conf.PPROF || newConf.PPROFAddress != p.conf.PPROFAddress || newConf.ReadTimeout != p.conf.ReadTimeout || closeLogger closeRecorderCleaner := newConf == nil || !reflect.DeepEqual(gatherCleanerEntries(newConf.Paths), gatherCleanerEntries(p.conf.Paths)) || closeLogger closePathManager := newConf == nil || newConf.LogLevel != p.conf.LogLevel || newConf.ExternalAuthenticationURL != p.conf.ExternalAuthenticationURL || newConf.RTSPAddress != p.conf.RTSPAddress || !reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.UDPMaxPayloadSize != p.conf.UDPMaxPayloadSize || closeMetrics || closeLogger if !closePathManager && !reflect.DeepEqual(newConf.Paths, p.conf.Paths) { p.pathManager.ReloadConf(newConf.Paths) } closeRTSPServer := newConf == nil || newConf.RTSP != p.conf.RTSP || newConf.Encryption != p.conf.Encryption || newConf.RTSPAddress != p.conf.RTSPAddress || !reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || !reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) || 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 || !reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) || newConf.RunOnConnect != p.conf.RunOnConnect || newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart || newConf.RunOnDisconnect != p.conf.RunOnDisconnect || closeMetrics || closePathManager || closeLogger closeRTSPSServer := newConf == nil || newConf.RTSP != p.conf.RTSP || newConf.Encryption != p.conf.Encryption || newConf.RTSPSAddress != p.conf.RTSPSAddress || !reflect.DeepEqual(newConf.AuthMethods, p.conf.AuthMethods) || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.ServerCert != p.conf.ServerCert || newConf.ServerKey != p.conf.ServerKey || newConf.RTSPAddress != p.conf.RTSPAddress || !reflect.DeepEqual(newConf.Protocols, p.conf.Protocols) || newConf.RunOnConnect != p.conf.RunOnConnect || newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart || newConf.RunOnDisconnect != p.conf.RunOnDisconnect || closeMetrics || closePathManager || closeLogger closeRTMPServer := newConf == nil || newConf.RTMP != p.conf.RTMP || newConf.RTMPEncryption != p.conf.RTMPEncryption || newConf.RTMPAddress != p.conf.RTMPAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.RTSPAddress != p.conf.RTSPAddress || newConf.RunOnConnect != p.conf.RunOnConnect || newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart || newConf.RunOnDisconnect != p.conf.RunOnDisconnect || closeMetrics || closePathManager || closeLogger closeRTMPSServer := newConf == nil || newConf.RTMP != p.conf.RTMP || newConf.RTMPEncryption != p.conf.RTMPEncryption || newConf.RTMPSAddress != p.conf.RTMPSAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.RTMPServerCert != p.conf.RTMPServerCert || newConf.RTMPServerKey != p.conf.RTMPServerKey || newConf.RTSPAddress != p.conf.RTSPAddress || newConf.RunOnConnect != p.conf.RunOnConnect || newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart || newConf.RunOnDisconnect != p.conf.RunOnDisconnect || closeMetrics || closePathManager || closeLogger closeHLSServer := newConf == nil || newConf.HLS != p.conf.HLS || newConf.HLSAddress != p.conf.HLSAddress || newConf.HLSEncryption != p.conf.HLSEncryption || newConf.HLSServerKey != p.conf.HLSServerKey || newConf.HLSServerCert != p.conf.HLSServerCert || newConf.ExternalAuthenticationURL != p.conf.ExternalAuthenticationURL || newConf.HLSAlwaysRemux != p.conf.HLSAlwaysRemux || newConf.HLSVariant != p.conf.HLSVariant || newConf.HLSSegmentCount != p.conf.HLSSegmentCount || newConf.HLSSegmentDuration != p.conf.HLSSegmentDuration || newConf.HLSPartDuration != p.conf.HLSPartDuration || newConf.HLSSegmentMaxSize != p.conf.HLSSegmentMaxSize || newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin || !reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) || newConf.HLSDirectory != p.conf.HLSDirectory || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || closePathManager || closeMetrics || closeLogger closeWebRTCServer := newConf == nil || newConf.WebRTC != p.conf.WebRTC || newConf.WebRTCAddress != p.conf.WebRTCAddress || newConf.WebRTCEncryption != p.conf.WebRTCEncryption || newConf.WebRTCServerKey != p.conf.WebRTCServerKey || newConf.WebRTCServerCert != p.conf.WebRTCServerCert || newConf.WebRTCAllowOrigin != p.conf.WebRTCAllowOrigin || !reflect.DeepEqual(newConf.WebRTCTrustedProxies, p.conf.WebRTCTrustedProxies) || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.WebRTCLocalUDPAddress != p.conf.WebRTCLocalUDPAddress || newConf.WebRTCLocalTCPAddress != p.conf.WebRTCLocalTCPAddress || newConf.WebRTCIPsFromInterfaces != p.conf.WebRTCIPsFromInterfaces || !reflect.DeepEqual(newConf.WebRTCIPsFromInterfacesList, p.conf.WebRTCIPsFromInterfacesList) || !reflect.DeepEqual(newConf.WebRTCAdditionalHosts, p.conf.WebRTCAdditionalHosts) || !reflect.DeepEqual(newConf.WebRTCICEServers2, p.conf.WebRTCICEServers2) || closeMetrics || closePathManager || closeLogger closeSRTServer := newConf == nil || newConf.SRT != p.conf.SRT || newConf.SRTAddress != p.conf.SRTAddress || newConf.RTSPAddress != p.conf.RTSPAddress || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteTimeout != p.conf.WriteTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || newConf.UDPMaxPayloadSize != p.conf.UDPMaxPayloadSize || newConf.RunOnConnect != p.conf.RunOnConnect || newConf.RunOnConnectRestart != p.conf.RunOnConnectRestart || newConf.RunOnDisconnect != p.conf.RunOnDisconnect || closePathManager || closeLogger closeAPI := newConf == nil || newConf.API != p.conf.API || newConf.APIAddress != p.conf.APIAddress || newConf.ReadTimeout != p.conf.ReadTimeout || closePathManager || closeRTSPServer || closeRTSPSServer || closeRTMPServer || closeHLSServer || closeWebRTCServer || closeSRTServer || closeLogger if newConf == nil && p.confWatcher != nil { p.confWatcher.Close() p.confWatcher = nil } if p.api != nil { if closeAPI { p.api.Close() p.api = nil } else if !calledByAPI { // avoid a loop p.api.ReloadConf(newConf) } } if closeSRTServer && p.srtServer != nil { if p.metrics != nil { p.metrics.SetSRTServer(nil) } p.srtServer.Close() p.srtServer = nil } if closeWebRTCServer && p.webRTCServer != nil { if p.metrics != nil { p.metrics.SetWebRTCServer(nil) } p.webRTCServer.Close() p.webRTCServer = nil } if closeHLSServer && p.hlsServer != nil { if p.metrics != nil { p.metrics.SetHLSServer(nil) } p.pathManager.setHLSServer(nil) p.hlsServer.Close() p.hlsServer = nil } if closeRTMPSServer && p.rtmpsServer != nil { if p.metrics != nil { p.metrics.SetRTMPSServer(nil) } p.rtmpsServer.Close() p.rtmpsServer = nil } if closeRTMPServer && p.rtmpServer != nil { if p.metrics != nil { p.metrics.SetRTMPServer(nil) } p.rtmpServer.Close() p.rtmpServer = nil } if closeRTSPSServer && p.rtspsServer != nil { if p.metrics != nil { p.metrics.SetRTSPSServer(nil) } p.rtspsServer.Close() p.rtspsServer = nil } if closeRTSPServer && p.rtspServer != nil { if p.metrics != nil { p.metrics.SetRTSPServer(nil) } p.rtspServer.Close() p.rtspServer = nil } if closePathManager && p.pathManager != nil { if p.metrics != nil { p.metrics.SetPathManager(nil) } p.pathManager.close() p.pathManager = nil } if closeRecorderCleaner && p.recordCleaner != nil { p.recordCleaner.Close() p.recordCleaner = nil } if closePPROF && p.pprof != nil { p.pprof.Close() p.pprof = nil } if closeMetrics && p.metrics != nil { p.metrics.Close() p.metrics = nil } if newConf == nil && p.externalCmdPool != nil { p.Log(logger.Info, "waiting for running hooks") p.externalCmdPool.Close() } if closeLogger { p.logger.Close() p.logger = nil } } func (p *Core) reloadConf(newConf *conf.Conf, calledByAPI bool) error { p.closeResources(newConf, calledByAPI) p.conf = newConf return p.createResources(false) } // APIConfigSet is called by api. func (p *Core) APIConfigSet(conf *conf.Conf) { select { case p.chAPIConfigSet <- conf: case <-p.ctx.Done(): } }