2019-11-05 03:27:12 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
webcam.go provides an implementation of AVDevice for webcams.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
option) any later version.
|
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
package webcam
|
2019-11-05 03:27:12 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2019-11-05 04:26:34 +03:00
|
|
|
"fmt"
|
2019-11-05 03:27:12 +03:00
|
|
|
"io"
|
2019-12-23 07:57:46 +03:00
|
|
|
"io/ioutil"
|
2019-11-05 04:26:34 +03:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
|
|
|
|
2019-12-03 04:35:57 +03:00
|
|
|
"bitbucket.org/ausocean/av/codec/codecutil"
|
2019-11-06 09:57:10 +03:00
|
|
|
"bitbucket.org/ausocean/av/device"
|
|
|
|
"bitbucket.org/ausocean/av/revid/config"
|
2019-11-05 04:26:34 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-11-05 03:27:12 +03:00
|
|
|
)
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
// Used to indicate package in logging.
|
|
|
|
const pkg = "webcam: "
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Configuration defaults.
|
2019-11-05 03:27:12 +03:00
|
|
|
const (
|
2019-11-06 09:57:10 +03:00
|
|
|
defaultInputPath = "/dev/video0"
|
|
|
|
defaultFrameRate = 25
|
|
|
|
defaultBitrate = 400
|
|
|
|
defaultWidth = 1280
|
|
|
|
defaultHeight = 720
|
2019-11-05 03:27:12 +03:00
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Configuration field errors.
|
2019-11-05 03:27:12 +03:00
|
|
|
var (
|
2019-11-06 09:57:10 +03:00
|
|
|
errBadFrameRate = errors.New("frame rate bad or unset, defaulting")
|
|
|
|
errBadBitrate = errors.New("bitrate bad or unset, defaulting")
|
|
|
|
errBadWidth = errors.New("width bad or unset, defaulting")
|
|
|
|
errBadHeight = errors.New("height bad or unset, defaulting")
|
2019-11-13 04:16:01 +03:00
|
|
|
errBadInputPath = errors.New("input path bad or unset, defaulting")
|
2019-11-05 03:27:12 +03:00
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Webcam is an implementation of the AVDevice interface for a Webcam. Webcam
|
|
|
|
// uses an ffmpeg process to pipe the video data from the webcam.
|
2019-11-05 03:27:12 +03:00
|
|
|
type Webcam struct {
|
2019-12-23 07:57:46 +03:00
|
|
|
out io.ReadCloser
|
|
|
|
log config.Logger
|
|
|
|
cfg config.Config
|
|
|
|
cmd *exec.Cmd
|
|
|
|
done chan struct{}
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 08:34:07 +03:00
|
|
|
// New returns a new Webcam.
|
|
|
|
func New(l config.Logger) *Webcam {
|
2019-12-23 07:57:46 +03:00
|
|
|
return &Webcam{
|
|
|
|
log: l,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 03:25:13 +03:00
|
|
|
// Name returns the name of the device.
|
|
|
|
func (w *Webcam) Name() string {
|
|
|
|
return "Webcam"
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Set will validate the relevant fields of the given Config struct and assign
|
|
|
|
// the struct to the Webcam's Config. If fields are not valid, an error is
|
|
|
|
// added to the multiError and a default value is used.
|
2019-11-06 09:57:10 +03:00
|
|
|
func (w *Webcam) Set(c config.Config) error {
|
|
|
|
var errs device.MultiError
|
2019-11-13 04:16:01 +03:00
|
|
|
if c.InputPath == "" {
|
|
|
|
const defaultInputPath = "/dev/video0"
|
|
|
|
errs = append(errs, errBadInputPath)
|
|
|
|
c.InputPath = defaultInputPath
|
|
|
|
}
|
|
|
|
|
2019-11-05 03:27:12 +03:00
|
|
|
if c.Width == 0 {
|
|
|
|
errs = append(errs, errBadWidth)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.Width = defaultWidth
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Height == 0 {
|
|
|
|
errs = append(errs, errBadHeight)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.Height = defaultHeight
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.FrameRate == 0 {
|
|
|
|
errs = append(errs, errBadFrameRate)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.FrameRate = defaultFrameRate
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Bitrate <= 0 {
|
|
|
|
errs = append(errs, errBadBitrate)
|
2019-11-06 09:57:10 +03:00
|
|
|
c.Bitrate = defaultBitrate
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
|
|
|
w.cfg = c
|
2019-11-06 09:57:10 +03:00
|
|
|
return errs
|
2019-11-05 03:27:12 +03:00
|
|
|
}
|
2019-11-05 04:26:34 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Start will build the required arguments for ffmpeg and then execute the
|
|
|
|
// command, piping video output where we can read using the Read method.
|
2019-11-05 04:26:34 +03:00
|
|
|
func (w *Webcam) Start() error {
|
2019-12-03 07:06:16 +03:00
|
|
|
br := w.cfg.Bitrate * 1000
|
|
|
|
|
2019-11-05 04:26:34 +03:00
|
|
|
args := []string{
|
|
|
|
"-i", w.cfg.InputPath,
|
|
|
|
"-r", fmt.Sprint(w.cfg.FrameRate),
|
2019-12-03 07:06:16 +03:00
|
|
|
"-b:v", fmt.Sprint(br),
|
|
|
|
"-s", fmt.Sprintf("%dx%d", w.cfg.Width, w.cfg.Height),
|
2019-12-03 07:25:08 +03:00
|
|
|
}
|
2019-11-05 04:26:34 +03:00
|
|
|
|
2019-12-03 04:35:57 +03:00
|
|
|
switch w.cfg.InputCodec {
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("revid: invalid input codec: %v", w.cfg.InputCodec)
|
|
|
|
case codecutil.H264:
|
|
|
|
args = append(args,
|
|
|
|
"-f", "h264",
|
2019-12-03 07:06:16 +03:00
|
|
|
"-maxrate", fmt.Sprint(br),
|
|
|
|
"-bufsize", fmt.Sprint(br/2),
|
2019-12-03 04:35:57 +03:00
|
|
|
)
|
|
|
|
case codecutil.MJPEG:
|
|
|
|
args = append(args,
|
|
|
|
"-f", "mjpeg",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-11-05 04:26:34 +03:00
|
|
|
args = append(args,
|
|
|
|
"-",
|
|
|
|
)
|
|
|
|
|
|
|
|
w.log.Log(logger.Info, pkg+"ffmpeg args", "args", strings.Join(args, " "))
|
|
|
|
w.cmd = exec.Command("ffmpeg", args...)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
w.out, err = w.cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to create pipe: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-12-23 07:57:46 +03:00
|
|
|
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:
|
2019-12-23 08:07:20 +03:00
|
|
|
w.cfg.Logger.Log(logger.Info, "webcam.Stop() called, finished checking stderr")
|
2019-12-23 07:57:46 +03:00
|
|
|
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 {
|
2019-12-23 08:07:20 +03:00
|
|
|
w.cfg.Logger.Log(logger.Error, "error from webcam stderr", "error", string(buf))
|
2019-12-23 07:57:46 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-05 04:26:34 +03:00
|
|
|
err = w.cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to start ffmpeg: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Stop will kill the ffmpeg process and close the output pipe.
|
2019-11-05 04:26:34 +03:00
|
|
|
func (w *Webcam) Stop() error {
|
2019-12-23 07:57:46 +03:00
|
|
|
close(w.done)
|
2019-11-05 04:26:34 +03:00
|
|
|
if w.cmd == nil || w.cmd.Process == nil {
|
2019-11-06 04:01:12 +03:00
|
|
|
return errors.New("ffmpeg process was never started")
|
2019-11-05 04:26:34 +03:00
|
|
|
}
|
|
|
|
err := w.cmd.Process.Kill()
|
|
|
|
if err != nil {
|
2019-11-06 04:01:12 +03:00
|
|
|
return fmt.Errorf("could not kill ffmpeg process: %w", err)
|
2019-11-05 04:26:34 +03:00
|
|
|
}
|
|
|
|
return w.out.Close()
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Read implements io.Reader. If the pipe is nil a read error is returned.
|
2019-11-05 04:26:34 +03:00
|
|
|
func (w *Webcam) Read(p []byte) (int, error) {
|
2019-11-05 12:44:04 +03:00
|
|
|
if w.out != nil {
|
|
|
|
return w.out.Read(p)
|
|
|
|
}
|
|
|
|
return 0, errors.New("webcam not streaming")
|
2019-11-05 04:26:34 +03:00
|
|
|
}
|