Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HLS: make closeAfterInactivity configurable #3329

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions apidocs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ components:
type: string
hlsDirectory:
type: string
hlsMuxerCloseAfter:
type: string

# WebRTC server
webrtc:
Expand Down
2 changes: 2 additions & 0 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions internal/servers/hls/muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -55,6 +54,7 @@ type muxer struct {
segmentMaxSize conf.StringSize
directory string
writeQueueSize int
closeAfter conf.StringDuration
wg *sync.WaitGroup
pathName string
pathManager serverPathManager
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions internal/servers/hls/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type Server struct {
Directory string
ReadTimeout conf.StringDuration
WriteQueueSize int
MuxerCloseAfter conf.StringDuration
PathManager serverPathManager
Parent serverParent

Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mediamtx.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading