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.
|
|
|
|
*/
|
|
|
|
|
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"
|
|
|
|
"os/exec"
|
|
|
|
"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
|
|
|
|
defaultRaspividBitrate = 48000
|
|
|
|
defaultRaspividFramerate = 25
|
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")
|
|
|
|
)
|
|
|
|
|
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 {
|
2019-11-06 09:57:10 +03:00
|
|
|
cfg config.Config
|
2019-11-02 03:01:40 +03:00
|
|
|
cmd *exec.Cmd
|
|
|
|
out io.ReadCloser
|
2019-11-06 09:57:10 +03:00
|
|
|
log config.Logger
|
2019-11-02 03:01:40 +03:00
|
|
|
}
|
|
|
|
|
2019-11-12 08:34:07 +03:00
|
|
|
// New returns a new Raspivid.
|
|
|
|
func New(l config.Logger) *Raspivid { return &Raspivid{log: l} }
|
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 06:08:57 +03:00
|
|
|
if c.CBR || containsUint8(config.OutputHTTP, c.Outputs) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2019-11-05 12:44:04 +03:00
|
|
|
// 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.
|
2019-11-02 03:01:40 +03:00
|
|
|
func (r *Raspivid) Start() 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),
|
|
|
|
"--exposure", fmt.Sprint(r.cfg.Exposure),
|
|
|
|
"--awb", fmt.Sprint(r.cfg.AutoWhiteBalance),
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.FlipHorizontal {
|
|
|
|
args = append(args, "--hflip")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.FlipVertical {
|
|
|
|
args = append(args, "--vflip")
|
|
|
|
}
|
|
|
|
if r.cfg.FlipHorizontal {
|
|
|
|
args = append(args, "--hflip")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.cfg.InputCodec {
|
|
|
|
default:
|
|
|
|
return 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),
|
|
|
|
)
|
2019-11-22 06:08:57 +03:00
|
|
|
if !r.cfg.CBR {
|
2019-11-02 03:01:40 +03:00
|
|
|
args = append(args, "-qp", fmt.Sprint(r.cfg.Quantization))
|
|
|
|
}
|
|
|
|
case codecutil.MJPEG:
|
|
|
|
args = append(args, "--codec", "MJPEG")
|
|
|
|
}
|
|
|
|
r.cfg.Logger.Log(logger.Info, pkg+"raspivid args", "raspividArgs", strings.Join(args, " "))
|
|
|
|
r.cmd = exec.Command("raspivid", args...)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
r.out, err = r.cmd.StdoutPipe()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not pipe command output: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = r.cmd.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not start raspivid command: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
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)
|
|
|
|
}
|
2019-11-05 12:44:04 +03:00
|
|
|
return r.out.Close()
|
2019-11-02 03:12:56 +03:00
|
|
|
}
|