cmd/revid-cli & revid: Checking revid mode differently - now using ns.Mode(), which should soon be an available feature. Also now using ns.SetMode() - which tells netreceiver that we've changed mode.

This commit is contained in:
saxon 2019-02-03 23:37:38 +10:30
parent 9095044e23
commit 1010721dd0
2 changed files with 45 additions and 138 deletions

View File

@ -50,6 +50,13 @@ const (
defaultLogVerbosity = logger.Debug defaultLogVerbosity = logger.Debug
) )
// Revid modes
const (
normal = "Normal"
paused = "Paused"
burst = "Burst"
)
// Other misc consts // Other misc consts
const ( const (
netSendRetryTime = 5 * time.Second netSendRetryTime = 5 * time.Second
@ -244,9 +251,9 @@ func handleFlags() revid.Config {
// initialize then run the main NetSender client // initialize then run the main NetSender client
func run(cfg revid.Config) error { func run(cfg revid.Config) error {
// initialize NetSender and use NetSender's logger
log.Log(logger.Info, pkg+"running in NetSender mode") log.Log(logger.Info, pkg+"running in NetSender mode")
// initialize NetSender and use NetSender's logger
var ns netsender.Sender var ns netsender.Sender
if err := ns.Init(log, nil, nil, nil); err != nil { if err := ns.Init(log, nil, nil, nil); err != nil {
return err return err
@ -254,8 +261,12 @@ func run(cfg revid.Config) error {
vars, _ := ns.Vars() vars, _ := ns.Vars()
vs := ns.VarSum() vs := ns.VarSum()
rv, cfg, err = updateRevid(&ns, rv, cfg, vars, false) rv, err := revid.New(cfg, &ns)
if err != nil { if err != nil {
log.Log(logger.Fatal, pkg+"could not initialise revid", "error", err.Error())
}
if err = rv.Update(vars); err != nil {
return err return err
} }
@ -267,7 +278,6 @@ func run(cfg revid.Config) error {
} }
if vs != ns.VarSum() { if vs != ns.VarSum() {
// vars changed
vars, err := ns.Vars() vars, err := ns.Vars()
if err != nil { if err != nil {
log.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error()) log.Log(logger.Error, pkg+"netSender failed to get vars", "error", err.Error())
@ -275,24 +285,32 @@ func run(cfg revid.Config) error {
continue continue
} }
vs = ns.VarSum() vs = ns.VarSum()
if vars["mode"] == "Paused" {
if !paused { switch ns.Mode() {
case paused:
if rv.IsRunning() {
log.Log(logger.Info, pkg+"pausing revid") log.Log(logger.Info, pkg+"pausing revid")
err = stopRevid(rv) if err = rv.Stop(); err != nil {
if err != nil { log.Log(logger.Error, pkg+"failed to stop revid", "error", err.Error())
log.Log(logger.Error, pkg+"failed to stop revide", "error", err.Error())
continue continue
} }
paused = true ns.SetMode(paused)
} }
} else { case normal:
rv, cfg, err = updateRevid(&ns, rv, cfg, vars, !paused) if err = rv.Update(vars); err != nil {
if err != nil {
return err return err
} }
if paused { ns.SetMode(normal)
paused = false 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")) sleepTime, _ := strconv.Atoi(ns.Param("mp"))
@ -323,117 +341,6 @@ func send(ns *netsender.Sender, rv *revid.Revid) error {
return nil 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. // flagStrings implements an appending string set flag.
type flagStrings []string type flagStrings []string

View File

@ -405,19 +405,19 @@ func (r *Revid) Update(vars map[string]string) error {
case "FramesPerClip": case "FramesPerClip":
f, err := strconv.ParseUint(value, 10, 0) f, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.FramesPerClip = uint(f) r.config.FramesPerClip = uint(f)
case "RtmpUrl": case "RtmpUrl":
r.config.RtmpUrl = value r.config.RtmpUrl = value
case "Bitrate": case "Bitrate":
r, err := strconv.ParseUint(value, 10, 0) v, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.Bitrate = uint(r) r.config.Bitrate = uint(v)
case "OutputFileName": case "OutputFileName":
r.config.OutputFileName = value r.config.OutputFileName = value
case "InputFileName": case "InputFileName":
@ -425,37 +425,37 @@ func (r *Revid) Update(vars map[string]string) error {
case "Height": case "Height":
h, err := strconv.ParseUint(value, 10, 0) h, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.Height = uint(h) r.config.Height = uint(h)
case "Width": case "Width":
w, err := strconv.ParseUint(value, 10, 0) w, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.Width = uint(w) r.config.Width = uint(w)
case "FrameRate": case "FrameRate":
r, err := strconv.ParseUint(value, 10, 0) v, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.FrameRate = uint(r) r.config.FrameRate = uint(v)
case "HttpAddress": case "HttpAddress":
r.config.HttpAddress = value r.config.HttpAddress = value
case "Quantization": case "Quantization":
q, err := strconv.ParseUint(value, 10, 0) q, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.Quantization = uint(q) r.config.Quantization = uint(q)
case "IntraRefreshPeriod": case "IntraRefreshPeriod":
p, err := strconv.ParseUint(value, 10, 0) p, err := strconv.ParseUint(value, 10, 0)
if err != nil { 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 break
} }
r.config.IntraRefreshPeriod = uint(p) r.config.IntraRefreshPeriod = uint(p)
@ -466,7 +466,7 @@ func (r *Revid) Update(vars map[string]string) error {
case "false": case "false":
r.config.FlipHorizontal = false r.config.FlipHorizontal = false
default: 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": case "VerticalFlip":
switch strings.ToLower(value) { switch strings.ToLower(value) {
@ -475,7 +475,7 @@ func (r *Revid) Update(vars map[string]string) error {
case "false": case "false":
r.config.FlipVertical = false r.config.FlipVertical = false
default: default:
log.Log(logger.Warning, pkg+"invalid VerticalFlip param", "value", value) r.config.Logger.Log(logger.Warning, pkg+"invalid VerticalFlip param", "value", value)
} }
default: default:
} }