Browse Source

hls muxer: add option to dump segments on disk (#1322) (#1567)

pull/1583/head
Alessandro Ros 2 years ago committed by GitHub
parent
commit
c1bcd0c7eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      apidocs/openapi.yaml
  2. 1
      internal/conf/conf.go
  3. 2
      internal/core/core.go
  4. 18
      internal/core/hls_muxer.go
  5. 5
      internal/core/hls_server.go
  6. 4
      rtsp-simple-server.yml

2
apidocs/openapi.yaml

@ -127,6 +127,8 @@ components:
type: array type: array
items: items:
type: string type: string
hlsDirectory:
type: string
# WebRTC # WebRTC
webrtcDisable: webrtcDisable:

1
internal/conf/conf.go

@ -222,6 +222,7 @@ type Conf struct {
HLSSegmentMaxSize StringSize `json:"hlsSegmentMaxSize"` HLSSegmentMaxSize StringSize `json:"hlsSegmentMaxSize"`
HLSAllowOrigin string `json:"hlsAllowOrigin"` HLSAllowOrigin string `json:"hlsAllowOrigin"`
HLSTrustedProxies IPsOrCIDRs `json:"hlsTrustedProxies"` HLSTrustedProxies IPsOrCIDRs `json:"hlsTrustedProxies"`
HLSDirectory string `json:"hlsDirectory"`
// WebRTC // WebRTC
WebRTCDisable bool `json:"webrtcDisable"` WebRTCDisable bool `json:"webrtcDisable"`

2
internal/core/core.go

@ -400,6 +400,7 @@ func (p *Core) createResources(initial bool) error {
p.conf.HLSSegmentMaxSize, p.conf.HLSSegmentMaxSize,
p.conf.HLSAllowOrigin, p.conf.HLSAllowOrigin,
p.conf.HLSTrustedProxies, p.conf.HLSTrustedProxies,
p.conf.HLSDirectory,
p.conf.ReadBufferCount, p.conf.ReadBufferCount,
p.pathManager, p.pathManager,
p.metrics, p.metrics,
@ -575,6 +576,7 @@ func (p *Core) closeResources(newConf *conf.Conf, calledByAPI bool) {
newConf.HLSSegmentMaxSize != p.conf.HLSSegmentMaxSize || newConf.HLSSegmentMaxSize != p.conf.HLSSegmentMaxSize ||
newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin || newConf.HLSAllowOrigin != p.conf.HLSAllowOrigin ||
!reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) || !reflect.DeepEqual(newConf.HLSTrustedProxies, p.conf.HLSTrustedProxies) ||
newConf.HLSDirectory != p.conf.HLSDirectory ||
newConf.ReadBufferCount != p.conf.ReadBufferCount || newConf.ReadBufferCount != p.conf.ReadBufferCount ||
closePathManager || closePathManager ||
closeMetrics closeMetrics

18
internal/core/hls_muxer.go

@ -9,6 +9,8 @@ import (
"io" "io"
"net" "net"
"net/http" "net/http"
"os"
"path/filepath"
"sync" "sync"
"sync/atomic" "sync/atomic"
"time" "time"
@ -56,7 +58,6 @@ type hlsMuxerParent interface {
} }
type hlsMuxer struct { type hlsMuxer struct {
name string
remoteAddr string remoteAddr string
externalAuthenticationURL string externalAuthenticationURL string
alwaysRemux bool alwaysRemux bool
@ -65,6 +66,7 @@ type hlsMuxer struct {
segmentDuration conf.StringDuration segmentDuration conf.StringDuration
partDuration conf.StringDuration partDuration conf.StringDuration
segmentMaxSize conf.StringSize segmentMaxSize conf.StringSize
directory string
readBufferCount int readBufferCount int
wg *sync.WaitGroup wg *sync.WaitGroup
pathName string pathName string
@ -88,7 +90,6 @@ type hlsMuxer struct {
func newHLSMuxer( func newHLSMuxer(
parentCtx context.Context, parentCtx context.Context,
name string,
remoteAddr string, remoteAddr string,
externalAuthenticationURL string, externalAuthenticationURL string,
alwaysRemux bool, alwaysRemux bool,
@ -97,6 +98,7 @@ func newHLSMuxer(
segmentDuration conf.StringDuration, segmentDuration conf.StringDuration,
partDuration conf.StringDuration, partDuration conf.StringDuration,
segmentMaxSize conf.StringSize, segmentMaxSize conf.StringSize,
directory string,
readBufferCount int, readBufferCount int,
wg *sync.WaitGroup, wg *sync.WaitGroup,
pathName string, pathName string,
@ -106,7 +108,6 @@ func newHLSMuxer(
ctx, ctxCancel := context.WithCancel(parentCtx) ctx, ctxCancel := context.WithCancel(parentCtx)
m := &hlsMuxer{ m := &hlsMuxer{
name: name,
remoteAddr: remoteAddr, remoteAddr: remoteAddr,
externalAuthenticationURL: externalAuthenticationURL, externalAuthenticationURL: externalAuthenticationURL,
alwaysRemux: alwaysRemux, alwaysRemux: alwaysRemux,
@ -115,6 +116,7 @@ func newHLSMuxer(
segmentDuration: segmentDuration, segmentDuration: segmentDuration,
partDuration: partDuration, partDuration: partDuration,
segmentMaxSize: segmentMaxSize, segmentMaxSize: segmentMaxSize,
directory: directory,
readBufferCount: readBufferCount, readBufferCount: readBufferCount,
wg: wg, wg: wg,
pathName: pathName, pathName: pathName,
@ -207,7 +209,7 @@ func (m *hlsMuxer) run() {
} }
case req := <-m.chAPIHLSMuxersList: case req := <-m.chAPIHLSMuxersList:
req.data.Items[m.name] = hlsServerAPIMuxersListItem{ req.data.Items[m.pathName] = hlsServerAPIMuxersListItem{
Created: m.created, Created: m.created,
LastRequest: time.Unix(0, atomic.LoadInt64(m.lastRequestTime)), LastRequest: time.Unix(0, atomic.LoadInt64(m.lastRequestTime)),
BytesSent: atomic.LoadUint64(m.bytesSent), BytesSent: atomic.LoadUint64(m.bytesSent),
@ -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)") "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{ m.muxer = &gohlslib.Muxer{
Variant: gohlslib.MuxerVariant(m.variant), Variant: gohlslib.MuxerVariant(m.variant),
SegmentCount: m.segmentCount, SegmentCount: m.segmentCount,
@ -304,6 +313,7 @@ func (m *hlsMuxer) runInner(innerCtx context.Context, innerReady chan struct{})
SegmentMaxSize: uint64(m.segmentMaxSize), SegmentMaxSize: uint64(m.segmentMaxSize),
VideoTrack: videoFormat, VideoTrack: videoFormat,
AudioTrack: audioFormat, AudioTrack: audioFormat,
Directory: muxerDirectory,
} }
err := m.muxer.Start() err := m.muxer.Start()

5
internal/core/hls_server.go

@ -64,6 +64,7 @@ type hlsServer struct {
segmentMaxSize conf.StringSize segmentMaxSize conf.StringSize
allowOrigin string allowOrigin string
trustedProxies conf.IPsOrCIDRs trustedProxies conf.IPsOrCIDRs
directory string
readBufferCount int readBufferCount int
pathManager *pathManager pathManager *pathManager
metrics *metrics metrics *metrics
@ -99,6 +100,7 @@ func newHLSServer(
segmentMaxSize conf.StringSize, segmentMaxSize conf.StringSize,
allowOrigin string, allowOrigin string,
trustedProxies conf.IPsOrCIDRs, trustedProxies conf.IPsOrCIDRs,
directory string,
readBufferCount int, readBufferCount int,
pathManager *pathManager, pathManager *pathManager,
metrics *metrics, metrics *metrics,
@ -134,6 +136,7 @@ func newHLSServer(
segmentMaxSize: segmentMaxSize, segmentMaxSize: segmentMaxSize,
allowOrigin: allowOrigin, allowOrigin: allowOrigin,
trustedProxies: trustedProxies, trustedProxies: trustedProxies,
directory: directory,
readBufferCount: readBufferCount, readBufferCount: readBufferCount,
pathManager: pathManager, pathManager: pathManager,
parent: parent, parent: parent,
@ -344,7 +347,6 @@ func (s *hlsServer) onRequest(ctx *gin.Context) {
func (s *hlsServer) createMuxer(pathName string, remoteAddr string) *hlsMuxer { func (s *hlsServer) createMuxer(pathName string, remoteAddr string) *hlsMuxer {
r := newHLSMuxer( r := newHLSMuxer(
s.ctx, s.ctx,
pathName,
remoteAddr, remoteAddr,
s.externalAuthenticationURL, s.externalAuthenticationURL,
s.alwaysRemux, s.alwaysRemux,
@ -353,6 +355,7 @@ func (s *hlsServer) createMuxer(pathName string, remoteAddr string) *hlsMuxer {
s.segmentDuration, s.segmentDuration,
s.partDuration, s.partDuration,
s.segmentMaxSize, s.segmentMaxSize,
s.directory,
s.readBufferCount, s.readBufferCount,
&s.wg, &s.wg,
pathName, pathName,

4
rtsp-simple-server.yml

@ -167,6 +167,10 @@ hlsAllowOrigin: '*'
# If the server receives a request from one of these entries, IP in logs # If the server receives a request from one of these entries, IP in logs
# will be taken from the X-Forwarded-For header. # will be taken from the X-Forwarded-For header.
hlsTrustedProxies: [] 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 # WebRTC parameters

Loading…
Cancel
Save