revid: wrote implementation of Start method for Raspivid implementation

This commit is contained in:
Saxon 2019-11-02 10:31:40 +10:30
parent 924858c1c0
commit b554c2820a
1 changed files with 71 additions and 5 deletions

View File

@ -27,8 +27,12 @@ package revid
import ( import (
"errors" "errors"
"fmt" "fmt"
"io"
"os/exec"
"strings"
"bitbucket.org/ausocean/av/codec/codecutil" "bitbucket.org/ausocean/av/codec/codecutil"
"bitbucket.org/ausocean/utils/logger"
) )
// Raspivid AVDevice configuration defaults. // Raspivid AVDevice configuration defaults.
@ -47,10 +51,6 @@ const (
raspividDefaultFramerate = 25 raspividDefaultFramerate = 25
) )
type Raspivid struct {
c Config
}
var ( var (
errBadCodec = errors.New("codec bad or unset, defaulting") errBadCodec = errors.New("codec bad or unset, defaulting")
errBadRotation = errors.New("rotation bad or unset, defaulting") errBadRotation = errors.New("rotation bad or unset, defaulting")
@ -66,6 +66,12 @@ var (
errBadQuantization = errors.New("quantization bad or unset, defaulting") errBadQuantization = errors.New("quantization bad or unset, defaulting")
) )
type Raspivid struct {
cfg Config
cmd *exec.Cmd
out io.ReadCloser
}
type multiError []error type multiError []error
func (me multiError) Error() string { func (me multiError) Error() string {
@ -140,6 +146,66 @@ func (r *Raspivid) Set(c Config) error {
c.AutoWhiteBalance = raspividDefaultAutoWhiteBalance c.AutoWhiteBalance = raspividDefaultAutoWhiteBalance
} }
r.c = c r.cfg = c
return multiError(errs) return multiError(errs)
} }
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),
)
if r.cfg.VBR {
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
}