Skip to content

Commit

Permalink
hls muxer: add option to dump segments on disk (#1322)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 committed Mar 12, 2023
1 parent ed45a09 commit 18ab4c9
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 5 deletions.
2 changes: 2 additions & 0 deletions apidocs/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ components:
type: array
items:
type: string
hlsDirectory:
type: string

# WebRTC
webrtcDisable:
Expand Down
1 change: 1 addition & 0 deletions internal/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ type Conf struct {
HLSSegmentMaxSize StringSize `json:"hlsSegmentMaxSize"`
HLSAllowOrigin string `json:"hlsAllowOrigin"`
HLSTrustedProxies IPsOrCIDRs `json:"hlsTrustedProxies"`
HLSDirectory string `json:"hlsDirectory"`

// WebRTC
WebRTCDisable bool `json:"webrtcDisable"`
Expand Down
2 changes: 2 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ func (p *Core) createResources(initial bool) error {
p.conf.HLSSegmentMaxSize,
p.conf.HLSAllowOrigin,
p.conf.HLSTrustedProxies,
p.conf.HLSDirectory,
p.conf.ReadBufferCount,
p.pathManager,
p.metrics,
Expand Down Expand Up @@ -575,6 +576,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
newConf.HLSSegmentMaxSize != p.conf.HLSSegmentMaxSize ||
newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin ||
!reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) ||
newConf.HLSDirectory != p.conf.HLSDirectory ||
newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closePathManager ||
closeMetrics
Expand Down
18 changes: 14 additions & 4 deletions internal/core/hls_muxer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import (
"io"
"net"
"net/http"
"path/filepath"
"sync"
"sync/atomic"
"time"
"os"

"github.com/aler9/gortsplib/v2/pkg/codecs/mpeg4audio"
"github.com/aler9/gortsplib/v2/pkg/format"
Expand Down Expand Up @@ -56,7 +58,6 @@ type hlsMuxerParent interface {
}

type hlsMuxer struct {
name string
remoteAddr string
externalAuthenticationURL string
alwaysRemux bool
Expand All @@ -65,6 +66,7 @@ type hlsMuxer struct {
segmentDuration conf.StringDuration
partDuration conf.StringDuration
segmentMaxSize conf.StringSize
directory string
readBufferCount int
wg *sync.WaitGroup
pathName string
Expand All @@ -88,7 +90,6 @@ type hlsMuxer struct {

func newHLSMuxer(
parentCtx context.Context,
name string,
remoteAddr string,
externalAuthenticationURL string,
alwaysRemux bool,
Expand All @@ -97,6 +98,7 @@ func newHLSMuxer(
segmentDuration conf.StringDuration,
partDuration conf.StringDuration,
segmentMaxSize conf.StringSize,
directory string,
readBufferCount int,
wg *sync.WaitGroup,
pathName string,
Expand All @@ -106,7 +108,6 @@ func newHLSMuxer(
ctx, ctxCancel := context.WithCancel(parentCtx)

m := &hlsMuxer{
name: name,
remoteAddr: remoteAddr,
externalAuthenticationURL: externalAuthenticationURL,
alwaysRemux: alwaysRemux,
Expand All @@ -115,6 +116,7 @@ func newHLSMuxer(
segmentDuration: segmentDuration,
partDuration: partDuration,
segmentMaxSize: segmentMaxSize,
directory: directory,
readBufferCount: readBufferCount,
wg: wg,
pathName: pathName,
Expand Down Expand Up @@ -207,7 +209,7 @@ func (m *hlsMuxer) run() {
}

case req := <-m.chAPIHLSMuxersList:
req.data.Items[m.name] = hlsServerAPIMuxersListItem{
req.data.Items[m.pathName] = hlsServerAPIMuxersListItem{
Created: m.created,
LastRequest: time.Unix(0, atomic.LoadInt64(m.lastRequestTime)),
BytesSent: atomic.LoadUint64(m.bytesSent),
Expand Down Expand Up @@ -296,6 +298,13 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{})
"the stream doesn't contain any supported codec (which are currently H264, H265, MPEG4-Audio, Opus)")
}

var muxerDirectory string
if m.directory != "" {
muxerDirectory = filepath.Join(m.directory, m.pathName)
os.MkdirAll(muxerDirectory, 0o755)
defer os.Remove(muxerDirectory)
}

m.muxer = &gohlslib.Muxer{
Variant: gohlslib.MuxerVariant(m.variant),
SegmentCount: m.segmentCount,
Expand All @@ -304,6 +313,7 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{})
SegmentMaxSize: uint64(m.segmentMaxSize),
VideoTrack: videoFormat,
AudioTrack: audioFormat,
Directory: muxerDirectory,
}

err := m.muxer.Start()
Expand Down
5 changes: 4 additions & 1 deletion internal/core/hls_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type hlsServer struct {
segmentMaxSize conf.StringSize
allowOrigin string
trustedProxies conf.IPsOrCIDRs
directory string
readBufferCount int
pathManager *pathManager
metrics *metrics
Expand Down Expand Up @@ -99,6 +100,7 @@ func newHLSServer(
segmentMaxSize conf.StringSize,
allowOrigin string,
trustedProxies conf.IPsOrCIDRs,
directory string,
readBufferCount int,
pathManager *pathManager,
metrics *metrics,
Expand Down Expand Up @@ -134,6 +136,7 @@ func newHLSServer(
segmentMaxSize: segmentMaxSize,
allowOrigin: allowOrigin,
trustedProxies: trustedProxies,
directory: directory,
readBufferCount: readBufferCount,
pathManager: pathManager,
parent: parent,
Expand Down Expand Up @@ -344,7 +347,6 @@ func (s *hlsServer) onRequest(ctx *gin.Context) {
func (s *hlsServer) createMuxer(pathName string, remoteAddr string) *hlsMuxer {
r := newHLSMuxer(
s.ctx,
pathName,
remoteAddr,
s.externalAuthenticationURL,
s.alwaysRemux,
Expand All @@ -353,6 +355,7 @@ func (s *hlsServer) createMuxer(pathName string, remoteAddr string) *hlsMuxer {
s.segmentDuration,
s.partDuration,
s.segmentMaxSize,
s.directory,
s.readBufferCount,
&s.wg,
pathName,
Expand Down
4 changes: 4 additions & 0 deletions rtsp-simple-server.yml
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ hlsAllowOrigin: '*'
# If the server receives a request from one of these entries, IP in logs
# will be taken from the X-Forwarded-For header.
hlsTrustedProxies: []
# Directory in which to save segments, instead of keeping them in the RAM.
# This decreases performance, since reading from disk is less performant than
# reading from RAM, but allows to save RAM.
hlsDirectory: ''

###############################################
# WebRTC parameters
Expand Down

0 comments on commit 18ab4c9

Please sign in to comment.