project structure refactoring

This commit is contained in:
Artem
2024-11-05 23:47:00 +01:00
parent 1f5a56cc5d
commit c228921237
16 changed files with 51 additions and 47 deletions

97
external/ffmpeg/ffmpeg.go vendored Normal file
View File

@@ -0,0 +1,97 @@
package ffmpeg
import (
"rkkvm/config"
"rkkvm/external/process"
)
//h264
//ffmpeg -re -i /dev/video0 -c:v h264_rkmpp -b:v 2000000 -bsf:v h264_mp4toannexb -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200
//h265
//ffmpeg -re -i /dev/video0 -c:v hevc_rkmpp -b:v 2000000 -g 10 -f rtp rtp://127.0.0.1:5004?pkt_size=1200
// vp8
//ffmpeg -re -i /dev/video0 -c:v libvpx -crf 10 -b:v 3M -deadline 1 -g 10 -error-resilient 1 -auto-alt-ref 1 -an -f rtp rtp://127.0.0.1:5004?pkt_size=1200
/*
arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -init_hw_device rkmpp=hw -filter_hw_device hw \
-f wav -thread_queue_size 4096 -i pipe:0 -f v4l2 -i /dev/video0 \
-vf hwupload,scale_rkrga=h=720:force_original_aspect_ratio=1 \
-c:v h264_rkmpp -flags +low_delay -b:v 6000000 -framerate 60 -g 10 \
-map 0:a -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200 \
-map 1:v -f rtp rtp://127.0.0.1:5004?pkt_size=1200
arecord -D hw:0,0 -f cd -r 44100 -c 2 | /app/ffmpeg -re -f wav -i pipe:0 -c:a aac -b:a 128k -ar 44100 -ac 2 -f rtp rtp://127.0.0.1:5006?pkt_size=1200
arecord -D hw:0,0 -f dat -r 48000 -c 2 --buffer-size=60 | /app/ffmpeg -re -init_hw_device rkmpp=hw -filter_hw_device hw \
-f wav -i pipe:0 -map 0:a -c:a libopus -b:a 48000 -sample_fmt s16 -ssrc 1 -payload_type 111 -f rtp -max_delay 0 -application lowdelay \
-f rtp rtp://127.0.0.1:5006?pkt_size=1200 \
-i /dev/video0 -map 1:v -vf hwupload,scale_rkrga=h=720:force_original_aspect_ratio=1 -c:v h264_rkmpp -flags +low_delay -b:v 6000000 -framerate 60 -g 10 \
-f rtp rtp://127.0.0.1:5004?pkt_size=1200
*/
// https://jsfiddle.net/z7ms3u5r/
var ffmpeg *FFmpeg
type FFmpeg struct {
*process.PipedCmd
config.FFmpeg
}
func (f *FFmpeg) getCmd() string {
return f.Commands[config.StreamInputVideoAudio]
}
func (f *FFmpeg) SetBitrate(b int) {
f.Bitrate = b
if f.Bitrate < 0 {
f.Bitrate = 6000
}
}
func (f *FFmpeg) SetFPS(fps int) {
f.FPS = fps
if f.FPS < 0 {
f.FPS = 30
} else if f.FPS > 60 {
f.FPS = 60
}
}
func (f *FFmpeg) SetResolution(height int) {
f.Height = height
if f.Height <= 0 {
f.Height = 1080
} else if f.Height > 2060 {
f.Height = 2060
}
}
func (f *FFmpeg) SetGOP(gop int) {
f.GOP = gop
if f.GOP < 0 {
f.GOP = 2
}
}
func (f *FFmpeg) ApplyOptions() {
f.ChangeCmd(f.FormatCmd(f.getCmd()))
}
func InitFFmpeg() *FFmpeg {
cfg := config.Get().Video
cmd := cfg.Commands[config.StreamInputVideoAudio]
ffmpeg = &FFmpeg{
PipedCmd: process.InitPipedCmd(cfg.FormatCmd(cmd)),
FFmpeg: cfg,
}
return ffmpeg
}
func GetFFmpeg() *FFmpeg {
return ffmpeg
}

132
external/process/extprocess.go vendored Normal file
View File

