From 6debb52abdf86c7c9fabcc2283e4e57ad8590c27 Mon Sep 17 00:00:00 2001 From: xjr20042 Date: Thu, 9 May 2024 16:08:15 +0800 Subject: [PATCH] hls: make closeAfterInactivity configurable (#3329) * HLS: make closeAfterInactivity configurable * add `hlsMuxerCloseAfter` to `apidocs/openapi.yaml` --- apidocs/openapi.yaml | 2 ++ internal/conf/conf.go | 2 ++ internal/core/core.go | 2 ++ internal/servers/hls/muxer.go | 8 ++++---- internal/servers/hls/server.go | 2 ++ mediamtx.yml | 3 +++ 6 files changed, 15 insertions(+), 4 deletions(-) diff --git a/apidocs/openapi.yaml b/apidocs/openapi.yaml index 05bab0e9..d4f6e9f0 100644 --- a/apidocs/openapi.yaml +++ b/apidocs/openapi.yaml @@ -199,6 +199,8 @@ components: type: string hlsDirectory: type: string + hlsMuxerCloseAfter: + type: string # WebRTC server webrtc: diff --git a/internal/conf/conf.go b/internal/conf/conf.go index 8ca17bf7..6a26e426 100644 --- a/internal/conf/conf.go +++ b/internal/conf/conf.go @@ -221,6 +221,7 @@ type Conf struct { HLSPartDuration StringDuration `json:"hlsPartDuration"` HLSSegmentMaxSize StringSize `json:"hlsSegmentMaxSize"` HLSDirectory string `json:"hlsDirectory"` + HLSMuxerCloseAfter StringDuration `json:"hlsMuxerCloseAfter"` // WebRTC server WebRTC bool `json:"webrtc"` @@ -378,6 +379,7 @@ func (conf *Conf) setDefaults() { conf.HLSSegmentDuration = 1 * StringDuration(time.Second) conf.HLSPartDuration = 200 * StringDuration(time.Millisecond) conf.HLSSegmentMaxSize = 50 * 1024 * 1024 + conf.HLSMuxerCloseAfter = 60 * StringDuration(time.Second) // WebRTC server conf.WebRTC = true diff --git a/internal/core/core.go b/internal/core/core.go index f7476f84..9f5f6135 100644 --- a/internal/core/core.go +++ b/internal/core/core.go @@ -546,6 +546,7 @@ func (p *Core) createResources(initial bool) error { Directory: p.conf.HLSDirectory, ReadTimeout: p.conf.ReadTimeout, WriteQueueSize: p.conf.WriteQueueSize, + MuxerCloseAfter: p.conf.HLSMuxerCloseAfter, PathManager: p.pathManager, Parent: p, } @@ -826,6 +827,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) { newConf.HLSDirectory != p.conf.HLSDirectory || newConf.ReadTimeout != p.conf.ReadTimeout || newConf.WriteQueueSize != p.conf.WriteQueueSize || + newConf.HLSMuxerCloseAfter != p.conf.HLSMuxerCloseAfter || closePathManager || closeMetrics || closeLogger diff --git a/internal/servers/hls/muxer.go b/internal/servers/hls/muxer.go index c06c7fc2..6c93d97d 100644 --- a/internal/servers/hls/muxer.go +++ b/internal/servers/hls/muxer.go @@ -15,9 +15,8 @@ import ( ) const ( - closeCheckPeriod = 1 * time.Second - closeAfterInactivity = 60 * time.Second - recreatePause = 10 * time.Second + closeCheckPeriod = 1 * time.Second + recreatePause = 10 * time.Second ) func int64Ptr(v int64) *int64 { @@ -55,6 +54,7 @@ type muxer struct { segmentMaxSize conf.StringSize directory string writeQueueSize int + closeAfter conf.StringDuration wg *sync.WaitGroup pathName string pathManager serverPathManager @@ -221,7 +221,7 @@ func (m *muxer) runInner() error { case <-activityCheckTimer.C: t := time.Unix(0, atomic.LoadInt64(m.lastRequestTime)) - if time.Since(t) >= closeAfterInactivity { + if time.Since(t) >= time.Duration(m.closeAfter) { return fmt.Errorf("not used anymore") } activityCheckTimer = time.NewTimer(closeCheckPeriod) diff --git a/internal/servers/hls/server.go b/internal/servers/hls/server.go index 689d22b0..fcaae395 100644 --- a/internal/servers/hls/server.go +++ b/internal/servers/hls/server.go @@ -75,6 +75,7 @@ type Server struct { Directory string ReadTimeout conf.StringDuration WriteQueueSize int + MuxerCloseAfter conf.StringDuration PathManager serverPathManager Parent serverParent @@ -232,6 +233,7 @@ func (s *Server) createMuxer(pathName string, remoteAddr string, query string) * pathManager: s.PathManager, parent: s, query: query, + closeAfter: s.MuxerCloseAfter, } r.initialize() s.muxers[pathName] = r diff --git a/mediamtx.yml b/mediamtx.yml index 822ab449..cdc45cb0 100644 --- a/mediamtx.yml +++ b/mediamtx.yml @@ -329,6 +329,9 @@ hlsSegmentMaxSize: 50M # This decreases performance, since reading from disk is less performant than # reading from RAM, but allows to save RAM. hlsDirectory: '' +# The muxer will be closed when there are no +# reader requests and this amount of time has passed. +hlsMuxerCloseAfter: 60s ############################################### # Global settings -> WebRTC server