ffmpeg-playout/playout/playout.go

153 lines
3.4 KiB
Go
Raw Normal View History

2020-11-05 18:34:54 +01:00
package playout
import (
2020-12-27 16:52:31 +01:00
"bytes"
2020-11-05 18:34:54 +01:00
"fmt"
2021-05-22 11:37:14 +02:00
"git.entr0py.de/garionion/ffmpeg-playout/status"
2020-12-28 22:20:35 +01:00
"github.com/grafov/bcast"
2020-11-05 18:34:54 +01:00
"log"
2020-12-27 16:52:31 +01:00
"net/http"
"net/url"
"os"
2020-12-26 17:20:18 +01:00
"os/exec"
"path"
2020-12-27 16:52:31 +01:00
"strconv"
2020-12-28 22:20:35 +01:00
"syscall"
2020-11-05 18:34:54 +01:00
"time"
)
type Job struct {
2020-12-29 17:34:34 +01:00
ID int64
2020-12-26 17:20:18 +01:00
Version string
2020-11-05 18:34:54 +01:00
StartAt time.Time
StopAt time.Time
Source string
Output string
ControlChannel chan string
}
2020-12-26 17:20:18 +01:00
type Config struct {
2020-12-27 16:52:31 +01:00
PlayoutScriptPath string
PlayoutScript string
ProgressDir string
PrometheusPushGateway string
2020-12-28 22:20:35 +01:00
OutputFormat string
2020-12-26 17:20:18 +01:00
}
2020-12-28 22:20:35 +01:00
func monitorFFmpeg(cfg *Config, progressPath string, f *bcast.Member, output string) {
stats := make(chan status.Status)
c := 0
2020-12-26 17:20:18 +01:00
for {
2020-12-28 22:20:35 +01:00
if c > 15 {
return
}
if _, err := os.Stat(progressPath); os.IsNotExist(err) {
time.Sleep(1 * time.Second)
c++
continue
}
err := status.FFmpegStatusWatcher(stats, progressPath)
if err != nil {
log.Println(err)
2020-12-26 17:20:18 +01:00
continue
2020-12-28 22:20:35 +01:00
} else {
break
}
}
u, _ := url.Parse(cfg.PrometheusPushGateway)
u.Path = path.Join(u.Path, "metrics", "job", "Decklink_Outputs", output)
c = 0
2020-12-26 17:20:18 +01:00
2020-12-28 22:20:35 +01:00
for c < 4 {
select {
case <-f.Read:
return
case stat := <-stats:
metric := fmt.Sprintf("frame %v\n dup_frames %v\n bitrate %v\n fps %v\n speed %v\n", stat.Frame, stat.Dupframes, stat.Bitrate, stat.FPS, stat.Speed)
resp, err := http.Post(u.String(), "text/plain", bytes.NewBuffer([]byte(metric)))
2020-12-26 17:20:18 +01:00
if err != nil {
2020-12-28 22:20:35 +01:00
// log.Println(err)
continue
2020-12-26 17:20:18 +01:00
}
2020-12-28 22:20:35 +01:00
resp.Body.Close()
case <-time.After(4 * time.Second):
c++
break
}
}
f.Send("finished")
f.Close()
}
2020-12-29 17:34:34 +01:00
func killProcess(cmd *exec.Cmd, id int64) {
2020-12-28 22:20:35 +01:00
log.Printf("Kill %v", cmd.Process.Pid)
err := syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)
if err != nil {
log.Printf("Failed to kill %v: %v", cmd.Process.Pid, err)
}
log.Println(cmd.ProcessState.ExitCode())
log.Printf("Finish %d", id)
}
//nolint:funlen
func (p *Job) Playout(cfg *Config) {
2020-12-29 17:34:34 +01:00
progressPath := path.Join(cfg.ProgressDir, strconv.FormatInt(p.ID, 10))
2020-12-28 22:20:35 +01:00
playoutScript := path.Join(cfg.PlayoutScriptPath, cfg.PlayoutScript)
for {
log.Printf("Start Playout for %v", p.ID)
2020-12-29 22:31:47 +01:00
log.Println(p.StartAt)
2020-12-29 13:01:30 +01:00
log.Println(p.StopAt)
log.Println(time.Until(p.StopAt))
2020-12-28 22:20:35 +01:00
cmd := exec.Command(playoutScript, //nolint:gosec
"-i", p.Source,
"-o", p.Output,
"-f", cfg.OutputFormat,
"-p", progressPath,
)
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
cmd.Dir = cfg.PlayoutScriptPath
var outbuf, errbuf bytes.Buffer
cmd.Stdout = &outbuf
cmd.Stderr = &errbuf
err := cmd.Start()
if err != nil {
log.Printf("Failed to start Playout %v: %v", p.ID, err)
}
defer killProcess(cmd, p.ID)
pid := cmd.Process.Pid
log.Printf("PID for %v: %v", p.ID, pid)
log.Println(cmd.Args)
finishChannel := bcast.NewGroup()
go finishChannel.Broadcast(0)
go func(cmd *exec.Cmd, f *bcast.Member) {
err := cmd.Wait()
2020-12-26 17:20:18 +01:00
if err != nil {
log.Println(err)
}
2020-12-28 22:20:35 +01:00
f.Send("finished")
f.Close()
}(cmd, finishChannel.Join())
go monitorFFmpeg(cfg, progressPath, finishChannel.Join(), p.Output)
fChannelMember := finishChannel.Join()
ProcessManagement:
for {
select {
case <-fChannelMember.Read:
break ProcessManagement
case ctrlMsg := <-p.ControlChannel:
2020-12-29 16:23:12 +01:00
log.Printf("%d Control Message: %s", p.ID, ctrlMsg)
2020-12-28 22:20:35 +01:00
break
case <-time.After(time.Until(p.StopAt)):
break ProcessManagement
}
}
if time.Now().After(p.StopAt) {
2020-12-27 16:52:31 +01:00
return
2020-12-26 17:20:18 +01:00
}
2020-11-05 18:34:54 +01:00
}
}