Skip to content

Commit

Permalink
hls: fix directory creation when using hlsDirectory (#3135) (#3151)
Browse files Browse the repository at this point in the history
  • Loading branch information
aler9 authored Mar 19, 2024
1 parent 1d4ea2c commit 4e581af
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/servers/hls/muxer_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func (mi *muxerInstance) initialize() error {
if mi.directory != "" {
muxerDirectory = filepath.Join(mi.directory, mi.pathName)
os.MkdirAll(muxerDirectory, 0o755)
defer os.Remove(muxerDirectory)
}

mi.hmuxer = &gohlslib.Muxer{
Expand Down Expand Up @@ -91,6 +90,9 @@ func (mi *muxerInstance) close() {
mi.writer.Stop()
mi.hmuxer.Close()
mi.stream.RemoveReader(mi.writer)
if mi.hmuxer.Directory != "" {
os.Remove(mi.hmuxer.Directory)
}
}

func (mi *muxerInstance) createVideoTrack() *gohlslib.Track {
Expand Down
50 changes: 50 additions & 0 deletions internal/servers/hls/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package hls
import (
"fmt"
"net/http"
"os"
"path/filepath"
"testing"
"time"

Expand Down Expand Up @@ -293,3 +295,51 @@ func TestServerRead(t *testing.T) {
<-recv
})
}

func TestDirectory(t *testing.T) {
dir, err := os.MkdirTemp("", "mediamtx-playback")
require.NoError(t, err)
defer os.RemoveAll(dir)

desc := &description.Session{Medias: []*description.Media{test.MediaH264}}

stream, err := stream.New(
1460,
desc,
true,
test.NilLogger{},
)
require.NoError(t, err)

pathManager := &dummyPathManager{stream: stream}

s := &Server{
Address: "127.0.0.1:8888",
Encryption: false,
ServerKey: "",
ServerCert: "",
AlwaysRemux: true,
Variant: conf.HLSVariant(gohlslib.MuxerVariantMPEGTS),
SegmentCount: 7,
SegmentDuration: conf.StringDuration(1 * time.Second),
PartDuration: conf.StringDuration(200 * time.Millisecond),
SegmentMaxSize: 50 * 1024 * 1024,
AllowOrigin: "",
TrustedProxies: conf.IPNetworks{},
Directory: filepath.Join(dir, "mydir"),
ReadTimeout: conf.StringDuration(10 * time.Second),
WriteQueueSize: 512,
PathManager: pathManager,
Parent: &test.NilLogger{},
}
err = s.Initialize()
require.NoError(t, err)
defer s.Close()

s.PathReady(&dummyPath{})

time.Sleep(100 * time.Millisecond)

_, err = os.Stat(filepath.Join(dir, "mydir", "mystream"))
require.NoError(t, err)
}

0 comments on commit 4e581af

Please sign in to comment.