stderr implementation in webcam

same stderr messages made avaliable in raspivid.go now in webcam, so can see when there are problems with with the webcam, rather then our code
This commit is contained in:
Ella Pietraroia 2019-12-23 15:27:46 +10:30
parent 476deedf2b
commit d0102779ed
1 changed files with 37 additions and 5 deletions

View File

@ -28,6 +28,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"os/exec" "os/exec"
"strings" "strings"
@ -65,11 +66,15 @@ type Webcam struct {
log config.Logger log config.Logger
cfg config.Config cfg config.Config
cmd *exec.Cmd cmd *exec.Cmd
done chan struct{}
} }
// New returns a new Webcam. // New returns a new Webcam.
func New(l config.Logger) *Webcam { func New(l config.Logger) *Webcam {
return &Webcam{log: l} return &Webcam{
log: l,
done: make(chan struct{}),
}
} }
// Name returns the name of the device. // Name returns the name of the device.
@ -151,6 +156,32 @@ func (w *Webcam) Start() error {
return fmt.Errorf("failed to create pipe: %w", err) return fmt.Errorf("failed to create pipe: %w", err)
} }
stderr, err := w.cmd.StderrPipe()
if err != nil {
return fmt.Errorf("could not pipe command error: %w", err)
}
go func() {
for {
select {
case <-w.done:
w.cfg.Logger.Log(logger.Info, "raspivid.Stop() called, finished checking stderr")
return
default:
buf, err := ioutil.ReadAll(stderr)
if err != nil {
w.cfg.Logger.Log(logger.Error, "could not read stderr", "error", err)
return
}
if len(buf) != 0 {
w.cfg.Logger.Log(logger.Error, "error from raspivid stderr", "error", string(buf))
return
}
}
}
}()
err = w.cmd.Start() err = w.cmd.Start()
if err != nil { if err != nil {
return fmt.Errorf("failed to start ffmpeg: %w", err) return fmt.Errorf("failed to start ffmpeg: %w", err)
@ -161,6 +192,7 @@ func (w *Webcam) Start() error {
// Stop will kill the ffmpeg process and close the output pipe. // Stop will kill the ffmpeg process and close the output pipe.
func (w *Webcam) Stop() error { func (w *Webcam) Stop() error {
close(w.done)
if w.cmd == nil || w.cmd.Process == nil { if w.cmd == nil || w.cmd.Process == nil {
return errors.New("ffmpeg process was never started") return errors.New("ffmpeg process was never started")
} }