Files
groundstation/cmd/groundstation/video.go
T
piotr 400b6efef7
Build Ground Station / build (push) Successful in 19s
feat: gate video and terminal logs on AUX2 state
2026-09-29 15:23:37 +02:00

57 lines
1.1 KiB
Go

package main
import (
"fmt"
"os"
"os/exec"
"sync"
)
// videoController owns the optional fpv-video launcher. It never touches WFB
// directly; fpv-video remains the sole owner of the video receiver lifecycle.
type videoController struct {
command string
mu sync.Mutex
cmd *exec.Cmd
done chan error
}
func newVideoController(command string) *videoController {
return &videoController{command: command}
}
func (v *videoController) Set(active bool) error {
v.mu.Lock()
defer v.mu.Unlock()
if active {
if v.cmd != nil {
return nil
}
v.cmd = exec.Command(v.command)
v.cmd.Stdout, v.cmd.Stderr = os.Stdout, os.Stderr
if err := v.cmd.Start(); err != nil {
v.cmd = nil
return fmt.Errorf("start %s: %w", v.command, err)
}
v.done = make(chan error, 1)
cmd := v.cmd
go func() { v.done <- cmd.Wait() }()
return nil
}
if v.cmd == nil {
return nil
}
if err := v.cmd.Process.Signal(os.Interrupt); err != nil {
return fmt.Errorf("stop %s: %w", v.command, err)
}
<-v.done
v.cmd, v.done = nil, nil
return nil
}
func (v *videoController) Stop() error {
return v.Set(false)
}