2023-08-17 16:35:06 +05:00
|
|
|
package rtmp
|
|
|
|
|
|
|
|
import (
|
2023-08-18 12:48:05 +05:00
|
|
|
"stream_server/rtmp/flv"
|
2023-08-18 15:50:24 +05:00
|
|
|
"os"
|
2023-08-18 16:22:25 +05:00
|
|
|
"time"
|
|
|
|
"path/filepath"
|
2023-08-17 16:35:06 +05:00
|
|
|
)
|
|
|
|
|
|
|
|
func HandleDataLoop(chnk_wrp_ptr *ChunkWrapper) {
|
2023-08-24 12:51:12 +05:00
|
|
|
|
|
|
|
StreamCleanup(chnk_wrp_ptr.params.stream_key, 0, nil) // remove any unwanted media files still left over
|
2023-08-21 20:25:54 +05:00
|
|
|
file_writer, err := flv.NewFLVWriter(chnk_wrp_ptr.params.stream_key) // create a file writer (techincally a pipe to ffmpeg)
|
2023-08-18 13:24:54 +05:00
|
|
|
defer file_writer.Close()
|
2023-08-18 12:48:05 +05:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-21 20:25:54 +05:00
|
|
|
// first data message must always be metadata
|
2023-08-18 13:24:54 +05:00
|
|
|
if err = chnk_wrp_ptr.ReadMetadata(file_writer); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-17 16:35:06 +05:00
|
|
|
for {
|
2023-08-18 14:43:52 +05:00
|
|
|
msg_ptr, err := chnk_wrp_ptr.ReadChunk()
|
2023-08-17 16:35:06 +05:00
|
|
|
if err != nil {
|
|
|
|
return
|
2023-08-21 20:25:54 +05:00
|
|
|
} else if msg_ptr == nil { // if message was not fully read (mostly first video chunk)
|
|
|
|
continue // skip until full message
|
2023-08-17 16:35:06 +05:00
|
|
|
}
|
2023-08-18 14:43:52 +05:00
|
|
|
switch msg_ptr.msg_type {
|
|
|
|
case 20:
|
2023-08-21 20:25:54 +05:00
|
|
|
return // should try to process and check specifically for unpublish, works about the same either way
|
|
|
|
case 8, 9: // audio or video data
|
2023-08-18 14:43:52 +05:00
|
|
|
if err = file_writer.WriteMediaTag(&(msg_ptr.data), msg_ptr.timestamp, msg_ptr.msg_type); err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-17 16:35:06 +05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-18 16:22:25 +05:00
|
|
|
|
2023-08-21 20:25:54 +05:00
|
|
|
// find all files in given directory and delete, simple as
|
2023-08-24 12:51:12 +05:00
|
|
|
func StreamCleanup(stream_key string, delay time.Duration, stream_live *bool) {
|
2023-08-18 16:22:25 +05:00
|
|
|
time.Sleep(delay * time.Second)
|
2023-08-21 20:25:54 +05:00
|
|
|
base_dir, _ := os.UserHomeDir() // why would this ever need error handling? if it throws a problem something went very wrong somewhere anyway
|
2023-08-24 12:51:12 +05:00
|
|
|
if stream_live != nil && *stream_live == true {
|
|
|
|
return
|
|
|
|
}
|
2023-08-18 16:22:25 +05:00
|
|
|
stream_dir := base_dir + "/live/" + stream_key
|
|
|
|
|
|
|
|
leftover_files, _ := filepath.Glob(stream_dir + "/*")
|
|
|
|
for _, file := range leftover_files {
|
|
|
|
os.Remove(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|