diff --git a/revid/Config.go b/revid/Config.go new file mode 100644 index 00000000..3e7d0821 --- /dev/null +++ b/revid/Config.go @@ -0,0 +1,188 @@ +package revid + +// Config provides parameters relevant to a revid instance. A new config must +// be passed to the constructor. +type Config struct { + Input uint8 + InputCodec uint8 + Output uint8 + RtmpEncodingMethod uint8 + FramesPerClip uint + RtmpUrl string + Bitrate string + OutputFileName string + InputFileName string + Height string + Width string + FrameRate string + HttpAddress string + Quantization string + Timeout string + Packetization uint8 + IntraRefreshPeriod string + Logger smartLogger.LogInstance +} + +// Enums for config struct +const ( + NothingDefined = 0 + Raspivid = 1 + Rtp = 2 + H264Codec = 3 + File = 4 + HttpOut = 5 + H264 = 6 + Mjpeg = 7 + None = 8 + Mpegts = 9 + Rtmp = 10 + Ffmpeg = 11 + Revid = 12 + Flv = 13 +) + +// Default config settings +const ( + defaultFrameRate = "25" + defaultWidth = "1280" + defaultHeight = "720" + defaultIntraRefreshPeriod = "100" + defaultTimeout = "0" + defaultQuantization = "35" + defaultBitrate = "0" +) + +// checkConfig checks for any errors in the config files and defaults settings +// if particular parameters have not been defined. +func (config *Config) Validate(r *revidInst) error { + switch config.Input { + case Rtp: + case Raspivid: + case File: + case NothingDefined: + r.Log(Warning, "No input type defined, defaulting to raspivid!") + config.Input = Raspivid + default: + return errors.New("Bad input type defined in config!") + } + + switch config.InputCodec { + case H264: + if config.Bitrate != "" && config.Quantization != "" { + bitrate, _ := strconv.Atoi(config.Bitrate) + quantization, _ := strconv.Atoi(config.Quantization) + if (bitrate > 0 && quantization > 0) || (bitrate == 0 && quantization == 0) { + return errors.New("Bad bitrate and quantization combination for H264 input!") + } + } + case Mjpeg: + if config.Quantization != "" { + quantization, _ := strconv.Atoi(config.Quantization) + if quantization > 0 || config.Bitrate == "" { + return errors.New("Bad bitrate or quantization for mjpeg input!") + } + } + case NothingDefined: + r.Log(Warning, "No input codec defined, defaulting to h264!") + config.InputCodec = H264 + r.Log(Warning, "Defaulting bitrate to 0 and quantization to 35!") + config.Bitrate = defaultBitrate + config.Quantization = defaultQuantization + default: + return errors.New("Bad input codec defined in config!") + } + + switch config.Output { + case HttpOut: + case File: + case Rtmp: + switch config.RtmpEncodingMethod { + case Revid: + case Ffmpeg: + case NothingDefined: + r.Log(Warning, "No RTMP encoding method defined, defautling to ffmpeg!") + config.RtmpEncodingMethod = Ffmpeg + } + if config.RtmpUrl == "" { + return errors.New("Bad RTMP URL") + } + case NothingDefined: + r.Log(Warning, "No output defined, defaulting to httpOut!") + config.Output = HttpOut + default: + return errors.New("Bad output type defined in config!") + } + + switch config.Packetization { + case None: + case Mpegts: + case NothingDefined: + r.Log(Warning, "No packetization option defined, defaulting to none!") + config.Packetization = None + default: + return errors.New("Bad packetization option defined in config!") + } + + switch { + case config.FramesPerClip > 0: + case config.FramesPerClip == 0: + r.Log(Warning, "No frames per clip defined, defaulting to 1!") + config.FramesPerClip = 1 + case config.FramesPerClip < 0: + return errors.New("Bad frames per clip given!") + } + + if config.Width == "" { + r.Log(Warning, "No width defined, defaulting to 1280!") + config.Width = defaultWidth + } else { + if integer, err := strconv.Atoi(config.Width); integer < 0 || err != nil { + return errors.New("Bad width defined in config!") + } + } + + if config.Height == "" { + r.Log(Warning, "No height defined, defaulting to 720!") + config.Height = defaultHeight + } else { + if integer, err := strconv.Atoi(config.Height); integer < 0 || err != nil { + return errors.New("Bad height defined in config!") + } + } + + if config.FrameRate == "" { + r.Log(Warning, "No frame rate defined, defaulting to 25!") + config.FrameRate = defaultFrameRate + } else { + if integer, err := strconv.Atoi(config.FrameRate); integer < 0 || err != nil { + return errors.New("Bad frame rate defined in config!") + } + } + + if config.Timeout == "" { + r.Log(Warning, "No timeout defined, defaulting to 0!") + config.Timeout = defaultTimeout + } else { + if integer, err := strconv.Atoi(config.Timeout); integer < 0 || err != nil { + return errors.New("Bad timeout defined in config!") + } + } + + if config.IntraRefreshPeriod == "" { + r.Log(Warning, "No intra refresh defined, defaulting to 100!") + config.IntraRefreshPeriod = defaultIntraRefreshPeriod + } else { + if integer, err := strconv.Atoi(config.IntraRefreshPeriod); integer < 0 || err != nil { + return errors.New("Bad intra refresh defined in config!") + } + } + + if config.Quantization == "" { + r.Log(Warning, "No quantization defined, defaulting to 35!") + config.Quantization = defaultQuantization + } else { + if integer, err := strconv.Atoi(config.Quantization); integer < 0 || integer > 51 || err != nil { + return errors.New("Bad quantization defined in config!") + } + } +} diff --git a/revid/RevidInstance.go b/revid/RevidInstance.go index 6d95339d..1730e1d5 100644 --- a/revid/RevidInstance.go +++ b/revid/RevidInstance.go @@ -77,58 +77,6 @@ const ( Debug = "Debug" ) -// Config provides parameters relevant to a revid instance. A new config must -// be passed to the constructor. -type Config struct { - Input uint8 - InputCodec uint8 - Output uint8 - RtmpEncodingMethod uint8 - FramesPerClip uint - RtmpUrl string - Bitrate string - OutputFileName string - InputFileName string - Height string - Width string - FrameRate string - HttpAddress string - Quantization string - Timeout string - Packetization uint8 - IntraRefreshPeriod string - Logger smartLogger.LogInstance -} - -// Enums for config struct -const ( - NothingDefined = 0 - Raspivid = 1 - Rtp = 2 - H264Codec = 3 - File = 4 - HttpOut = 5 - H264 = 6 - Mjpeg = 7 - None = 8 - Mpegts = 9 - Rtmp = 10 - Ffmpeg = 11 - Revid = 12 - Flv = 13 -) - -// Default config settings -const ( - defaultFrameRate = "25" - defaultWidth = "1280" - defaultHeight = "720" - defaultIntraRefreshPeriod = "100" - defaultTimeout = "0" - defaultQuantization = "35" - defaultBitrate = "0" -) - // RevidInst provides methods to control a revidInst session; providing methods // to start, stop and change the state of an instance using the Config struct. type RevidInst interface { @@ -188,7 +136,7 @@ func (r *revidInst) GetConfigRef() *Config { // configuration; checking validity and returning errors if not valid. func (r *revidInst) ChangeState(config Config) error { r.config.Logger = config.Logger - err := checkConfig(config) + err := config.Validate(r) if err != nil { return errors.New("Config struct is bad!: " + err.Error()) } @@ -422,141 +370,6 @@ func (r *revidInst) sendClipToRtmp(clip []byte) error { return nil } -// checkConfig checks for any errors in the config files and defaults settings -// if particular parameters have not been defined. -func (r *revidInst) checkConfig(config Config) error { - switch config.Input { - case Rtp: - case Raspivid: - case File: - case NothingDefined: - r.Log(Warning, "No input type defined, defaulting to raspivid!") - config.Input = Raspivid - default: - return errors.New("Bad input type defined in config!") - } - - switch config.InputCodec { - case H264: - if config.Bitrate != "" && config.Quantization != "" { - bitrate, _ := strconv.Atoi(config.Bitrate) - quantization, _ := strconv.Atoi(config.Quantization) - if (bitrate > 0 && quantization > 0) || (bitrate == 0 && quantization == 0) { - return errors.New("Bad bitrate and quantization combination for H264 input!") - } - } - case Mjpeg: - if config.Quantization != "" { - quantization, _ := strconv.Atoi(config.Quantization) - if quantization > 0 || config.Bitrate == "" { - return errors.New("Bad bitrate or quantization for mjpeg input!") - } - } - case NothingDefined: - r.Log(Warning, "No input codec defined, defaulting to h264!") - config.InputCodec = H264 - r.Log(Warning, "Defaulting bitrate to 0 and quantization to 35!") - config.Bitrate = defaultBitrate - config.Quantization = defaultQuantization - default: - return errors.New("Bad input codec defined in config!") - } - - switch config.Output { - case HttpOut: - case File: - case Rtmp: - switch config.RtmpEncodingMethod { - case Revid: - case Ffmpeg: - case NothingDefined: - r.Log(Warning, "No RTMP encoding method defined, defautling to ffmpeg!") - config.RtmpEncodingMethod = Ffmpeg - } - if config.RtmpUrl == "" { - return errors.New("Bad RTMP URL") - } - case NothingDefined: - r.Log(Warning, "No output defined, defaulting to httpOut!") - config.Output = HttpOut - default: - return errors.New("Bad output type defined in config!") - } - - switch config.Packetization { - case None: - case Mpegts: - case NothingDefined: - r.Log(Warning, "No packetization option defined, defaulting to none!") - config.Packetization = None - default: - return errors.New("Bad packetization option defined in config!") - } - - switch { - case config.FramesPerClip > 0: - case config.FramesPerClip == 0: - r.Log(Warning, "No frames per clip defined, defaulting to 1!") - config.FramesPerClip = 1 - case config.FramesPerClip < 0: - return errors.New("Bad frames per clip given!") - } - - if config.Width == "" { - r.Log(Warning, "No width defined, defaulting to 1280!") - config.Width = defaultWidth - } else { - if integer, err := strconv.Atoi(config.Width); integer < 0 || err != nil { - return errors.New("Bad width defined in config!") - } - } - - if config.Height == "" { - r.Log(Warning, "No height defined, defaulting to 720!") - config.Height = defaultHeight - } else { - if integer, err := strconv.Atoi(config.Height); integer < 0 || err != nil { - return errors.New("Bad height defined in config!") - } - } - - if config.FrameRate == "" { - r.Log(Warning, "No frame rate defined, defaulting to 25!") - config.FrameRate = defaultFrameRate - } else { - if integer, err := strconv.Atoi(config.FrameRate); integer < 0 || err != nil { - return errors.New("Bad frame rate defined in config!") - } - } - - if config.Timeout == "" { - r.Log(Warning, "No timeout defined, defaulting to 0!") - config.Timeout = defaultTimeout - } else { - if integer, err := strconv.Atoi(config.Timeout); integer < 0 || err != nil { - return errors.New("Bad timeout defined in config!") - } - } - - if config.IntraRefreshPeriod == "" { - r.Log(Warning, "No intra refresh defined, defaulting to 100!") - config.IntraRefreshPeriod = defaultIntraRefreshPeriod - } else { - if integer, err := strconv.Atoi(config.IntraRefreshPeriod); integer < 0 || err != nil { - return errors.New("Bad intra refresh defined in config!") - } - } - - if config.Quantization == "" { - r.Log(Warning, "No quantization defined, defaulting to 35!") - config.Quantization = defaultQuantization - } else { - if integer, err := strconv.Atoi(config.Quantization); integer < 0 || integer > 51 || err != nil { - return errors.New("Bad quantization defined in config!") - } - } -} - // Start invokes a revidInst to start processing video from a defined input // and packetising to a defined output. func (r *revidInst) setupOutputForRtmp(){