From 5941184adaac1a37cb6a79a836fab0cc3184c2c1 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 18 Aug 2023 16:22:25 +0500 Subject: [PATCH] auto-clean stream directory at start of new stream and after 1m at end of stream, holy shit it just works --- rtmp/data_loop.go | 15 +++++++++++++++ rtmp/server.go | 1 + 2 files changed, 16 insertions(+) diff --git a/rtmp/data_loop.go b/rtmp/data_loop.go index a5e23a3..c40cfe8 100644 --- a/rtmp/data_loop.go +++ b/rtmp/data_loop.go @@ -3,9 +3,12 @@ package rtmp import ( "stream_server/rtmp/flv" "os" + "time" + "path/filepath" ) func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { + StreamCleanup(chnk_wrp_ptr.params.stream_key, 0) base_dir, _ := os.UserHomeDir() file_writer, err := flv.NewFLVWriter(base_dir + "/live/" + chnk_wrp_ptr.params.stream_key + "/") defer file_writer.Close() @@ -32,3 +35,15 @@ func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { } } } + +func StreamCleanup(stream_key string, delay time.Duration) { + time.Sleep(delay * time.Second) + base_dir, _ := os.UserHomeDir() + stream_dir := base_dir + "/live/" + stream_key + + leftover_files, _ := filepath.Glob(stream_dir + "/*") + for _, file := range leftover_files { + os.Remove(file) + } + +} diff --git a/rtmp/server.go b/rtmp/server.go index ef2885d..406ca6b 100644 --- a/rtmp/server.go +++ b/rtmp/server.go @@ -53,5 +53,6 @@ func handle_conn(conn net.Conn, stream_live *bool) { return } HandleDataLoop(chunk_wrapper) + go StreamCleanup(chunk_wrapper.params.stream_key, 60) *stream_live = false }