package srt import ( "os" "io" "strings" "stream_server/transcoder" "time" "path/filepath" ) func NewWriter(stream_key string) (io.WriteCloser, error) { transcoder_in, err := transcoder.NewTranscoder(stream_key) if err != nil { return nil, err } return transcoder_in, nil } func CleanFiles(stream_key string, delay time.Duration) { time.Sleep(delay * time.Second) base_dir, _ := os.UserHomeDir() stream_dir := base_dir + "/live/" + stream_key fileinfo, _ := os.Stat(stream_dir + "/stream.m3u8") if time.Now().Sub(fileinfo.ModTime()) > delay * time.Second { leftover_files, _ := filepath.Glob(stream_dir + "/*") for _, file := range leftover_files { os.Remove(file) } } } func CheckStreamID(stream_id []byte) (io.WriteCloser, string, bool) { stream_key := make([]byte, 0) for i := len(stream_id) - 1; i >= 0; i-- { if stream_id[i] == 0 { break } stream_key = append(stream_key, stream_id[i]) } stream_key_string := strings.TrimSpace(string(stream_key)) if check_stream_key(stream_key_string) { writer, err := NewWriter(stream_key_string) if err != nil { return nil, stream_key_string, false } return writer, stream_key_string, true } return nil, stream_key_string, false } func check_stream_key(stream_key string) (bool) { base_dir, _ := os.UserHomeDir() if fileinfo, err := os.Stat(base_dir + "/live/" + stream_key); err == nil && fileinfo.IsDir() { return true } return false }