av/revid/Config.go

302 lines
8.4 KiB
Go
Raw Normal View History

/*
NAME
Config.go
DESCRIPTION
See Readme.md
AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org>
LICENSE
Config.go is Copyright (C) 2017 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 [GNU licenses](http://www.gnu.org/licenses).
*/
2018-02-10 10:08:14 +03:00
package revid
import (
"errors"
2018-05-06 11:38:45 +03:00
_ "fmt"
"strconv"
2018-03-13 08:15:42 +03:00
"bitbucket.org/ausocean/utils/smartLogger"
//"../../utils/smartLogger"
)
2018-02-10 10:08:14 +03:00
// 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
2018-03-14 04:18:03 +03:00
RtmpMethod uint8
Packetization uint8
2018-03-14 04:18:03 +03:00
QuantizationMode uint8
2018-04-15 13:53:53 +03:00
Verbosity uint8
2018-05-06 11:38:45 +03:00
HorizontalFlip uint8
VerticalFlip uint8
FramesPerClip string
2018-02-10 10:08:14 +03:00
RtmpUrl string
Bitrate string
OutputFileName string
InputFileName string
Height string
Width string
FrameRate string
HttpAddress string
Quantization string
Timeout string
IntraRefreshPeriod string
Logger smartLogger.LogInstance
}
// Enums for config struct
const (
2018-03-14 04:18:03 +03:00
NothingDefined = 0
Raspivid = 1
Rtp = 2
H264Codec = 3
File = 4
Http = 5
H264 = 6
Mjpeg = 7
None = 8
Mpegts = 9
Ffmpeg = 11
Flv = 13
LibRtmp = 14
QuantizationOn = 15
QuantizationOff = 16
2018-03-17 16:29:42 +03:00
Yes = 17
No = 18
NativeRtmp = 19
2018-05-06 11:38:45 +03:00
FfmpegRtmp = 20
2018-02-10 10:08:14 +03:00
)
// Default config settings
const (
2018-05-06 11:38:45 +03:00
defaultInput = Raspivid
defaultOutput = NativeRtmp
defaultPacketization = Flv
defaultRtmpUrl = "rtmp://a.rtmp.youtube.com/live2/xt13-r4dh-f2w1-bh4s"
2018-02-10 10:08:14 +03:00
defaultFrameRate = "25"
defaultWidth = "1280"
defaultHeight = "720"
defaultIntraRefreshPeriod = "100"
defaultTimeout = "0"
2018-03-13 12:14:30 +03:00
defaultQuantization = "40"
2018-04-20 09:53:51 +03:00
defaultBitrate = "500000"
defaultQuantizationMode = QuantizationOff
defaultFramesPerClip = "1"
2018-05-06 11:38:45 +03:00
defaultVerticalFlip = No
defaultHorizontalFlip = No
2018-02-10 10:08:14 +03:00
)
2018-03-17 16:29:42 +03:00
// Validate checks for any errors in the config fields and defaults settings
2018-02-10 10:08:14 +03:00
// if particular parameters have not been defined.
2018-04-16 08:12:16 +03:00
func (config *Config) Validate(r *revid) error {
2018-03-17 16:29:42 +03:00
switch config.Verbosity {
case Yes:
case No:
case NothingDefined:
config.Verbosity = No
r.Log(Warning, "No verbosity mode defined, defaulting to no Verbosity!")
default:
return errors.New("Bad Verbosity defined in config!")
}
switch config.QuantizationMode {
case QuantizationOn:
case QuantizationOff:
case NothingDefined:
r.Log(Warning, "No quantization mode defined, defaulting to QuantizationOff!")
config.QuantizationMode = QuantizationOff
default:
2018-03-17 16:29:42 +03:00
return errors.New("Bad QuantizationMode defined in config!")
}
2018-02-10 10:08:14 +03:00
switch config.Input {
case Rtp:
case Raspivid:
case File:
case NothingDefined:
r.Log(Warning, "No input type defined, defaulting to raspivid!")
2018-04-20 09:53:51 +03:00
config.Input = defaultInput
2018-02-10 10:08:14 +03:00
default:
return errors.New("Bad input type defined in config!")
}
switch config.InputCodec {
case H264:
if config.Bitrate != "" && config.Quantization != "" {
2018-03-17 16:29:42 +03:00
bitrate, err := strconv.Atoi(config.Bitrate)
if err != nil {
return errors.New("Something is wrong with bitrate in conig!")
}
quantization, err := strconv.Atoi(config.Quantization)
if err != nil {
return errors.New("Something is wrong with quantization in config!")
}
2018-02-10 10:08:14 +03:00
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 != "" {
2018-03-17 16:29:42 +03:00
quantization, err := strconv.Atoi(config.Quantization)
if err != nil {
return errors.New("Something is wrong with quantization in config!")
}
2018-02-10 10:08:14 +03:00
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.Quantization = defaultQuantization
default:
return errors.New("Bad input codec defined in config!")
}
switch config.Output {
case Http:
2018-02-10 10:08:14 +03:00
case File:
case NativeRtmp:
if config.RtmpUrl == "" {
return errors.New("Bad RTMP URL")
}
r.Log(Info, "Defaulting frames per clip to 1 for rtmp output!")
config.FramesPerClip = "1"
case FfmpegRtmp:
2018-02-10 10:08:14 +03:00
if config.RtmpUrl == "" {
return errors.New("Bad RTMP URL")
}
2018-03-17 16:29:42 +03:00
r.Log(Info, "Defaulting frames per clip to 1 for rtmp output!")
config.FramesPerClip = "1"
2018-02-10 10:08:14 +03:00
case NothingDefined:
r.Log(Warning, "No output defined, defaulting to httpOut!")
2018-04-20 09:53:51 +03:00
config.Output = defaultOutput
2018-02-10 10:08:14 +03:00
default:
return errors.New("Bad output type defined in config!")
}
switch config.Packetization {
case None:
case Mpegts:
case Flv:
2018-02-10 10:08:14 +03:00
case NothingDefined:
r.Log(Warning, "No packetization option defined, defaulting to none!")
2018-04-20 09:53:51 +03:00
config.Packetization = Flv
2018-02-10 10:08:14 +03:00
default:
return errors.New("Bad packetization option defined in config!")
}
2018-05-03 15:36:17 +03:00
switch config.HorizontalFlip {
2018-05-03 11:27:36 +03:00
case Yes:
case No:
case NothingDefined:
2018-05-03 15:36:17 +03:00
r.Log(Warning, "No horizontal flip option defined, defaulting to not flipped!")
config.HorizontalFlip = defaultHorizontalFlip
2018-05-03 11:27:36 +03:00
default:
2018-05-03 15:36:17 +03:00
return errors.New("Bad horizontal flip option defined in config!")
}
switch config.VerticalFlip {
case Yes:
case No:
2018-05-05 06:40:04 +03:00
case NothingDefined:
2018-05-03 15:36:17 +03:00
r.Log(Warning, "No vertical flip option defined, defaulting to not flipped!")
config.VerticalFlip = defaultVerticalFlip
default:
return errors.New("Bad vertical flip option defined in config!")
2018-05-03 11:27:36 +03:00
}
2018-04-19 12:22:09 +03:00
if config.FramesPerClip == "" {
r.Log(Warning, "No FramesPerClip defined defined, defaulting to 1!")
config.Width = defaultFramesPerClip
} else {
2018-04-19 12:22:09 +03:00
if integer, err := strconv.Atoi(config.FramesPerClip); integer <= 0 || err != nil {
return errors.New("Bad width defined in config!")
}
2018-02-10 10:08:14 +03:00
}
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!")
}
}
2018-04-20 09:53:51 +03:00
if config.Bitrate == "" {
r.Log(Warning, "No bitrate defined, defaulting!")
config.Bitrate = defaultBitrate
} else {
2018-04-23 06:42:37 +03:00
if integer, err := strconv.Atoi(config.Bitrate); integer < 0 || err != nil {
2018-04-20 09:53:51 +03:00
return errors.New("Bad bitrate defined in config!")
}
}
2018-02-10 10:08:14 +03:00
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!")
}
}
return nil
2018-02-10 10:08:14 +03:00
}