mirror of https://bitbucket.org/ausocean/av.git
Merged in webcam-stderr (pull request #311)
stderr implementation in webcam Approved-by: Saxon Milton <saxon.milton@gmail.com>
This commit is contained in:
commit
016bbf0553
|
@ -28,6 +28,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"io/ioutil"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -61,15 +62,19 @@ var (
|
||||||
// Webcam is an implementation of the AVDevice interface for a Webcam. Webcam
|
// Webcam is an implementation of the AVDevice interface for a Webcam. Webcam
|
||||||
// uses an ffmpeg process to pipe the video data from the webcam.
|
// uses an ffmpeg process to pipe the video data from the webcam.
|
||||||
type Webcam struct {
|
type Webcam struct {
|
||||||
out io.ReadCloser
|
out io.ReadCloser
|
||||||
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, "webcam.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 webcam 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")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue