Merged in raspivid-stderr (pull request #299)

device/raspivid/raspivid.go: piping stderr and checking for errors

Approved-by: Saxon Milton <saxon.milton@gmail.com>
This commit is contained in:
Ella Pietraroia 2019-12-05 01:05:20 +00:00 committed by Saxon Milton
commit f8da6b0609
1 changed files with 39 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"
@ -106,14 +107,20 @@ var AutoWhiteBalanceModes = [...]string{
// Raspivid is an implementation of AVDevice that provides control over the // Raspivid is an implementation of AVDevice that provides control over the
// raspivid command to allow reading of data from a Raspberry Pi camera. // raspivid command to allow reading of data from a Raspberry Pi camera.
type Raspivid struct { type Raspivid struct {
cfg config.Config cfg config.Config
cmd *exec.Cmd cmd *exec.Cmd
out io.ReadCloser out io.ReadCloser
log config.Logger log config.Logger
done chan struct{}
} }
// New returns a new Raspivid. // New returns a new Raspivid.
func New(l config.Logger) *Raspivid { return &Raspivid{log: l} } func New(l config.Logger) *Raspivid {
return &Raspivid{
log: l,
done: make(chan struct{}),
}
}
// Name returns the name of the device. // Name returns the name of the device.
func (r *Raspivid) Name() string { func (r *Raspivid) Name() string {
@ -250,6 +257,32 @@ func (r *Raspivid) Start() error {
return fmt.Errorf("could not pipe command output: %w", err) return fmt.Errorf("could not pipe command output: %w", err)
} }
stderr, err := r.cmd.StderrPipe()
if err != nil {
return fmt.Errorf("could not pipe command error: %w", err)
}
go func() {
for {
select {
case <-r.done:
r.cfg.Logger.Log(logger.Info, "raspivid.Stop() called, finished checking stderr")
return
default:
buf, err := ioutil.ReadAll(stderr)
if err != nil {
r.cfg.Logger.Log(logger.Error, "could not read stderr", "error", err)
return
}
if len(buf) != 0 {
r.cfg.Logger.Log(logger.Error, "error from raspivid stderr", "error", string(buf))
return
}
}
}
}()
err = r.cmd.Start() err = r.cmd.Start()
if err != nil { if err != nil {
return fmt.Errorf("could not start raspivid command: %w", err) return fmt.Errorf("could not start raspivid command: %w", err)
@ -269,6 +302,7 @@ func (r *Raspivid) Read(p []byte) (int, error) {
// Stop will terminate the raspivid process and close the output pipe. // Stop will terminate the raspivid process and close the output pipe.
func (r *Raspivid) Stop() error { func (r *Raspivid) Stop() error {
close(r.done)
if r.cmd == nil || r.cmd.Process == nil { if r.cmd == nil || r.cmd.Process == nil {
return errors.New("raspivid process was never started") return errors.New("raspivid process was never started")
} }