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