From b554c2820ab516f65226540e8bd1fc019823cadf Mon Sep 17 00:00:00 2001 From: Saxon Date: Sat, 2 Nov 2019 10:31:40 +1030 Subject: [PATCH] revid: wrote implementation of Start method for Raspivid implementation --- revid/raspivid.go | 76 +++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/revid/raspivid.go b/revid/raspivid.go index f467b727..27b2c223 100644 --- a/revid/raspivid.go +++ b/revid/raspivid.go @@ -27,8 +27,12 @@ package revid import ( "errors" "fmt" + "io" + "os/exec" + "strings" "bitbucket.org/ausocean/av/codec/codecutil" + "bitbucket.org/ausocean/utils/logger" ) // Raspivid AVDevice configuration defaults. @@ -47,10 +51,6 @@ const ( raspividDefaultFramerate = 25 ) -type Raspivid struct { - c Config -} - var ( errBadCodec = errors.New("codec 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") ) +type Raspivid struct { + cfg Config + cmd *exec.Cmd + out io.ReadCloser +} + type multiError []error func (me multiError) Error() string { @@ -140,6 +146,66 @@ func (r *Raspivid) Set(c Config) error { c.AutoWhiteBalance = raspividDefaultAutoWhiteBalance } - r.c = c + r.cfg = c 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 +}