device/webcam: implement IsRunning method for webcam

This commit is contained in:
Scott 2020-01-30 11:21:39 +10:30
parent f1d1fe2cad
commit 5c5672486e
1 changed files with 15 additions and 8 deletions

View File

@ -62,11 +62,12 @@ 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{} done chan struct{}
isRunning bool
} }
// New returns a new Webcam. // New returns a new Webcam.
@ -161,6 +162,8 @@ func (w *Webcam) Start() error {
return fmt.Errorf("could not pipe command error: %w", err) return fmt.Errorf("could not pipe command error: %w", err)
} }
w.isRunning = true
go func() { go func() {
for { for {
select { select {
@ -202,7 +205,12 @@ func (w *Webcam) Stop() error {
if err != nil { if err != nil {
return fmt.Errorf("could not kill ffmpeg process: %w", err) return fmt.Errorf("could not kill ffmpeg process: %w", err)
} }
return w.out.Close() err = w.out.Close()
if err == nil {
w.isRunning = false
return nil
}
return err
} }
// Read implements io.Reader. If the pipe is nil a read error is returned. // Read implements io.Reader. If the pipe is nil a read error is returned.
@ -215,6 +223,5 @@ func (w *Webcam) Read(p []byte) (int, error) {
// IsRunning is used to determine if the webcam is running. // IsRunning is used to determine if the webcam is running.
func (w *Webcam) IsRunning() bool { func (w *Webcam) IsRunning() bool {
panic("not implemented") return w.isRunning
return false
} }