add: run video in pipe

This commit is contained in:
Charles Hathaway
2023-10-01 20:38:38 -07:00
parent 19bb6c49b4
commit a07a993bab
8 changed files with 570 additions and 141 deletions
+159
View File
@@ -0,0 +1,159 @@
// Package pipespy provides a structure which connects multiple things, forwarding data, but also
// calling a helper in the middle to spy.
package pipespy
import (
"errors"
"fmt"
"io"
"os/exec"
"sync"
"github.com/rs/zerolog/log"
)
type process interface {
// Sets the stdin.
SetStdin(io.Reader)
// Sets the stdout.
SetStdout(io.Writer)
// Start the process; should block until the process completes, then free Stdin and Stdout.
Run() error
}
type CmdWrap struct {
cmd *exec.Cmd
}
func NewCmd(cmd *exec.Cmd) *CmdWrap {
return &CmdWrap{
cmd: cmd,
}
}
func (s *CmdWrap) SetStdin(r io.Reader) {
s.cmd.Stdin = r
}
func (s *CmdWrap) SetStdout(w io.Writer) {
s.cmd.Stdout = w
}
func (s *CmdWrap) Run() error {
if err := s.cmd.Start(); err != nil {
return fmt.Errorf("failed to start command: %w", err)
}
return s.cmd.Wait()
}
type wrap struct {
proc process
writeCloser io.WriteCloser
}
func (s *wrap) SetStdin(r io.ReadCloser) {
s.proc.SetStdin(r)
}
func (s *wrap) SetStdout(w io.WriteCloser) {
s.writeCloser = w
s.proc.SetStdout(w)
}
func (s *wrap) Run() error {
defer s.writeCloser.Close()
return s.proc.Run()
}
type Pipe struct {
processOrder []*Spy
}
func New() *Pipe {
return &Pipe{}
}
// Add a process to the pipeline. Use the returned spy object to snoop on it.
// Snoop must be called before Start; will panic otherwise.
func (p *Pipe) Add(proc process) *Spy {
w := &wrap{
proc: proc,
}
piperReader, pipeWriter := io.Pipe()
w.SetStdout(pipeWriter)
spy := &Spy{
stdIn: piperReader,
proc: w,
}
p.processOrder = append(p.processOrder, spy)
if l := len(p.processOrder) - 1; l > 0 {
p.processOrder[l].proc.SetStdin(p.processOrder[l-1].Snoop())
}
return spy
}
// Starts the pipeline; there is a Go routine created to ensure readers dont stall.
// Make sure to kill the processes, which will cause EOF to propegate. The returned
// function can be used to wait for all to cleanly exit.
func (p *Pipe) Start() func() []error {
errorCh := make(chan error, len(p.processOrder))
wg := sync.WaitGroup{}
wg.Add(len(p.processOrder))
for _, s := range p.processOrder {
s.running = true
go func(s *Spy) {
defer wg.Done()
buff := make([]byte, 128)
var err error
for n := 1; err == nil || n == 0; _, err = s.stdIn.Read(buff) {
// do nothing
}
if !errors.Is(err, io.EOF) {
log.Warn().Err(err).Msg("unexpected error from reader")
}
}(s)
go func(s *Spy) {
if err := s.proc.Run(); err != nil {
errorCh <- err
}
// will this cause some writes to get lost? is that a problem?
for _, closer := range s.closers {
if err := closer(); err != nil {
log.Warn().Err(err).Msg("error closing pipe")
}
}
}(s)
}
return func() []error {
wg.Wait()
close(errorCh)
var errs []error
for err := range errorCh {
errs = append(errs, err)
}
return errs
}
}
type Spy struct {
stdIn io.Reader
closers []func() error
proc *wrap
running bool
}
// Snoop on the Stdout of the process being spied; reader will recieve a copy of the bytes.
func (s *Spy) Snoop() io.ReadCloser {
if s.running {
panic("Call to snoop on a running process")
}
pipeReader, pipeWriter := io.Pipe()
s.closers = append(s.closers, pipeWriter.Close)
r := io.TeeReader(s.stdIn, pipeWriter)
s.stdIn = r
return pipeReader
}
+115
View File
@@ -0,0 +1,115 @@
package pipespy_test
import (
"errors"
"fmt"
"io"
"sync"
"testing"
"github.com/chathaway-codes/home-sensors/v2/internal/pipespy"
"github.com/google/go-cmp/cmp"
)
func TestPipeSpy(t *testing.T) {
pipe := pipespy.New()
readPipe, writerPipe := io.Pipe()
stage1 := &simpleWriter{t: t, stdIn: readPipe}
stage2 := &simpleWriter{t: t}
stdOut1 := pipe.Add(stage1).Snoop()
stdOut2 := pipe.Add(stage2).Snoop()
pipe.Add(&simpleWriter{t: t})
// Spawn threads to keep reading the snoop
var buff1, buff2 []byte
var err1, err2 error
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
buff1, err1 = io.ReadAll(stdOut1)
if errors.Is(err1, io.EOF) {
err1 = nil
}
}()
go func() {
defer wg.Done()
buff2, err2 = io.ReadAll(stdOut2)
if errors.Is(err2, io.EOF) {
err2 = nil
}
}()
wait := pipe.Start()
msg := "This is a message!"
fmt.Fprint(writerPipe, msg)
if err := writerPipe.Close(); err != nil {
t.Fatalf("failed to close pipe: %v", err)
}
wait()
wg.Wait()
if err1 != nil || err2 != nil {
t.Errorf("Got errs %v and %v; want nil or EOF", err1, err2)
}
if diff := cmp.Diff(string(buff1), msg); diff != "" {
t.Errorf("got diff:\n%s", diff)
}
if diff := cmp.Diff(string(buff2), msg); diff != "" {
t.Errorf("got diff:\n%s", diff)
}
}
func TestPipeSpyError(t *testing.T) {
readPipe, writerPipe := io.Pipe()
pipe := pipespy.New()
pipe.Add(&simpleWriter{t: t, stdIn: readPipe})
pipe.Add(&simpleWriter{t: t, err: fmt.Errorf("this is an error")})
pipe.Add(&simpleWriter{t: t})
wait := pipe.Start()
if _, err := fmt.Fprintf(writerPipe, "Hello"); err != nil {
t.Fatalf("error: %v", err)
}
writerPipe.Close()
errs := wait()
if len(errs) != 1 {
t.Errorf("Expected 1 err; got %d", len(errs))
}
}
type simpleWriter struct {
stdIn io.Reader
stdOut io.Writer
gotData []byte
err error
t *testing.T
}
func (s *simpleWriter) SetStdin(r io.Reader) {
s.stdIn = r
}
func (s *simpleWriter) SetStdout(w io.Writer) {
s.stdOut = w
}
func (s *simpleWriter) Run() error {
if s.err != nil {
return s.err
}
bytes, err := io.ReadAll(s.stdIn)
if err != nil && !errors.Is(err, io.EOF) {
s.t.Errorf("Unexpected err: %v", err)
}
s.gotData = bytes
s.stdOut.Write(bytes)
return nil
}
+170
View File
@@ -0,0 +1,170 @@
// Package video implements logic to stream video from a config.
package video
import (
"bufio"
"context"
"errors"
"io"
"os/exec"
"sync"
"github.com/chathaway-codes/home-sensors/v2/internal/pipespy"
"github.com/chathaway-codes/home-sensors/v2/internal/watcher/config"
"github.com/google/uuid"
"github.com/pion/webrtc/v3"
"github.com/pion/webrtc/v3/pkg/media/ivfreader"
"github.com/rs/zerolog/log"
)
var Default = &Mod{}
type Video struct {
mu sync.Mutex
h264Cmd, ivfCmd *exec.Cmd
ivfListeners map[string]chan<- []byte
ivfCodecReady chan struct{}
ivfCodec string
cancelFunc func()
ctx context.Context
}
func New(cfg *config.Config) (*Video, error) {
ctx, cancelFunc := context.WithCancel(context.Background())
// Setup commands
h264cmd := exec.CommandContext(ctx, cfg.H264Cmd.Binary, cfg.H264Cmd.Arguments...)
ivfCmd := exec.CommandContext(ctx, cfg.IVFCmd.Binary, cfg.IVFCmd.Arguments...)
return &Video{
h264Cmd: h264cmd,
ivfCmd: ivfCmd,
ivfListeners: make(map[string]chan<- []byte),
ivfCodecReady: make(chan struct{}),
cancelFunc: cancelFunc,
ctx: ctx,
}, nil
}
// Run launches the commands and begins creating data. It will block until Done is called.
func (v *Video) Run() {
pipe := pipespy.New()
pipe.Add(pipespy.NewCmd(v.h264Cmd))
ivfSnoop := pipe.Add(pipespy.NewCmd(v.ivfCmd)).Snoop()
log.Info().Str("cmd", v.h264Cmd.String()).Msg("h264 command")
log.Info().Str("cmd", v.ivfCmd.String()).Msg("ivf command")
// Log stderr if it appears
go logToStdErr("h264", &v.h264Cmd.Stderr)
go logToStdErr("ivf", &v.ivfCmd.Stderr)
cleanUp := pipe.Start()
defer func() {
errs := cleanUp()
for _, err := range errs {
log.Err(err).Send()
}
}()
go func(r io.Reader) {
ivf, header, err := ivfreader.NewWith(r)
if err != nil {
log.Error().Err(err).Msg("failed to create ivfreader")
return
}
// Determine video codec
var trackCodec string
switch header.FourCC {
case "AV01":
trackCodec = webrtc.MimeTypeAV1
case "VP90":
trackCodec = webrtc.MimeTypeVP9
case "VP80":
trackCodec = webrtc.MimeTypeVP8
default:
log.Error().Err(err).Str("fourcc", header.FourCC).Msg("unable to handle FourCC")
}
v.mu.Lock()
v.ivfCodec = trackCodec
close(v.ivfCodecReady)
v.mu.Unlock()
log.Info().Msgf("starting to stream IVF with codec %q", trackCodec)
for {
select {
case <-v.ctx.Done():
// Exit cleanly
return
default:
// do nothing
}
frame, _, ivfErr := ivf.ParseNextFrame()
if errors.Is(ivfErr, io.EOF) {
log.Debug().Msg("all video frames parsed and sent")
return
}
if ivfErr != nil {
log.Error().Err(err).Msg("failed to parse frame")
}
v.mu.Lock()
for _, lis := range v.ivfListeners {
lis <- frame
}
v.mu.Unlock()
}
}(ivfSnoop)
log.Info().Msg("video streams started")
<-v.ctx.Done()
}
// Join will connect to a running stream; note, it will block
// until the ivf codec is decided. Make sure to call Run first.
func (v *Video) Join() (<-chan []byte, string, func()) {
<-v.ivfCodecReady
v.mu.Lock()
defer v.mu.Unlock()
myID := uuid.New().String()
ch := make(chan []byte)
v.ivfListeners[myID] = ch
return ch, v.ivfCodec, func() {
v.mu.Lock()
defer v.mu.Unlock()
delete(v.ivfListeners, myID)
}
}
func logToStdErr(name string, w *io.Writer) {
pipeReader, pipeWriter := io.Pipe()
*w = pipeWriter
lineReader := bufio.NewScanner(pipeReader)
for lineReader.Scan() {
log.Info().Str("video", name).Str("stderr", lineReader.Text()).Send()
}
}
// Done stops the processing.
func (v *Video) Done() {
v.cancelFunc()
}
type Mod struct{}
func (m *Mod) Get() (*Video, error) {
cfg, err := config.Default.Get()
if err != nil {
return nil, err
}
return New(cfg)
}
+57
View File
@@ -0,0 +1,57 @@
// Package config handles loading the config.
package config
import (
"flag"
"fmt"
"os"
"gopkg.in/yaml.v3"
)
var Default *Mod
type Cmd struct {
Binary string
Arguments []string
}
type Config struct {
H264Cmd *Cmd `yaml:"h264"`
IVFCmd *Cmd `yaml:"ivf"`
}
func New(source []byte) (*Config, error) {
config := &Config{}
if err := yaml.Unmarshal(source, config); err != nil {
return nil, fmt.Errorf("failed to parse config: %w", err)
}
return config, nil
}
type Mod struct {
filePath string
}
func (m *Mod) RegisterFlags(fs *flag.FlagSet) {
fs.StringVar(&m.filePath, "watcher_config", "", "path to the watcher configuration")
}
func (m *Mod) Get() (*Config, error) {
bytes, err := os.ReadFile(m.filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file %q: %w", m.filePath, err)
}
config, err := New(bytes)
if err != nil {
return nil, err
}
return config, nil
}
func init() {
Default = &Mod{}
Default.RegisterFlags(flag.CommandLine)
}