2019-11-01 14:15:46 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
|
|
|
raspivid.go provides an implementation of the AVDevice interface for raspivid.
|
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
2019-11-04 11:57:05 +03:00
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
2019-11-01 14:15:46 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-03-12 08:26:29 +03:00
|
|
|
// Package raspivid provides an implementation of AVDevice for the raspberry
|
|
|
|
// pi camera.
|
2019-11-06 09:57:10 +03:00
|
|
|
package raspivid
|
2019-11-01 14:15:46 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2019-11-02 03:01:40 +03:00
|
|
|
"io"
|
2019-12-04 09:11:26 +03:00
|
|
|
"io/ioutil"
|
2019-11-02 03:01:40 +03:00
|
|
|
"os/exec"
|
2021-03-30 06:36:00 +03:00
|
|
|
"strconv"
|
2019-11-02 03:01:40 +03:00
|
|
|
"strings"
|
2019-11-01 14:15:46 +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-02 03:01:40 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
2019-11-22 09:49:27 +03:00
|
|
|
"bitbucket.org/ausocean/utils/sliceutils"
|
2019-11-01 14:15:46 +03:00
|
|
|
)
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
// To indicate package when logging.
|
|
|
|
const pkg = "raspivid: "
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Raspivid configuration defaults.
|
2019-11-01 14:15:46 +03:00
|
|
|
const (
|
2019-11-05 12:44:04 +03:00
|
|
|
defaultRaspividCodec = codecutil.H264
|
|
|
|
defaultRaspividRotation = 0
|
|
|
|
defaultRaspividWidth = 1280
|
|
|
|
defaultRaspividHeight = 720
|
|
|
|
defaultRaspividBrightness = 50
|
|
|
|
defaultRaspividSaturation = 0
|
|
|
|
defaultRaspividExposure = "auto"
|
|
|
|
defaultRaspividAutoWhiteBalance = "auto"
|
|
|
|
defaultRaspividMinFrames = 100
|
|
|
|
defaultRaspividQuantization = 30
|
2020-01-07 03:56:17 +03:00
|
|
|
defaultRaspividBitrate = 4800
|
2019-11-05 12:44:04 +03:00
|
|
|
defaultRaspividFramerate = 25
|
2021-03-30 06:36:00 +03:00
|
|
|
defaultRaspividSharpness = 0
|
|
|
|
defaultRaspividContrast = 0
|
|
|
|
defaultRaspividISO = 100
|
|
|
|
defaultRaspividEV = 0
|
|
|
|
defaultRaspividAWBGains = "1.0,1.0"
|
2019-11-01 14:15:46 +03:00
|
|
|
)
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Configuration errors.
|
2019-11-01 14:15:46 +03:00
|
|
|
var (
|
|
|
|
errBadCodec = errors.New("codec bad or unset, defaulting")
|
|
|
|
errBadRotation = errors.New("rotation bad or unset, defaulting")
|
|
|
|
errBadWidth = errors.New("width bad or unset, defaulting")
|
|
|
|
errBadHeight = errors.New("height bad or unset, defaulting")
|
|
|
|
errBadFrameRate = errors.New("framerate bad or unset, defaulting")
|
|
|
|
errBadBitrate = errors.New("bitrate bad or unset, defaulting")
|
|
|
|
errBadMinFrames = errors.New("min frames bad or unset, defaulting")
|
|
|
|
errBadSaturation = errors.New("saturation bad or unset, defaulting")
|
|
|
|
errBadBrightness = errors.New("brightness bad or unset, defaulting")
|
|
|
|
errBadExposure = errors.New("exposure bad or unset, defaulting")
|
|
|
|
errBadAutoWhiteBalance = errors.New("auto white balance bad or unset, defaulting")
|
|
|
|
errBadQuantization = errors.New("quantization bad or unset, defaulting")
|
2021-03-30 06:36:00 +03:00
|
|
|
errBadAWBGains = errors.New("auto white balance gains bad or unset, defaulting")
|
|
|
|
errBadEV = errors.New("exposure value bad or unset, defaulting")
|
|
|
|
errBadContrast = errors.New("contrast bad or unset, defaulting")
|
|
|
|
errBadSharpness = errors.New("sharpness bad or unset, defaulting")
|
|
|
|
errBadISO = errors.New("iso bad or unset, defaulting")
|
2019-11-01 14:15:46 +03:00
|
|
|
)
|
|
|
|
|
2019-11-06 09:57:10 +03:00
|
|
|
// Possible modes for raspivid --exposure parameter.
|
|
|
|
var ExposureModes = [...]string{
|
|
|
|
"auto",
|
|
|
|
"night",
|
|
|
|
"nightpreview",
|
|
|
|
"backlight",
|
|
|
|
"spotlight",
|
|
|
|
"sports",
|
|
|
|
"snow",
|
|
|
|
"beach",
|
|
|
|
"verylong",
|
|
|
|
"fixedfps",
|
|
|
|
"antishake",
|
|
|
|
"fireworks",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Possible modes for raspivid --awb parameter.
|
|
|
|
var AutoWhiteBalanceModes = [...]string{
|
|
|
|
"off",
|
|
|
|
"auto",
|
|
|
|
"sun",
|
|
|
|
"cloud",
|
|
|
|
"shade",
|
|
|
|
"tungsten",
|
|
|
|
"fluorescent",
|
|
|
|
"incandescent",
|
|
|
|
"flash",
|
|
|
|
"horizon",
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Raspivid is an implementation of AVDevice that provides control over the
|
|
|
|
// raspivid command to allow reading of data from a Raspberry Pi camera.
|
2019-11-02 03:01:40 +03:00
|
|
|
type Raspivid struct {
|
2020-01-30 03:52:11 +03:00
|
|
|
cfg config.Config
|
|
|
|
cmd *exec.Cmd
|
|
|
|
out io.ReadCloser
|
|
|
|
log config.Logger
|
|
|
|
done chan struct{}
|
|
|
|
isRunning bool
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 08:34:07 +03:00
|
|
|
// New returns a new Raspivid.
|
2019-12-04 09:11:26 +03:00
|
|
|
func New(l config.Logger) *Raspivid {
|
|
|
|
return &Raspivid{
|
|
|
|
log: l,
|
|
|
|
done: make(chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 14:15:46 +03:00
|
|
|
|
2019-11-22 03:25:13 +03:00
|
|
|
// Name returns the name of the device.
|
|
|
|
func (r *Raspivid) Name() string {
|
|
|
|
return "Raspivid"
|
|
|
|
}
|
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Set will take a Config struct, check the validity of the relevant fields
|
|
|
|
// and then performs any configuration necessary. 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 (r *Raspivid) Set(c config.Config) error {
|
|
|
|
var errs device.MultiError
|
2019-11-01 14:15:46 +03:00
|
|
|
switch c.InputCodec {
|
|
|
|
case codecutil.H264, codecutil.MJPEG:
|
|
|
|
default:
|
2019-11-05 12:44:04 +03:00
|
|
|
c.InputCodec = defaultRaspividCodec
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadCodec)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Rotation > 359 {
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Rotation = defaultRaspividRotation
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadRotation)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Width == 0 {
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Width = defaultRaspividWidth
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadWidth)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Height == 0 {
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Height = defaultRaspividHeight
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadHeight)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.FrameRate == 0 {
|
2019-11-05 12:44:04 +03:00
|
|
|
c.FrameRate = defaultRaspividFramerate
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadFrameRate)
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:51:27 +03:00
|
|
|
if c.CBR || sliceutils.ContainsUint8(c.Outputs, config.OutputRTMP) {
|
2019-11-01 14:15:46 +03:00
|
|
|
c.Quantization = 0
|
|
|
|
if c.Bitrate <= 0 {
|
|
|
|
errs = append(errs, errBadBitrate)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Bitrate = defaultRaspividBitrate
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
2019-11-22 06:08:57 +03:00
|
|
|
} else {
|
|
|
|
c.Bitrate = 0
|
|
|
|
if c.Quantization < 10 || c.Quantization > 40 {
|
|
|
|
errs = append(errs, errBadQuantization)
|
|
|
|
c.Quantization = defaultRaspividQuantization
|
|
|
|
}
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.MinFrames <= 0 {
|
|
|
|
errs = append(errs, errBadMinFrames)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.MinFrames = defaultRaspividMinFrames
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Brightness <= 0 || c.Brightness > 100 {
|
|
|
|
errs = append(errs, errBadBrightness)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Brightness = defaultRaspividBrightness
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Saturation < -100 || c.Saturation > 100 {
|
|
|
|
errs = append(errs, errBadSaturation)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Saturation = defaultRaspividSaturation
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
2019-11-22 09:49:27 +03:00
|
|
|
if c.Exposure == "" || !sliceutils.ContainsString(ExposureModes[:], c.Exposure) {
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadExposure)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.Exposure = defaultRaspividExposure
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
if c.EV < -10 || c.EV > 10 {
|
|
|
|
errs = append(errs, errBadEV)
|
|
|
|
c.EV = defaultRaspividEV
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Contrast < -100 || c.Contrast > 100 {
|
|
|
|
errs = append(errs, errBadContrast)
|
|
|
|
c.Contrast = defaultRaspividContrast
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Sharpness < -100 || c.Sharpness > 100 {
|
|
|
|
errs = append(errs, errBadSharpness)
|
|
|
|
c.Sharpness = defaultRaspividSharpness
|
|
|
|
}
|
|
|
|
|
2019-11-22 09:49:27 +03:00
|
|
|
if c.AutoWhiteBalance == "" || !sliceutils.ContainsString(AutoWhiteBalanceModes[:], c.AutoWhiteBalance) {
|
2019-11-01 14:15:46 +03:00
|
|
|
errs = append(errs, errBadAutoWhiteBalance)
|
2019-11-05 12:44:04 +03:00
|
|
|
c.AutoWhiteBalance = defaultRaspividAutoWhiteBalance
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
if !goodAWBGains(c.AWBGains) {
|
|
|
|
errs = append(errs, errBadAWBGains)
|
|
|
|
c.AWBGains = defaultRaspividAWBGains
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ISO == 0 || c.ISO < 100 || c.ISO > 800 {
|
|
|
|
errs = append(errs, errBadISO)
|
|
|
|
c.ISO = defaultRaspividISO
|
|
|
|
}
|
|
|
|
|
2019-11-02 03:01:40 +03:00
|
|
|
r.cfg = c
|
2019-11-06 09:57:10 +03:00
|
|
|
return errs
|
2019-11-01 14:15:46 +03:00
|
|
|
}
|
2019-11-02 03:01:40 +03:00
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
func goodAWBGains(g string) bool {
|
|
|
|
parts := strings.Split(g, ",")
|
|
|
|
if len(parts) != 2 {
|
|
|
|
return false
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
bg, err := strconv.ParseFloat(parts[0], 64)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
rg, err := strconv.ParseFloat(parts[1], 64)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
2021-03-30 06:36:00 +03:00
|
|
|
|
|
|
|
if bg < 0 || rg < 0 {
|
|
|
|
return false
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
|
|
|
|
2021-03-30 06:36:00 +03:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start will prepare the arguments for the raspivid command using the
|
|
|
|
// configuration set using the Set method then call the raspivid command,
|
|
|
|
// piping the video output from which the Read method will read from.
|
|
|
|
func (r *Raspivid) Start() error {
|
|
|
|
args, err := r.createArgs()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create raspivid args: %w", err)
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
2021-03-30 06:36:00 +03:00
|
|
|
|
2019-11-02 03:01:40 +03:00
|
|
|
r.cfg.Logger.Log(logger.Info, pkg+"raspivid args", "raspividArgs", strings.Join(args, " "))
|
|
|
|
r.cmd = exec.Command("raspivid", args...)
|
|
|
|
|
|
|
|
r.out, err = r.cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not pipe command output: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-12-04 09:11:26 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-11-02 03:01:40 +03:00
|
|
|
err = r.cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not start raspivid command: %w", err)
|
|
|
|
}
|
2020-01-30 03:52:11 +03:00
|
|
|
r.isRunning = true
|
2019-11-02 03:01:40 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-11-02 03:06:55 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Read implements io.Reader. Calling read before Start has been called will
|
|
|
|
// result in 0 bytes read and an error.
|
2019-11-02 03:06:55 +03:00
|
|
|
func (r *Raspivid) Read(p []byte) (int, error) {
|
2019-11-05 12:44:04 +03:00
|
|
|
if r.out != nil {
|
|
|
|
return r.out.Read(p)
|
|
|
|
}
|
|
|
|
return 0, errors.New("cannot read, raspivid has not started")
|
2019-11-02 03:06:55 +03:00
|
|
|
}
|
2019-11-02 03:12:56 +03:00
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// Stop will terminate the raspivid process and close the output pipe.
|
2019-11-02 03:12:56 +03:00
|
|
|
func (r *Raspivid) Stop() error {
|
2020-03-31 12:13:21 +03:00
|
|
|
if r.isRunning == false {
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-04 09:11:26 +03:00
|
|
|
close(r.done)
|
2019-11-02 03:12:56 +03:00
|
|
|
if r.cmd == nil || r.cmd.Process == nil {
|
|
|
|
return errors.New("raspivid process was never started")
|
|
|
|
}
|
|
|
|
err := r.cmd.Process.Kill()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not kill raspivid process: %w", err)
|
|
|
|
}
|
2020-01-30 03:52:11 +03:00
|
|
|
r.isRunning = false
|
2019-11-05 12:44:04 +03:00
|
|
|
return r.out.Close()
|
2019-11-02 03:12:56 +03:00
|
|
|
}
|
2020-01-28 09:22:46 +03:00
|
|
|
|
|
|
|
// IsRunning is used to determine if the pi's camera is running.
|
|
|
|
func (r *Raspivid) IsRunning() bool {
|
2020-01-30 03:52:11 +03:00
|
|
|
return r.isRunning
|
2020-01-28 09:22:46 +03:00
|
|
|
}
|
2021-03-30 06:36:00 +03:00
|
|
|
|
|
|
|
func (r *Raspivid) createArgs() ([]string, error) {
|
|
|
|
const disabled = "0"
|
|
|
|
args := []string{
|
|
|
|
"--output", "-",
|
|
|
|
"--nopreview",
|
|
|
|
"--timeout", disabled,
|
|
|
|
"--width", fmt.Sprint(r.cfg.Width),
|
|
|
|
"--height", fmt.Sprint(r.cfg.Height),
|
|
|
|
"--bitrate", fmt.Sprint(r.cfg.Bitrate * 1000), // Convert from kbps to bps.
|
|
|
|
"--framerate", fmt.Sprint(r.cfg.FrameRate),
|
|
|
|
"--rotation", fmt.Sprint(r.cfg.Rotation),
|
|
|
|
"--brightness", fmt.Sprint(r.cfg.Brightness),
|
|
|
|
"--saturation", fmt.Sprint(r.cfg.Saturation),
|
|
|
|
"--sharpness", fmt.Sprint(r.cfg.Sharpness),
|
|
|
|
"--contrast", fmt.Sprint(r.cfg.Contrast),
|
|
|
|
"--awb", fmt.Sprint(r.cfg.AutoWhiteBalance),
|
|
|
|
"--exposure", fmt.Sprint(r.cfg.Exposure),
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.ISO != defaultRaspividISO {
|
|
|
|
args = append(args, []string{"--ISO", fmt.Sprint(r.cfg.ISO)}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.Exposure == "off" {
|
|
|
|
args = append(args, []string{"--ev", fmt.Sprint(r.cfg.EV)}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.AutoWhiteBalance == "off" {
|
|
|
|
args = append(args, []string{"--awbgains", fmt.Sprint(r.cfg.AWBGains)}...)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.HorizontalFlip {
|
|
|
|
args = append(args, "--hflip")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.VerticalFlip {
|
|
|
|
args = append(args, "--vflip")
|
|
|
|
}
|
|
|
|
if r.cfg.HorizontalFlip {
|
|
|
|
args = append(args, "--hflip")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.cfg.InputCodec {
|
|
|
|
default:
|
|
|
|
return []string{}, fmt.Errorf("revid: invalid input codec: %v", r.cfg.InputCodec)
|
|
|
|
case codecutil.H264:
|
|
|
|
args = append(args,
|
|
|
|
"--codec", "H264",
|
|
|
|
"--inline",
|
|
|
|
"--intra", fmt.Sprint(r.cfg.MinFrames),
|
|
|
|
)
|
|
|
|
if !r.cfg.CBR {
|
|
|
|
args = append(args, "-qp", fmt.Sprint(r.cfg.Quantization))
|
|
|
|
}
|
|
|
|
case codecutil.MJPEG:
|
|
|
|
args = append(args, "--codec", "MJPEG")
|
|
|
|
}
|
|
|
|
return args, nil
|
|
|
|
}
|