2018-02-27 18:10:38 +03:00
|
|
|
/*
|
|
|
|
NAME
|
|
|
|
Config.go
|
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
See Readme.md
|
|
|
|
|
|
|
|
AUTHORS
|
2018-06-07 14:50:57 +03:00
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
2018-02-27 18:10:38 +03:00
|
|
|
|
|
|
|
LICENSE
|
2018-06-08 03:02:13 +03:00
|
|
|
Config.go is Copyright (C) 2017-2018 the Australian Ocean Lab (AusOcean)
|
2018-02-27 18:10:38 +03:00
|
|
|
|
|
|
|
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
|
2018-06-07 14:50:57 +03:00
|
|
|
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
|
2018-02-27 18:10:38 +03:00
|
|
|
*/
|
|
|
|
|
2018-02-10 10:08:14 +03:00
|
|
|
package revid
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strconv"
|
|
|
|
|
2018-05-24 06:35:29 +03:00
|
|
|
"bitbucket.org/ausocean/utils/smartlogger"
|
2018-02-12 10:58:29 +03:00
|
|
|
)
|
|
|
|
|
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
|
2018-02-27 18:10:38 +03:00
|
|
|
Packetization uint8
|
2018-03-14 04:18:03 +03:00
|
|
|
QuantizationMode uint8
|
2018-09-22 08:16:54 +03:00
|
|
|
LogLevel int8
|
2018-05-06 11:38:45 +03:00
|
|
|
HorizontalFlip uint8
|
|
|
|
VerticalFlip uint8
|
2018-06-20 08:08:34 +03:00
|
|
|
FramesPerClip int
|
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
|
2018-09-11 10:26:33 +03:00
|
|
|
Logger Logger
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enums for config struct
|
|
|
|
const (
|
2018-06-07 14:50:57 +03:00
|
|
|
NothingDefined = iota
|
|
|
|
Raspivid
|
|
|
|
H264Codec
|
|
|
|
File
|
|
|
|
Http
|
|
|
|
H264
|
|
|
|
Mjpeg
|
|
|
|
None
|
|
|
|
Mpegts
|
|
|
|
Ffmpeg
|
|
|
|
Flv
|
|
|
|
LibRtmp
|
|
|
|
QuantizationOn
|
|
|
|
QuantizationOff
|
|
|
|
Yes
|
|
|
|
No
|
2018-06-25 04:23:26 +03:00
|
|
|
Rtmp
|
2018-06-07 14:50:57 +03:00
|
|
|
FfmpegRtmp
|
2018-02-10 10:08:14 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// Default config settings
|
|
|
|
const (
|
2018-05-06 11:38:45 +03:00
|
|
|
defaultInput = Raspivid
|
2018-06-08 03:02:13 +03:00
|
|
|
defaultOutput = Http
|
2018-05-06 11:38:45 +03:00
|
|
|
defaultPacketization = Flv
|
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"
|
2018-03-14 04:02:22 +03:00
|
|
|
defaultQuantizationMode = QuantizationOff
|
2018-06-20 08:08:34 +03:00
|
|
|
defaultFramesPerClip = 1
|
2018-05-06 11:38:45 +03:00
|
|
|
defaultVerticalFlip = No
|
|
|
|
defaultHorizontalFlip = No
|
2018-09-25 10:40:35 +03:00
|
|
|
httpFramesPerClip = 7
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultInputCodec = H264
|
|
|
|
defaultVerbosity = 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-06-09 05:01:21 +03:00
|
|
|
func (c *Config) Validate(r *Revid) error {
|
2018-09-22 08:16:54 +03:00
|
|
|
switch c.LogLevel {
|
2018-03-17 16:29:42 +03:00
|
|
|
case Yes:
|
|
|
|
case No:
|
|
|
|
case NothingDefined:
|
2018-09-22 08:16:54 +03:00
|
|
|
c.LogLevel = defaultVerbosity
|
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no LogLevel mode defined, defaulting",
|
|
|
|
"LogLevel", defaultVerbosity)
|
2018-03-17 16:29:42 +03:00
|
|
|
default:
|
2018-09-22 08:16:54 +03:00
|
|
|
return errors.New("bad LogLevel defined in config")
|
2018-03-17 16:29:42 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.QuantizationMode {
|
2018-03-14 04:02:22 +03:00
|
|
|
case QuantizationOn:
|
|
|
|
case QuantizationOff:
|
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no quantization mode defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"quantizationMode", QuantizationOff)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.QuantizationMode = QuantizationOff
|
2018-03-14 04:02:22 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad QuantizationMode defined in config")
|
2018-03-14 04:02:22 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.Input {
|
2018-02-10 10:08:14 +03:00
|
|
|
case Raspivid:
|
|
|
|
case File:
|
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no input type defined, defaulting", "input",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultInput)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Input = defaultInput
|
2018-02-10 10:08:14 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad input type defined in config")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.InputCodec {
|
2018-02-10 10:08:14 +03:00
|
|
|
case H264:
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Bitrate != "" && c.Quantization != "" {
|
|
|
|
bitrate, err := strconv.Atoi(c.Bitrate)
|
2018-03-17 16:29:42 +03:00
|
|
|
if err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("bitrate not an integer")
|
2018-03-17 16:29:42 +03:00
|
|
|
}
|
2018-06-09 05:01:21 +03:00
|
|
|
quantization, err := strconv.Atoi(c.Quantization)
|
2018-03-17 16:29:42 +03:00
|
|
|
if err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("quantization not an integer")
|
2018-03-17 16:29:42 +03:00
|
|
|
}
|
2018-02-10 10:08:14 +03:00
|
|
|
if (bitrate > 0 && quantization > 0) || (bitrate == 0 && quantization == 0) {
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad bitrate and quantization combination for H264 input")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case Mjpeg:
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Quantization != "" {
|
|
|
|
quantization, err := strconv.Atoi(c.Quantization)
|
2018-03-17 16:29:42 +03:00
|
|
|
if err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("quantization not an integer")
|
2018-03-17 16:29:42 +03:00
|
|
|
}
|
2018-06-09 05:01:21 +03:00
|
|
|
if quantization > 0 || c.Bitrate == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad bitrate or quantization for mjpeg input")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no input codec defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"inputCodec", defaultInputCodec)
|
|
|
|
c.InputCodec = defaultInputCodec
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"defaulting quantization", "quantization",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultQuantization)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Quantization = defaultQuantization
|
2018-02-10 10:08:14 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad input codec defined in config")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.Output {
|
2018-02-10 10:08:14 +03:00
|
|
|
case File:
|
2018-06-25 04:23:26 +03:00
|
|
|
case Rtmp, FfmpegRtmp:
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.RtmpUrl == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Info, pkg+"no RTMP URL: falling back to HTTP")
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Output = Http
|
2018-06-08 03:02:13 +03:00
|
|
|
break
|
|
|
|
}
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Info, pkg+"defaulting frames per clip for rtmp out",
|
2018-09-15 05:56:05 +03:00
|
|
|
"framesPerClip", defaultFramesPerClip)
|
|
|
|
c.FramesPerClip = defaultFramesPerClip
|
2018-02-10 10:08:14 +03:00
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no output defined, defaulting", "output",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultOutput)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Output = defaultOutput
|
2018-06-20 06:18:08 +03:00
|
|
|
fallthrough
|
|
|
|
case Http:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Info, pkg+"defaulting frames per clip for http out",
|
2018-09-15 05:56:05 +03:00
|
|
|
"framesPerClip", httpFramesPerClip)
|
|
|
|
c.FramesPerClip = httpFramesPerClip
|
2018-02-10 10:08:14 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad output type defined in config")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.Packetization {
|
2018-02-10 10:08:14 +03:00
|
|
|
case None:
|
|
|
|
case Mpegts:
|
2018-02-12 10:58:29 +03:00
|
|
|
case Flv:
|
2018-02-10 10:08:14 +03:00
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no packetization option defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"packetization", defaultPacketization)
|
|
|
|
c.Packetization = defaultPacketization
|
2018-02-10 10:08:14 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad packetization option defined in config")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.HorizontalFlip {
|
2018-05-03 11:27:36 +03:00
|
|
|
case Yes:
|
|
|
|
case No:
|
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no horizontal flip option defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"horizontalFlip", defaultHorizontalFlip)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.HorizontalFlip = defaultHorizontalFlip
|
2018-05-03 11:27:36 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad horizontal flip option defined in config")
|
2018-05-03 15:36:17 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.VerticalFlip {
|
2018-05-03 15:36:17 +03:00
|
|
|
case Yes:
|
|
|
|
case No:
|
2018-05-05 06:40:04 +03:00
|
|
|
case NothingDefined:
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no vertical flip option defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"verticalFlip", defaultVerticalFlip)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.VerticalFlip = defaultVerticalFlip
|
2018-05-03 15:36:17 +03:00
|
|
|
default:
|
2018-09-20 05:04:17 +03:00
|
|
|
return errors.New("bad vertical flip option defined in config")
|
2018-05-03 11:27:36 +03:00
|
|
|
}
|
|
|
|
|
2018-06-20 08:08:34 +03:00
|
|
|
if c.FramesPerClip < 1 {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no FramesPerClip defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"framesPerClip", defaultFramesPerClip)
|
2018-06-13 08:15:03 +03:00
|
|
|
c.FramesPerClip = defaultFramesPerClip
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Width == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no width defined, defaulting", "width",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultWidth)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Width = defaultWidth
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.Width); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("width not unsigned integer")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Height == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no height defined, defaulting", "height",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultHeight)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Height = defaultHeight
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.Height); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("height not unsigned integer")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.FrameRate == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no frame rate defined, defaulting", "fps",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultFrameRate)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.FrameRate = defaultFrameRate
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.FrameRate); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("frame rate not unsigned integer")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Bitrate == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no bitrate defined, defaulting", "bitrate",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultBitrate)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Bitrate = defaultBitrate
|
2018-04-20 09:53:51 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.Bitrate); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("bitrate not unsigned integer")
|
2018-04-20 09:53:51 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Timeout == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no timeout defined, defaulting", "timeout", defaultTimeout)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Timeout = defaultTimeout
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.Timeout); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("timeout not unsigned integer")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.IntraRefreshPeriod == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no intra refresh defined, defaulting", "intraRefresh",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultIntraRefreshPeriod)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.IntraRefreshPeriod = defaultIntraRefreshPeriod
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.IntraRefreshPeriod); integer < 0 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("intra refresh not unsigned integer")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
if c.Quantization == "" {
|
2018-09-20 05:04:17 +03:00
|
|
|
c.Logger.Log(smartlogger.Warning, pkg+"no quantization defined, defaulting", "quantization",
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultQuantization)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Quantization = defaultQuantization
|
2018-02-10 10:08:14 +03:00
|
|
|
} else {
|
2018-06-09 05:01:21 +03:00
|
|
|
if integer, err := strconv.Atoi(c.Quantization); integer < 0 || integer > 51 || err != nil {
|
2018-09-22 08:15:07 +03:00
|
|
|
return errors.New("quantisation not unsigned integer or is over threshold")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-12 10:58:29 +03:00
|
|
|
return nil
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|