2023-08-18 12:48:05 +05:00
|
|
|
package flv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"os"
|
2023-08-18 13:24:54 +05:00
|
|
|
"encoding/binary"
|
2023-08-18 12:48:05 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
type FLVWriter struct {
|
2023-08-18 13:24:54 +05:00
|
|
|
w io.Writer
|
2023-08-18 12:48:05 +05:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-08-18 13:24:54 +05:00
|
|
|
writer.w = filepipe
|
2023-08-18 12:48:05 +05:00
|
|
|
if err = writer.write_flv_header(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (writer *FLVWriter) Close() (error) {
|
2023-08-18 13:24:54 +05:00
|
|
|
return writer.w.(*os.File).Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) {
|
|
|
|
uint24_buf := make([]byte, 4)
|
|
|
|
tag_header := make([]byte, 11)
|
|
|
|
tag_header[0] = 18
|
|
|
|
binary.BigEndian.PutUint32(uint24_buf, uint32(len((*data)[16:])))
|
|
|
|
copy(tag_header[1:4], uint24_buf[1:])
|
|
|
|
|
2023-08-18 14:21:31 +05:00
|
|
|
if _, err = writer.w.Write(tag_header); err != nil {
|
|
|
|
return
|
2023-08-18 13:24:54 +05:00
|
|
|
}
|
2023-08-18 14:21:31 +05:00
|
|
|
if _, err = writer.w.Write((*data)[16:]); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tag_len_buf := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(tag_len_buf, uint32(len((*data)[16:]) + 11))
|
|
|
|
_, err = writer.w.Write(tag_len_buf)
|
|
|
|
return
|
2023-08-18 12:48:05 +05:00
|
|
|
}
|
|
|
|
|
2023-08-18 14:21:31 +05:00
|
|
|
|
2023-08-18 12:48:05 +05:00
|
|
|
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
|
|
|
|
|
2023-08-18 13:24:54 +05:00
|
|
|
_, err = writer.w.Write(header)
|
2023-08-18 12:48:05 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|