diff --git a/cmd/revid-cli/main.go b/cmd/revid-cli/main.go index 826cb2a4..58f8e507 100644 --- a/cmd/revid-cli/main.go +++ b/cmd/revid-cli/main.go @@ -50,6 +50,13 @@ const ( defaultLogVerbosity = logger.Debug ) +// Revid modes +const ( + normal = "Normal" + paused = "Paused" + burst = "Burst" +) + // Other misc consts const ( netSendRetryTime = 5 * time.Second @@ -244,9 +251,9 @@ func handleFlags() revid.Config { // initialize then run the main NetSender client func run(cfg revid.Config) error { - // initialize NetSender and use NetSender's logger log.Log(logger.Info, pkg+"running in NetSender mode") + // initialize NetSender and use NetSender's logger var ns netsender.Sender if err := ns.Init(log, nil, nil, nil); err != nil { return err @@ -254,8 +261,12 @@ func run(cfg revid.Config) error { vars, _ := ns.Vars() vs := ns.VarSum() - rv, cfg, err = updateRevid(&ns, rv, cfg, vars, false) + rv, err := revid.New(cfg, &ns) if err != nil { + log.Log(logger.Fatal, pkg+"could not initialise revid", "error", err.Error()) + } + + if err = rv.Update(vars); err != nil { return err } @@ -267,7 +278,6 @@ func run(cfg revid.Config) error { } if vs != ns.VarSum() { - // vars changed vars, err := ns.Vars() if err != nil { log.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error()) @@ -275,24 +285,32 @@ func run(cfg revid.Config) error { continue } vs = ns.VarSum() - if vars["mode"] == "Paused" { - if !paused { + + switch ns.Mode() { + case paused: + if rv.IsRunning() { log.Log(logger.Info, pkg+"pausing revid") - err = stopRevid(rv) - if err != nil { - log.Log(logger.Error, pkg+"failed to stop revide", "error", err.Error()) + if err = rv.Stop(); err != nil { + log.Log(logger.Error, pkg+"failed to stop revid", "error", err.Error()) continue } - paused = true + ns.SetMode(paused) } - } else { - rv, cfg, err = updateRevid(&ns, rv, cfg, vars, !paused) - if err != nil { + case normal: + if err = rv.Update(vars); err != nil { return err } - if paused { - paused = false + ns.SetMode(normal) + case burst: + if err = rv.Start(); err != nil { + return err } + ns.SetMode(burst) + time.Sleep(rv.Config().BurstPeriod) + if err = rv.Stop(); err != nil { + return err + } + ns.SetMode(paused) } } sleepTime, _ := strconv.Atoi(ns.Param("mp")) @@ -323,117 +341,6 @@ func send(ns *netsender.Sender, rv *revid.Revid) error { return nil } -func updateRevid(ns *netsender.Sender, rv *revid.Revid, cfg revid.Config, vars map[string]string, stop bool) (*revid.Revid, revid.Config, error) { - if stop { - err := stopRevid(rv) - if err != nil { - return nil, cfg, err - } - } - - //look through the vars and update revid where needed - for key, value := range vars { - switch key { - case "Output": - // FIXME(kortschak): There can be only one! - // How do we specify outputs after the first? - // - // Maybe we shouldn't be doing this! - switch value { - case "File": - cfg.Outputs[0] = revid.File - case "Http": - cfg.Outputs[0] = revid.Http - case "Rtmp": - cfg.Outputs[0] = revid.Rtmp - case "FfmpegRtmp": - cfg.Outputs[0] = revid.FfmpegRtmp - default: - log.Log(logger.Warning, pkg+"invalid Output1 param", "value", value) - continue - } - case "FramesPerClip": - f, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid framesperclip param", "value", value) - break - } - cfg.FramesPerClip = uint(f) - case "RtmpUrl": - cfg.RtmpUrl = value - case "Bitrate": - r, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid framerate param", "value", value) - break - } - cfg.Bitrate = uint(r) - case "OutputFileName": - cfg.OutputFileName = value - case "InputFileName": - cfg.InputFileName = value - case "Height": - h, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid height param", "value", value) - break - } - cfg.Height = uint(h) - case "Width": - w, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid width param", "value", value) - break - } - cfg.Width = uint(w) - case "FrameRate": - r, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid framerate param", "value", value) - break - } - cfg.FrameRate = uint(r) - case "HttpAddress": - cfg.HttpAddress = value - case "Quantization": - q, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid quantization param", "value", value) - break - } - cfg.Quantization = uint(q) - case "IntraRefreshPeriod": - p, err := strconv.ParseUint(value, 10, 0) - if err != nil { - log.Log(logger.Warning, pkg+"invalid intrarefreshperiod param", "value", value) - break - } - cfg.IntraRefreshPeriod = uint(p) - case "HorizontalFlip": - switch strings.ToLower(value) { - case "true": - cfg.FlipHorizontal = true - case "false": - cfg.FlipHorizontal = false - default: - log.Log(logger.Warning, pkg+"invalid HorizontalFlip param", "value", value) - } - case "VerticalFlip": - switch strings.ToLower(value) { - case "true": - cfg.FlipVertical = true - case "false": - cfg.FlipVertical = false - default: - log.Log(logger.Warning, pkg+"invalid VerticalFlip param", "value", value) - } - default: - } - } - - return startRevid(ns, cfg) -} - // flagStrings implements an appending string set flag. type flagStrings []string diff --git a/revid/revid.go b/revid/revid.go index 33d847ea..c90ba8da 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -405,19 +405,19 @@ func (r *Revid) Update(vars map[string]string) error { case "FramesPerClip": f, err := strconv.ParseUint(value, 10, 0) if err != nil { - .Log(logger.Warning, pkg+"invalid framesperclip param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid framesperclip param", "value", value) break } r.config.FramesPerClip = uint(f) case "RtmpUrl": r.config.RtmpUrl = value case "Bitrate": - r, err := strconv.ParseUint(value, 10, 0) + v, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid framerate param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid framerate param", "value", value) break } - r.config.Bitrate = uint(r) + r.config.Bitrate = uint(v) case "OutputFileName": r.config.OutputFileName = value case "InputFileName": @@ -425,37 +425,37 @@ func (r *Revid) Update(vars map[string]string) error { case "Height": h, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid height param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid height param", "value", value) break } r.config.Height = uint(h) case "Width": w, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid width param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid width param", "value", value) break } r.config.Width = uint(w) case "FrameRate": - r, err := strconv.ParseUint(value, 10, 0) + v, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid framerate param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid framerate param", "value", value) break } - r.config.FrameRate = uint(r) + r.config.FrameRate = uint(v) case "HttpAddress": r.config.HttpAddress = value case "Quantization": q, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid quantization param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid quantization param", "value", value) break } r.config.Quantization = uint(q) case "IntraRefreshPeriod": p, err := strconv.ParseUint(value, 10, 0) if err != nil { - log.Log(logger.Warning, pkg+"invalid intrarefreshperiod param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid intrarefreshperiod param", "value", value) break } r.config.IntraRefreshPeriod = uint(p) @@ -466,7 +466,7 @@ func (r *Revid) Update(vars map[string]string) error { case "false": r.config.FlipHorizontal = false default: - log.Log(logger.Warning, pkg+"invalid HorizontalFlip param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid HorizontalFlip param", "value", value) } case "VerticalFlip": switch strings.ToLower(value) { @@ -475,7 +475,7 @@ func (r *Revid) Update(vars map[string]string) error { case "false": r.config.FlipVertical = false default: - log.Log(logger.Warning, pkg+"invalid VerticalFlip param", "value", value) + r.config.Logger.Log(logger.Warning, pkg+"invalid VerticalFlip param", "value", value) } default: }