2023-08-18 12:48:05 +05:00
|
|
|
package flv
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2023-08-18 15:50:24 +05:00
|
|
|
"os/exec"
|
2023-08-18 13:24:54 +05:00
|
|
|
"encoding/binary"
|
2023-08-21 15:10:07 +05:00
|
|
|
"os"
|
2023-08-18 12:48:05 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
type FLVWriter struct {
|
2023-08-21 20:25:54 +05:00
|
|
|
w io.WriteCloser // abstraction as a wrapper to the actual transcoder command
|
2023-08-18 12:48:05 +05:00
|
|
|
}
|
|
|
|
|
2023-08-21 15:10:07 +05:00
|
|
|
func NewFLVWriter(stream_key string) (*FLVWriter, error) {
|
|
|
|
base_dir, _ := os.UserHomeDir()
|
2023-08-18 12:48:05 +05:00
|
|
|
writer := new(FLVWriter)
|
2023-08-21 20:25:54 +05:00
|
|
|
// spawn ffmpeg as a transcoder + hls segmenter
|
|
|
|
// most are generic commands, added a prefix to each segment url to make serving it over http easier
|
2023-08-18 15:50:24 +05:00
|
|
|
transcoder := exec.Command(
|
|
|
|
"ffmpeg",
|
2023-08-21 15:10:07 +05:00
|
|
|
"-probesize", "500",
|
2023-08-18 15:50:24 +05:00
|
|
|
"-i", "pipe:0",
|
|
|
|
"-c:a", "aac",
|
|
|
|
"-c:v", "h264",
|
|
|
|
"-b:v", "1M",
|
|
|
|
"-hls_time", "6",
|
2023-08-21 15:10:07 +05:00
|
|
|
"-hls_list_size", "4",
|
|
|
|
"-hls_base_url", "/vid/" + stream_key + "/",
|
2023-08-18 15:50:24 +05:00
|
|
|
"-hls_flags", "delete_segments",
|
2023-08-21 18:44:12 +05:00
|
|
|
"-hls_flags", "+program_date_time",
|
2023-08-18 15:50:24 +05:00
|
|
|
"stream.m3u8",
|
|
|
|
)
|
2023-08-21 20:25:54 +05:00
|
|
|
transcoder.Dir = base_dir + "/live/" + stream_key + "/" // shift to the appropriate dir for the given stream key
|
|
|
|
flvpipe, err := transcoder.StdinPipe() // give control over the ffmpeg input as a stdin pipe (will push in flv packets as they come)
|
|
|
|
transcoder.Start() // spawn ffmpeg
|
2023-08-18 12:48:05 +05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-08-18 15:50:24 +05:00
|
|
|
writer.w = flvpipe
|
2023-08-21 20:25:54 +05:00
|
|
|
if err = writer.write_flv_header(); err != nil { // init its header now, might as well
|
2023-08-18 12:48:05 +05:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return writer, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (writer *FLVWriter) Close() (error) {
|
2023-08-18 15:50:24 +05:00
|
|
|
return writer.w.Close()
|
2023-08-18 13:24:54 +05:00
|
|
|
}
|
|
|
|
|
2023-08-21 20:25:54 +05:00
|
|
|
// first data tag, AMF0 message but FLV spec requires the encoded data
|
|
|
|
// just requires skipping the RTMP specific bits, then just writing as is
|
2023-08-18 13:24:54 +05:00
|
|
|
func (writer *FLVWriter) WriteMetadataTag(data *[]byte) (err error) {
|
|
|
|
uint24_buf := make([]byte, 4)
|
|
|
|
tag_header := make([]byte, 11)
|
2023-08-21 20:25:54 +05:00
|
|
|
tag_header[0] = 18 // RTMP msg_type 18
|
|
|
|
binary.BigEndian.PutUint32(uint24_buf, uint32(len((*data)[16:]))) // "@setDataFrame", null, actual data
|
2023-08-18 13:24:54 +05:00
|
|
|
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-21 20:25:54 +05:00
|
|
|
// write the actual audio + video data, same as metadata except no skipping
|
2023-08-18 14:43:52 +05:00
|
|
|
func (writer *FLVWriter) WriteMediaTag(data *[]byte, timestamp uint32, media_type uint8) (err error) {
|
|
|
|
uint24_buf := make([]byte, 4)
|
|
|
|
tag_header := make([]byte, 11)
|
|
|
|
tag_header[0] = media_type
|
|
|
|
data_len := uint32(len(*data))
|
|
|
|
binary.BigEndian.PutUint32(uint24_buf, data_len)
|
|
|
|
copy(tag_header[1:4], uint24_buf[1:])
|
|
|
|
|
|
|
|
binary.BigEndian.PutUint32(uint24_buf, timestamp)
|
|
|
|
copy(tag_header[4:7], uint24_buf[1:])
|
|
|
|
tag_header[7] = uint24_buf[0]
|
|
|
|
|
|
|
|
if _, err = writer.w.Write(tag_header); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err = writer.w.Write(*data); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tag_len_buf := make([]byte, 4)
|
|
|
|
binary.BigEndian.PutUint32(tag_len_buf, data_len + 11)
|
|
|
|
_, err = writer.w.Write(tag_len_buf)
|
|
|
|
return
|
|
|
|
}
|
2023-08-18 14:21:31 +05:00
|
|
|
|
2023-08-21 20:25:54 +05:00
|
|
|
// FLV file header always 9 bytes + 4 bytes for first ghost data tag
|
2023-08-18 12:48:05 +05:00
|
|
|
func (writer *FLVWriter) write_flv_header() (err error) {
|
|
|
|
header := make([]byte, 13)
|
|
|
|
copy(header[:3], "FLV")
|
2023-08-21 20:25:54 +05:00
|
|
|
header[3] = 1 // FLV version, only 1 is ever used
|
|
|
|
header[4] = 5 // 1 and 4 -> audio OR video | 5 -> audio + video
|
|
|
|
header[8] = 9 // len of header
|
2023-08-18 12:48:05 +05:00
|
|
|
|
2023-08-21 20:25:54 +05:00
|
|
|
// flv spec requires each tag + tag_len. defines a ghost tag at start with len 0
|
|
|
|
// likely for player seeking?
|
|
|
|
|
2023-08-18 13:24:54 +05:00
|
|
|
_, err = writer.w.Write(header)
|
2023-08-18 12:48:05 +05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|