cmd/revid-cli: make config struct function scoped

This commit is contained in:
Dan Kortschak 2018-12-06 14:04:55 +10:30
parent 00216b444b
commit 926763dd74
1 changed files with 85 additions and 83 deletions

View File

@ -64,35 +64,34 @@ var canProfile = true
var logger *smartlogger.Logger
// Globals
var (
rv *revid.Revid
config revid.Config
)
var rv *revid.Revid
func main() {
useNetsender := flag.Bool("NetSender", false, "Are we checking vars through netsender?")
runDurationPtr := flag.Duration("runDuration", defaultRunDuration, "How long do you want revid to run for?")
handleFlags()
cfg := handleFlags()
if !*useNetsender {
// run revid for the specified duration
startRevid(nil)
startRevid(nil, cfg)
time.Sleep(*runDurationPtr)
stopRevid()
return
}
err := run()
err := run(cfg)
if err != nil {
logger.Log(smartlogger.Fatal, pkg+"failed to run revid", "error", err.Error())
os.Exit(1)
}
}
// Handle flags interprets and validates command line flags and sets revid
// config etc accordingly
func handleFlags() {
// handleFlags parses command line flags and returns a revid configuration
// based on them.
func handleFlags() revid.Config {
var cfg revid.Config
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to `file`")
@ -126,7 +125,7 @@ func handleFlags() {
logger = smartlogger.New(defaultLogVerbosity, *logPathPtr)
config.Logger = logger
cfg.Logger = logger
if *cpuprofile != "" {
if canProfile {
@ -145,9 +144,9 @@ func handleFlags() {
switch *inputPtr {
case "Raspivid":
config.Input = revid.Raspivid
cfg.Input = revid.Raspivid
case "File":
config.Input = revid.File
cfg.Input = revid.File
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad input argument")
@ -155,7 +154,7 @@ func handleFlags() {
switch *inputCodecPtr {
case "H264":
config.InputCodec = revid.H264
cfg.InputCodec = revid.H264
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad input codec argument")
@ -163,17 +162,17 @@ func handleFlags() {
switch *output1Ptr {
case "File":
config.Output1 = revid.File
cfg.Output1 = revid.File
case "Http":
config.Output1 = revid.Http
cfg.Output1 = revid.Http
case "Rtmp":
config.Output1 = revid.Rtmp
cfg.Output1 = revid.Rtmp
case "FfmpegRtmp":
config.Output1 = revid.FfmpegRtmp
cfg.Output1 = revid.FfmpegRtmp
case "Udp":
config.Output1 = revid.Udp
cfg.Output1 = revid.Udp
case "Rtp":
config.Output1 = revid.Rtp
cfg.Output1 = revid.Rtp
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad output 1 argument")
@ -181,17 +180,17 @@ func handleFlags() {
switch *output2Ptr {
case "File":
config.Output2 = revid.File
cfg.Output2 = revid.File
case "Http":
config.Output2 = revid.Http
cfg.Output2 = revid.Http
case "Rtmp":
config.Output2 = revid.Rtmp
cfg.Output2 = revid.Rtmp
case "FfmpegRtmp":
config.Output2 = revid.FfmpegRtmp
cfg.Output2 = revid.FfmpegRtmp
case "Udp":
config.Output2 = revid.Udp
cfg.Output2 = revid.Udp
case "Rtp":
config.Output2 = revid.Rtp
cfg.Output2 = revid.Rtp
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad output 2 argument")
@ -199,9 +198,9 @@ func handleFlags() {
switch *rtmpMethodPtr {
case "Ffmpeg":
config.RtmpMethod = revid.Ffmpeg
cfg.RtmpMethod = revid.Ffmpeg
case "LibRtmp":
config.RtmpMethod = revid.LibRtmp
cfg.RtmpMethod = revid.LibRtmp
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad rtmp method argument")
@ -209,11 +208,11 @@ func handleFlags() {
switch *packetizationPtr {
case "None":
config.Packetization = revid.None
cfg.Packetization = revid.None
case "Mpegts":
config.Packetization = revid.Mpegts
cfg.Packetization = revid.Mpegts
case "Flv":
config.Packetization = revid.Flv
cfg.Packetization = revid.Flv
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad packetization argument")
@ -221,9 +220,9 @@ func handleFlags() {
switch *quantizationModePtr {
case "QuantizationOn":
config.QuantizationMode = revid.QuantizationOn
cfg.QuantizationMode = revid.QuantizationOn
case "QuantizationOff":
config.QuantizationMode = revid.QuantizationOff
cfg.QuantizationMode = revid.QuantizationOff
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad quantization mode argument")
@ -231,9 +230,9 @@ func handleFlags() {
switch *verbosityPtr {
case "No":
config.LogLevel = smartlogger.Fatal
cfg.LogLevel = smartlogger.Fatal
case "Yes":
config.LogLevel = smartlogger.Debug
cfg.LogLevel = smartlogger.Debug
case "":
default:
logger.Log(smartlogger.Error, pkg+"bad verbosity argument")
@ -241,46 +240,48 @@ func handleFlags() {
switch *horizontalFlipPtr {
case "No":
config.FlipHorizontal = false
cfg.FlipHorizontal = false
case "Yes":
config.FlipHorizontal = true
cfg.FlipHorizontal = true
case "":
config.FlipHorizontal = false
cfg.FlipHorizontal = false
default:
logger.Log(smartlogger.Error, pkg+"bad horizontal flip option")
}
switch *verticalFlipPtr {
case "No":
config.FlipVertical = false
cfg.FlipVertical = false
case "Yes":
config.FlipVertical = true
cfg.FlipVertical = true
case "":
config.FlipVertical = false
cfg.FlipVertical = false
default:
logger.Log(smartlogger.Error, pkg+"bad vertical flip option")
}
fpc, err := strconv.Atoi(*framesPerClipPtr)
if err == nil && fpc > 0 {
config.FramesPerClip = fpc
cfg.FramesPerClip = fpc
}
config.RtmpUrl = *rtmpUrlPtr
config.Bitrate = *bitratePtr
config.OutputFileName = *outputFileNamePtr
config.InputFileName = *inputFileNamePtr
config.Height = *heightPtr
config.Width = *widthPtr
config.FrameRate = *frameRatePtr
config.HttpAddress = *httpAddressPtr
config.Quantization = *quantizationPtr
config.Timeout = *timeoutPtr
config.IntraRefreshPeriod = *intraRefreshPeriodPtr
config.RtpAddress = *rtpAddrPtr
cfg.RtmpUrl = *rtmpUrlPtr
cfg.Bitrate = *bitratePtr
cfg.OutputFileName = *outputFileNamePtr
cfg.InputFileName = *inputFileNamePtr
cfg.Height = *heightPtr
cfg.Width = *widthPtr
cfg.FrameRate = *frameRatePtr
cfg.HttpAddress = *httpAddressPtr
cfg.Quantization = *quantizationPtr
cfg.Timeout = *timeoutPtr
cfg.IntraRefreshPeriod = *intraRefreshPeriodPtr
cfg.RtpAddress = *rtpAddrPtr
return cfg
}
// initialize then run the main NetSender client
func run() error {
func run(cfg revid.Config) error {
// initialize NetSender and use NetSender's logger
//config.Logger = netsender.Logger()
logger.Log(smartlogger.Info, pkg+"running in NetSender mode")
@ -297,7 +298,7 @@ func run() error {
paused = true
}
if !paused {
err = updateRevid(&ns, vars, false)
cfg, err = updateRevid(&ns, cfg, vars, false)
if err != nil {
return err
}
@ -326,7 +327,7 @@ func run() error {
paused = true
}
} else {
err = updateRevid(&ns, vars, !paused)
cfg, err = updateRevid(&ns, cfg, vars, !paused)
if err != nil {
return err
}
@ -364,13 +365,14 @@ func send(ns *netsender.Sender) error {
}
// wrappers for stopping and starting revid
func startRevid(ns *netsender.Sender) (err error) {
rv, err = revid.New(config, ns)
func startRevid(ns *netsender.Sender, cfg revid.Config) (revid.Config, error) {
var err error
rv, err = revid.New(cfg, ns)
if err != nil {
return err
return cfg, err
}
rv.Start()
return nil
return cfg, nil
}
func stopRevid() {
@ -381,7 +383,7 @@ func stopRevid() {
time.Sleep(revidStopTime)
}
func updateRevid(ns *netsender.Sender, vars map[string]string, stop bool) error {
func updateRevid(ns *netsender.Sender, cfg revid.Config, vars map[string]string, stop bool) (revid.Config, error) {
if stop {
stopRevid()
}
@ -392,13 +394,13 @@ func updateRevid(ns *netsender.Sender, vars map[string]string, stop bool) error
case "Output":
switch value {
case "File":
config.Output1 = revid.File
cfg.Output1 = revid.File
case "Http":
config.Output1 = revid.Http
cfg.Output1 = revid.Http
case "Rtmp":
config.Output1 = revid.Rtmp
cfg.Output1 = revid.Rtmp
case "FfmpegRtmp":
config.Output1 = revid.FfmpegRtmp
cfg.Output1 = revid.FfmpegRtmp
default:
logger.Log(smartlogger.Warning, pkg+"invalid Output1 param", "value", value)
continue
@ -406,78 +408,78 @@ func updateRevid(ns *netsender.Sender, vars map[string]string, stop bool) error
case "FramesPerClip":
fpc, err := strconv.Atoi(value)
if fpc > 0 && err == nil {
config.FramesPerClip = fpc
cfg.FramesPerClip = fpc
} else {
logger.Log(smartlogger.Warning, pkg+"invalid FramesPerClip param", "value", value)
}
case "RtmpUrl":
config.RtmpUrl = value
cfg.RtmpUrl = value
case "Bitrate":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.Bitrate = value
cfg.Bitrate = value
} else {
logger.Log(smartlogger.Warning, pkg+"invalid Bitrate param", "value", value)
}
case "OutputFileName":
config.OutputFileName = value
cfg.OutputFileName = value
case "InputFileName":
config.InputFileName = value
cfg.InputFileName = value
case "Height":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.Height = value
cfg.Height = value
} else {
logger.Log(smartlogger.Warning, pkg+"invalid Height param", "value", value)
}
case "Width":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.Width = value
cfg.Width = value
} else {
logger.Log(smartlogger.Warning, pkg+"invalid Width param", "value", value)
}
case "FrameRate":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.FrameRate = value
cfg.FrameRate = value
} else {
logger.Log(smartlogger.Warning, pkg+"invalid FrameRate param", "value", value)
}
case "HttpAddress":
config.HttpAddress = value
cfg.HttpAddress = value
case "Quantization":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.Quantization = value
cfg.Quantization = value
} else {
logger.Log(smartlogger.Warning, pkg+"invalid Quantization param", "value", value)
}
case "Timeout":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.Timeout = value
cfg.Timeout = value
}
case "IntraRefreshPeriod":
asInt, err := strconv.Atoi(value)
if asInt > 0 && err == nil {
config.IntraRefreshPeriod = value
cfg.IntraRefreshPeriod = value
}
case "HorizontalFlip":
switch value {
case "Yes":
config.FlipHorizontal = true
cfg.FlipHorizontal = true
case "No":
config.FlipHorizontal = false
cfg.FlipHorizontal = false
default:
logger.Log(smartlogger.Warning, pkg+"invalid HorizontalFlip param", "value", value)
}
case "VerticalFlip":
switch value {
case "Yes":
config.FlipVertical = true
cfg.FlipVertical = true
case "No":
config.FlipVertical = false
cfg.FlipVertical = false
default:
logger.Log(smartlogger.Warning, pkg+"invalid VerticalFlip param", "value", value)
}
@ -485,5 +487,5 @@ func updateRevid(ns *netsender.Sender, vars map[string]string, stop bool) error
}
}
return startRevid(ns)
return startRevid(ns, cfg)
}