From cbf1492b0e7c8cf890586aec5216740ce4be6b17 Mon Sep 17 00:00:00 2001 From: Muaz Ahmad Date: Fri, 18 Aug 2023 12:48:05 +0500 Subject: [PATCH] flv stream data handling, temporary dump to a file instead of ffmpeg --- rtmp/data_loop.go | 6 ++++++ rtmp/flv/writer.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 rtmp/flv/writer.go diff --git a/rtmp/data_loop.go b/rtmp/data_loop.go index 56c095d..39c31a0 100644 --- a/rtmp/data_loop.go +++ b/rtmp/data_loop.go @@ -2,9 +2,14 @@ package rtmp import ( "fmt" + "stream_server/rtmp/flv" ) func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { + file_writer, err := flv.NewFLVWriter(chnk_wrp_ptr.params.stream_key) + if err != nil { + return + } for { p, err := chnk_wrp_ptr.ReadChunk() if err != nil { @@ -14,4 +19,5 @@ func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) { fmt.Println(p.msg_type, p.msg_len) } } + file_writer.Close() } diff --git a/rtmp/flv/writer.go b/rtmp/flv/writer.go new file mode 100644 index 0000000..e8a0364 --- /dev/null +++ b/rtmp/flv/writer.go @@ -0,0 +1,42 @@ +package flv + +import ( + "io" + "os" +) + +type FLVWriter struct { + W io.Writer + curr_offset int +} + +func NewFLVWriter(stream_dir string) (*FLVWriter, error) { + writer := new(FLVWriter) + base_dir, _ := os.UserHomeDir() + filepipe, err := os.Create(base_dir + "/live/" + stream_dir + "/stream.flv") + if err != nil { + return nil, err + } + writer.W = filepipe + if err = writer.write_flv_header(); err != nil { + return nil, err + } + return writer, nil +} + +func (writer *FLVWriter) Close() (error) { + return writer.W.(*os.File).Close() +} + +func (writer *FLVWriter) write_flv_header() (err error) { + header := make([]byte, 13) + copy(header[:3], "FLV") + header[3] = 1 + header[4] = 5 + header[8] = 9 + + n, err := writer.W.Write(header) + writer.curr_offset += n + return +} +