@@ -0,0 +1,132 @@
package process
import (
"io"
"os"
"os/exec"
"sync"
log "github.com/sirupsen/logrus"
)
type ExtProcess struct {
path string
cmd *exec.Cmd
mu sync.Mutex
args []string
running bool
stopChan chan struct{}
finished chan struct{}
stdin io.WriteCloser
}
func Init(path string, args []string) *ExtProcess {
return &ExtProcess{
args: args,
path: path,
}
}
func (u *ExtProcess) Start() {
u.mu.Lock()
defer u.mu.Unlock()
if u.running {
log.Debug("process is already running.")
return
}
u.stopChan = make(chan struct{})
u.finished = make(chan struct{})
log.Debugf("Starting external process: %s %v", u.path, u.args)
u.cmd = exec.Command(u.path, u.args...)
u.cmd.Stdout = os.Stdout
u.cmd.Stderr = os.Stderr
stdin, err := u.cmd.StdinPipe()
if err != nil {
log.Errorf("Couldn't handle stdin pipe: %v", err)
return
}
u.stdin = stdin
err = u.cmd.Start()
if err != nil {
log.Errorf("Failed to start process: %v", err)
}
u.running = true
go func() {
err = u.cmd.Wait()
u.running = false
log.Errorf("process exited with error: %v", err)
// planned stop
if err == nil {
u.finished <- struct{}{}
close(u.stopChan)
return
}
u.stopChan <- struct{}{}
close(u.finished)
}()
}
// Stop the ustreamer application
func (u *ExtProcess) Stop() {
u.mu.Lock()
defer u.mu.Unlock()
if !u.running {
log.Debug("process is not running.")
return
}
log.Warn("Stopping process...")
_, err := u.stdin.Write([]byte("q")) // TODO: works for ffmpeg, should be general or move into FFmpeg struct
if err != nil {
log.Errorf("Failed to stop process: %v", err)
if err := u.cmd.Process.Kill(); err != nil {
log.Errorf("Failed to kill process: %v", err)
}
}
if err := u.stdin.Close(); err != nil {
log.Errorf("Failed to close stdin: %v", err)
}
select {
case <-u.finished:
log.Info("stopped as expected")
case <-u.stopChan:
log.Info("was killed")
}
u.running = false
}
func (u *ExtProcess) ChangeArgs(newArgs []string) {
u.mu.Lock()
defer u.mu.Unlock()
u.args = newArgs
log.Printf("Updated process arguments: %v", u.args)
}
func (u *ExtProcess) Watch() {
for {
select {
case <-u.stopChan:
log.Errorf("process stopped unexpectedly. Restarting...")
u.Start()
/*case <-time.After(1 * time.Second): // Adjust the monitoring interval as needed
if !u.running {
log.Errorf("process is not running. Restarting...")
u.Start()
}*/
}
}
}

136
external/process/pipedprocess.go vendored Normal file
View File

@@ -0,0 +1,136 @@
package process
import (
"os"
"os/exec"
"strings"
"sync"
"syscall"
log "github.com/sirupsen/logrus"
)
// PipedCmd struct manages a sequence of piped commands.
type PipedCmd struct {
cmds []*exec.Cmd
mu sync.Mutex
running bool
}
// InitPipedCmd initializes a PipedCmd instance with a sequence of commands.
func InitPipedCmd(cmd string) *PipedCmd {
pipedCmd := &PipedCmd{}
pipedCmd.ChangeCmd(cmd)
return pipedCmd
}
func (p *PipedCmd) ChangeCmd(cmd string) {
cmds := strings.Split(cmd, "|")
for i, c := range cmds {
cmds[i] = strings.TrimSpace(c)
}
log.Debugf("Cmds: %+v", cmds)
pipedCmds := make([]*exec.Cmd, len(cmds))
// Initialize each command in the sequence
for i, cmd := range cmds {
if len(cmd) == 0 {
continue
}
cmdArgs := strings.Split(cmd, " ")
pipedCmds[i] = exec.Command(cmdArgs[0], cmdArgs[1:]...)
}
p.cmds = pipedCmds
}
// Start begins execution of all commands in the piped sequence.
func (p *PipedCmd) Start() error {
p.mu.Lock()
defer p.mu.Unlock()
if p.running {
log.Debugf("Process is already running.")
return nil
}
// Pipe each command's output to the next command's input
for i := 0; i < len(p.cmds)-1; i++ {
stdout, err := p.cmds[i].StdoutPipe()
if err != nil {
return err
}
p.cmds[i+1].Stdin = stdout
}
// pipe stdout and stderr of the last command into os
p.cmds[len(p.cmds)-1].Stdout = os.Stdout
p.cmds[len(p.cmds)-1].Stderr = os.Stderr
// Start each command in the sequence
for _, cmd := range p.cmds {
if err := cmd.Start(); err != nil {
p.terminateAll()
return err
} else {
log.Debugf("Started process: (%d) %v", cmd.Process.Pid, cmd.Args)
}
}
p.running = true
// Monitor commands in a goroutine to handle failures
go p.monitorCommands()
return nil
}
// monitorCommands waits for each command to finish and checks for errors.
func (p *PipedCmd) monitorCommands() {
var wg sync.WaitGroup
wg.Add(len(p.cmds))
for _, cmd := range p.cmds {
go func(cmd *exec.Cmd) {
defer wg.Done()
err := cmd.Wait()
if err != nil && err.Error() != "signal: terminated" {
log.Debugf("Command failed: %v", err)
p.terminateAll() // Terminate all if any command fails
}
}(cmd)
}
// Wait for all commands to complete or terminate
wg.Wait()
p.mu.Lock()
defer p.mu.Unlock()
p.running = false
}
// terminateAll sends a termination signal to all running commands.
func (p *PipedCmd) terminateAll() {
for _, cmd := range p.cmds {
if cmd.Process != nil {
log.Debugf("Sending SIGTERM to: (%d) %v", cmd.Process.Pid, cmd.Args)
_ = cmd.Process.Signal(syscall.SIGTERM) // Send SIGTERM to allow graceful termination
}
}
p.running = false
}
// Stop manually stops all commands in the sequence.
func (p *PipedCmd) Stop() {
p.mu.Lock()
defer p.mu.Unlock()
if !p.running {
log.Debug("Process is not running.")
return
}
log.Debug("Stopping process...")
p.terminateAll()
}