/* DESCRIPTION variables.go contains a list of structs that provide a variable Name, type in a string format, a function for updating the variable in the Config struct from a string, and finally, a validation function to check the validity of the corresponding field value in the Config. AUTHORS Saxon A. Nelson-Milton LICENSE Copyright (C) 2020 the Australian Ocean Lab (AusOcean) It is free software: you can redistribute it and/or modify them under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. It is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with revid in gpl.txt. If not, see http://www.gnu.org/licenses. */ package config import ( "fmt" "strconv" "strings" "time" "bitbucket.org/ausocean/av/codec/codecutil" "bitbucket.org/ausocean/utils/logger" ) // Config map Keys. const ( KeyAutoWhiteBalance = "AutoWhiteBalance" KeyBitDepth = "BitDepth" KeyBitrate = "Bitrate" KeyBrightness = "Brightness" KeyBurstPeriod = "BurstPeriod" KeyCameraChan = "CameraChan" KeyCameraIP = "CameraIP" KeyCBR = "CBR" KeyClipDuration = "ClipDuration" KeyChannels = "Channels" KeyExposure = "Exposure" KeyFileFPS = "FileFPS" KeyFilters = "Filters" KeyFrameRate = "FrameRate" KeyHeight = "Height" KeyHorizontalFlip = "HorizontalFlip" KeyHTTPAddress = "HTTPAddress" KeyInput = "Input" KeyInputCodec = "InputCodec" KeyInputPath = "InputPath" KeyLogging = "logging" KeyLoop = "Loop" KeyMinFPS = "MinFPS" KeyMinFrames = "MinFrames" KeyMode = "mode" KeyMotionDownscaling = "MotionDownscaling" KeyMotionHistory = "MotionHistory" KeyMotionInterval = "MotionInterval" KeyMotionKernel = "MotionKernel" KeyMotionMinArea = "MotionMinArea" KeyMotionPadding = "MotionPadding" KeyMotionPixels = "MotionPixels" KeyMotionThreshold = "MotionThreshold" KeyOutput = "Output" KeyOutputPath = "OutputPath" KeyOutputs = "Outputs" KeyPSITime = "PSITime" KeyQuantization = "Quantization" KeyRBCapacity = "RBCapacity" KeyRBStartElementSize = "RBStartElementSize" KeyRBWriteTimeout = "RBWriteTimeout" KeyRecPeriod = "RecPeriod" KeyRotation = "Rotation" KeyRTMPURL = "RTMPURL" KeyRTPAddress = "RTPAddress" KeySampleRate = "SampleRate" KeySaturation = "Saturation" KeyJPEGQuality = "JPEGQuality" KeySuppress = "Suppress" KeyTimelapseDuration = "TimelapseDuration" KeyTimelapseInterval = "TimelapseInterval" KeyVBRBitrate = "VBRBitrate" KeyVBRQuality = "VBRQuality" KeyVerticalFlip = "VerticalFlip" KeyWidth = "Width" ) // Config map parameter types. const ( typeString = "string" typeInt = "int" typeUint = "uint" typeBool = "bool" typeFloat = "float" ) // Default variable values. const ( // General revid defaults. defaultInput = InputRaspivid defaultOutput = OutputHTTP defaultInputCodec = codecutil.H264 defaultVerbosity = logger.Error defaultRTPAddr = "localhost:6970" defaultCameraIP = "192.168.1.50" defaultBurstPeriod = 10 // Seconds defaultMinFrames = 100 defaultFrameRate = 25 defaultClipDuration = 0 defaultPSITime = 2 defaultFileFPS = 0 // Ring buffer defaults. defaultRBCapacity = 50000000 // => 50MB defaultRBStartElementSize = 1000 // bytes defaultRBWriteTimeout = 5 // Seconds. // Motion filter parameter defaults. defaultMinFPS = 1.0 ) var Variables = []struct { Name string Type_ string Update func(*Config, string) Validate func(*Config) }{ { Name: KeyAutoWhiteBalance, Type_: "enum:off,auto,sun,cloud,shade,tungsten,fluorescent,incandescent,flash,horizon", Update: func(c *Config, v string) { c.AutoWhiteBalance = v }, }, { Name: KeyBitDepth, Type_: typeUint, Update: func(c *Config, v string) { c.BitDepth = parseUint(KeyBitDepth, v, c) }, }, { Name: KeyBitrate, Type_: typeUint, Update: func(c *Config, v string) { c.Bitrate = parseUint(KeyBitrate, v, c) }, }, { Name: KeyBrightness, Type_: typeUint, Update: func(c *Config, v string) { c.Brightness = parseUint(KeyBrightness, v, c) }, }, { Name: KeyBurstPeriod, Type_: typeUint, Update: func(c *Config, v string) { c.BurstPeriod = parseUint(KeyBurstPeriod, v, c) }, Validate: func(c *Config) { if c.BurstPeriod <= 0 { c.LogInvalidField(KeyBurstPeriod, defaultBurstPeriod) c.BurstPeriod = defaultBurstPeriod } }, }, { Name: KeyCameraChan, Type_: typeUint, Update: func(c *Config, v string) { c.CameraChan = uint8(parseUint(KeyCameraChan, v, c)) }, }, { Name: KeyCameraIP, Type_: typeString, Update: func(c *Config, v string) { c.CameraIP = v }, Validate: func(c *Config) { if c.CameraIP == "" { c.LogInvalidField(KeyCameraIP, defaultCameraIP) c.CameraIP = defaultCameraIP } }, }, { Name: KeyCBR, Type_: typeBool, Update: func(c *Config, v string) { c.CBR = parseBool(KeyCBR, v, c) }, }, { Name: KeyClipDuration, Type_: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, "invalid ClipDuration param", "value", v) } c.ClipDuration = time.Duration(_v) * time.Second }, Validate: func(c *Config) { if c.ClipDuration <= 0 { c.LogInvalidField(KeyClipDuration, defaultClipDuration) c.ClipDuration = defaultClipDuration } }, }, { Name: KeyChannels, Type_: typeUint, Update: func(c *Config, v string) { c.Channels = parseUint(KeyChannels, v, c) }, }, { Name: KeyExposure, Type_: "enum:auto,night,nightpreview,backlight,spotlight,sports,snow,beach,verylong,fixedfps,antishake,fireworks", Update: func(c *Config, v string) { c.Exposure = v }, }, { Name: KeyFileFPS, Type_: typeUint, Update: func(c *Config, v string) { c.FileFPS = parseUint(KeyFileFPS, v, c) }, Validate: func(c *Config) { if c.FileFPS <= 0 || (c.FileFPS > 0 && c.Input != InputFile) { c.LogInvalidField(KeyFileFPS, defaultFileFPS) c.FileFPS = defaultFileFPS } }, }, { Name: KeyFilters, Type_: "enums:NoOp,MOG,VariableFPS,KNN,Difference,Basic", Update: func(c *Config, v string) { filters := strings.Split(v, ",") m := map[string]uint{"NoOp": FilterNoOp, "MOG": FilterMOG, "VariableFPS": FilterVariableFPS, "KNN": FilterKNN, "Difference": FilterDiff, "Basic": FilterBasic} c.Filters = make([]uint, len(filters)) for i, filter := range filters { v, ok := m[filter] if !ok { c.Logger.Log(logger.Warning, "invalid Filters param", "value", v) } c.Filters[i] = uint(v) } }, }, { Name: KeyFrameRate, Type_: typeUint, Update: func(c *Config, v string) { c.FrameRate = parseUint(KeyFrameRate, v, c) }, Validate: func(c *Config) { if c.FrameRate <= 0 || c.FrameRate > 60 { c.LogInvalidField(KeyFrameRate, defaultFrameRate) c.FrameRate = defaultFrameRate } }, }, { Name: KeyHeight, Type_: typeUint, Update: func(c *Config, v string) { c.Height = parseUint(KeyHeight, v, c) }, }, { Name: KeyHorizontalFlip, Type_: typeBool, Update: func(c *Config, v string) { c.HorizontalFlip = parseBool(KeyHorizontalFlip, v, c) }, }, { Name: KeyHTTPAddress, Type_: typeString, Update: func(c *Config, v string) { c.HTTPAddress = v }, }, { Name: KeyInput, Type_: "enum:raspivid,raspistill,rtsp,v4l,file,audio", Update: func(c *Config, v string) { c.Input = parseEnum( KeyInput, v, map[string]uint8{ "raspivid": InputRaspivid, "raspistill": InputRaspistill, "rtsp": InputRTSP, "v4l": InputV4L, "file": InputFile, "audio": InputAudio, }, c, ) }, Validate: func(c *Config) { switch c.Input { case InputRaspivid, InputRaspistill, InputV4L, InputFile, InputAudio, InputRTSP: default: c.LogInvalidField(KeyInput, defaultInput) c.Input = defaultInput } }, }, { Name: KeyInputCodec, Type_: "enum:H264,H265,MJPEG,JPEG,PCM,ADPCM", Update: func(c *Config, v string) { c.InputCodec = parseEnum( KeyInputCodec, v, map[string]uint8{ "h264": codecutil.H264, "h265": codecutil.H265, "mjpeg": codecutil.MJPEG, "jpeg": codecutil.JPEG, "pcm": codecutil.PCM, "adpcm": codecutil.ADPCM, }, c, ) }, Validate: func(c *Config) { switch c.InputCodec { case codecutil.H264, codecutil.MJPEG, codecutil.JPEG, codecutil.PCM, codecutil.ADPCM: default: c.LogInvalidField(KeyInputCodec, defaultInputCodec) c.InputCodec = defaultInputCodec } }, }, { Name: KeyInputPath, Type_: typeString, Update: func(c *Config, v string) { c.InputPath = v }, }, { Name: KeyLogging, Type_: "enum:Debug,Info,Warning,Error,Fatal", Update: func(c *Config, v string) { switch v { case "Debug": c.LogLevel = logger.Debug case "Info": c.LogLevel = logger.Info case "Warning": c.LogLevel = logger.Warning case "Error": c.LogLevel = logger.Error case "Fatal": c.LogLevel = logger.Fatal default: c.Logger.Log(logger.Warning, "invalid Logging param", "value", v) } }, Validate: func(c *Config) { switch c.LogLevel { case logger.Debug, logger.Info, logger.Warning, logger.Error, logger.Fatal: default: c.LogInvalidField("LogLevel", defaultVerbosity) c.LogLevel = defaultVerbosity } }, }, { Name: KeyLoop, Type_: typeBool, Update: func(c *Config, v string) { c.Loop = parseBool(KeyLoop, v, c) }, }, { Name: KeyMinFPS, Type_: typeUint, Update: func(c *Config, v string) { c.MinFPS = parseUint(KeyMinFPS, v, c) }, Validate: func(c *Config) { c.MinFPS = lessThanOrEqual(KeyMinFPS, c.MinFPS, 0, c, defaultMinFPS) }, }, { Name: KeyMinFrames, Type_: typeUint, Update: func(c *Config, v string) { c.MinFrames = parseUint(KeyMinFrames, v, c) }, Validate: func(c *Config) { const maxMinFrames = 1000 if c.MinFrames <= 0 || c.MinFrames > maxMinFrames { c.LogInvalidField(KeyMinFrames, defaultMinFrames) c.MinFrames = defaultMinFrames } }, }, { Name: KeyMode, Type_: "enum:Normal,Paused,Burst,Loop", Update: func(c *Config, v string) { c.Loop = false if v == KeyLoop { c.Loop = true } }, }, { Name: KeyMotionDownscaling, Type_: typeUint, Update: func(c *Config, v string) { c.MotionDownscaling = parseUint(KeyMotionDownscaling, v, c) }, }, { Name: KeyMotionHistory, Type_: typeUint, Update: func(c *Config, v string) { c.MotionHistory = parseUint(KeyMotionHistory, v, c) }, }, { Name: KeyMotionInterval, Type_: typeUint, Update: func(c *Config, v string) { c.MotionInterval = parseUint(KeyMotionInterval, v, c) }, }, { Name: KeyMotionKernel, Type_: typeUint, Update: func(c *Config, v string) { c.MotionKernel = parseUint(KeyMotionKernel, v, c) }, }, { Name: KeyMotionMinArea, Type_: typeFloat, Update: func(c *Config, v string) { f, err := strconv.ParseFloat(v, 64) if err != nil { c.Logger.Log(logger.Warning, "invalid MotionMinArea var", "value", v) } c.MotionMinArea = f }, }, { Name: KeyMotionPadding, Type_: typeUint, Update: func(c *Config, v string) { c.MotionPadding = parseUint(KeyMotionPadding, v, c) }, }, { Name: KeyMotionPixels, Type_: typeUint, Update: func(c *Config, v string) { c.MotionPixels = parseUint(KeyMotionPixels, v, c) }, }, { Name: KeyMotionThreshold, Type_: typeFloat, Update: func(c *Config, v string) { f, err := strconv.ParseFloat(v, 64) if err != nil { c.Logger.Log(logger.Warning, "invalid MotionThreshold var", "value", v) } c.MotionThreshold = f }, }, { Name: KeyOutput, Type_: "enum:File,HTTP,RTMP,RTP", Update: func(c *Config, v string) { c.Outputs = make([]uint8, 1) switch strings.ToLower(v) { case "file": c.Outputs[0] = OutputFile case "files": c.Outputs[0] = OutputFiles case "http": c.Outputs[0] = OutputHTTP case "rtmp": c.Outputs[0] = OutputRTMP case "rtp": c.Outputs[0] = OutputRTP default: c.Logger.Log(logger.Warning, "invalid output param", "value", v) } }, }, { Name: KeyOutputPath, Type_: typeString, Update: func(c *Config, v string) { c.OutputPath = v }, }, { Name: KeyOutputs, Type_: "enums:File,HTTP,RTMP,RTP", Update: func(c *Config, v string) { outputs := strings.Split(v, ",") c.Outputs = make([]uint8, len(outputs)) for i, output := range outputs { switch strings.ToLower(output) { case "file": c.Outputs[i] = OutputFile case "files": c.Outputs[i] = OutputFiles case "http": c.Outputs[i] = OutputHTTP case "rtmp": c.Outputs[i] = OutputRTMP case "rtp": c.Outputs[i] = OutputRTP default: c.Logger.Log(logger.Warning, "invalid outputs param", "value", v) } } }, Validate: func(c *Config) { if c.Outputs == nil { c.LogInvalidField(KeyOutputs, defaultOutput) c.Outputs = append(c.Outputs, defaultOutput) } }, }, { Name: KeyPSITime, Type_: typeUint, Update: func(c *Config, v string) { c.PSITime = parseUint(KeyPSITime, v, c) }, Validate: func(c *Config) { c.PSITime = lessThanOrEqual(KeyPSITime, c.PSITime, 0, c, defaultPSITime) }, }, { Name: KeyQuantization, Type_: typeUint, Update: func(c *Config, v string) { c.Quantization = parseUint(KeyQuantization, v, c) }, }, { Name: KeyRBCapacity, Type_: typeUint, Update: func(c *Config, v string) { c.RBCapacity = parseUint(KeyRBCapacity, v, c) }, Validate: func(c *Config) { c.RBCapacity = lessThanOrEqual(KeyRBCapacity, c.RBCapacity, 0, c, defaultRBCapacity) }, }, { Name: KeyRBStartElementSize, Type_: typeUint, Update: func(c *Config, v string) { c.RBStartElementSize = parseUint("RBStartElementSize", v, c) }, Validate: func(c *Config) { c.RBStartElementSize = lessThanOrEqual("RBStartElementSize", c.RBStartElementSize, 0, c, defaultRBStartElementSize) }, }, { Name: KeyRBWriteTimeout, Type_: typeUint, Update: func(c *Config, v string) { c.RBWriteTimeout = parseUint(KeyRBWriteTimeout, v, c) }, Validate: func(c *Config) { c.RBWriteTimeout = lessThanOrEqual(KeyRBWriteTimeout, c.RBWriteTimeout, 0, c, defaultRBWriteTimeout) }, }, { Name: KeyRecPeriod, Type_: typeFloat, Update: func(c *Config, v string) { _v, err := strconv.ParseFloat(v, 64) if err != nil { c.Logger.Log(logger.Warning, fmt.Sprintf("invalid %s param", KeyRecPeriod), "value", v) } c.RecPeriod = _v }, }, { Name: KeyRotation, Type_: typeUint, Update: func(c *Config, v string) { c.Rotation = parseUint(KeyRotation, v, c) }, }, { Name: KeyRTMPURL, Type_: typeString, Update: func(c *Config, v string) { c.RTMPURL = v }, }, { Name: KeyRTPAddress, Type_: typeString, Update: func(c *Config, v string) { c.RTPAddress = v }, Validate: func(c *Config) { if c.RTPAddress == "" { c.LogInvalidField(KeyRTPAddress, defaultRTPAddr) c.RTPAddress = defaultRTPAddr } }, }, { Name: KeySampleRate, Type_: typeUint, Update: func(c *Config, v string) { c.SampleRate = parseUint(KeySampleRate, v, c) }, }, { Name: KeySaturation, Type_: typeInt, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, "invalid saturation param", "value", v) } c.Saturation = _v }, }, { Name: KeyJPEGQuality, Type_: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, "invalid JPEGQuality param", "value", v) } c.JPEGQuality = _v }, }, { Name: KeySuppress, Type_: typeBool, Update: func(c *Config, v string) { c.Suppress = parseBool(KeySuppress, v, c) c.Logger.(*logger.Logger).SetSuppress(c.Suppress) }, }, { Name: KeyTimelapseInterval, Type_: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, "invalid TimelapseInterval param", "value", v) } c.TimelapseInterval = time.Duration(_v) * time.Second }, }, { Name: KeyTimelapseDuration, Type_: typeUint, Update: func(c *Config, v string) { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, "invalid TimelapseDuration param", "value", v) } c.TimelapseDuration = time.Duration(_v) * time.Second }, }, { Name: KeyVBRBitrate, Type_: typeUint, Update: func(c *Config, v string) { c.VBRBitrate = parseUint(KeyVBRBitrate, v, c) }, }, { Name: KeyVBRQuality, Type_: "enum:standard,fair,good,great,excellent", Update: func(c *Config, v string) { c.VBRQuality = Quality(parseEnum( KeyVBRQuality, v, map[string]uint8{ "standard": uint8(QualityStandard), "fair": uint8(QualityFair), "good": uint8(QualityGood), "great": uint8(QualityGreat), "excellent": uint8(QualityExcellent), }, c, )) }, }, { Name: KeyVerticalFlip, Type_: typeBool, Update: func(c *Config, v string) { c.VerticalFlip = parseBool(KeyVerticalFlip, v, c) }, }, { Name: KeyWidth, Type_: typeUint, Update: func(c *Config, v string) { c.Width = parseUint(KeyWidth, v, c) }, }, } func parseUint(n, v string, c *Config) uint { _v, err := strconv.Atoi(v) if err != nil { c.Logger.Log(logger.Warning, fmt.Sprintf("invalid %s param", n), "value", v) } return uint(_v) } func parseBool(n, v string, c *Config) (b bool) { switch strings.ToLower(v) { case "true": b = true case "false": b = false default: c.Logger.Log(logger.Warning, fmt.Sprintf("invalid value for %s param", n), "value", v) } return } func parseEnum(n, v string, enums map[string]uint8, c *Config) uint8 { _v, ok := enums[strings.ToLower(v)] if !ok { c.Logger.Log(logger.Warning, fmt.Sprintf("invalid value for %s param", n), "value", v) } return _v } func lessThanOrEqual(n string, v, cmp uint, c *Config, def uint) uint { if v <= cmp { c.LogInvalidField(n, def) return def } return v }