54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
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, nil) // remove any unwanted media files still left over
|
|
file_writer, err := flv.NewFLVWriter(chnk_wrp_ptr.params.stream_key) // create a file writer (techincally a pipe to ffmpeg)
|
|
defer file_writer.Close()
|
|
if err != nil {
|
|
return
|
|
}
|
|
// first data message must always be metadata
|
|
if err = chnk_wrp_ptr.ReadMetadata(file_writer); err != nil {
|
|
return
|
|
}
|
|
for {
|
|
msg_ptr, err := chnk_wrp_ptr.ReadChunk()
|
|
if err != nil {
|
|
return
|
|
} else if msg_ptr == nil { // if message was not fully read (mostly first video chunk)
|
|
continue // skip until full message
|
|
}
|
|
switch msg_ptr.msg_type {
|
|
case 20:
|
|
return // should try to process and check specifically for unpublish, works about the same either way
|
|
case 8, 9: // audio or video data
|
|
if err = file_writer.WriteMediaTag(&(msg_ptr.data), msg_ptr.timestamp, msg_ptr.msg_type); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// find all files in given directory and delete, simple as
|
|
func StreamCleanup(stream_key string, delay time.Duration, stream_live *bool) {
|
|
time.Sleep(delay * time.Second)
|
|
base_dir, _ := os.UserHomeDir() // why would this ever need error handling? if it throws a problem something went very wrong somewhere anyway
|
|
if stream_live != nil && *stream_live == true {
|
|
return
|
|
}
|
|
stream_dir := base_dir + "/live/" + stream_key
|
|
|
|
leftover_files, _ := filepath.Glob(stream_dir + "/*")
|
|
for _, file := range leftover_files {
|
|
os.Remove(file)
|
|
}
|
|
|
|
}
|