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"
|
|
|
|
|
2019-01-02 08:09:47 +03:00
|
|
|
"bitbucket.org/ausocean/utils/logger"
|
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 {
|
2018-12-10 02:09:20 +03:00
|
|
|
LogLevel int8
|
|
|
|
|
|
|
|
Input uint8
|
|
|
|
InputCodec uint8
|
2019-01-19 12:09:43 +03:00
|
|
|
Outputs []uint8
|
2018-12-10 02:09:20 +03:00
|
|
|
RtmpMethod uint8
|
|
|
|
Packetization uint8
|
|
|
|
|
|
|
|
// Quantize specifies whether the input to
|
|
|
|
// revid will have constant or variable
|
|
|
|
// bitrate.
|
|
|
|
Quantize bool
|
2018-12-05 12:24:52 +03:00
|
|
|
|
|
|
|
// FlipHorizonatla and FlipVertical specify
|
|
|
|
// whether video frames should be flipped.
|
|
|
|
FlipHorizontal bool
|
|
|
|
FlipVertical bool
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
FramesPerClip uint
|
2018-02-10 10:08:14 +03:00
|
|
|
RtmpUrl string
|
2018-12-10 02:09:20 +03:00
|
|
|
Bitrate uint
|
2019-03-03 09:08:03 +03:00
|
|
|
OutputPath string
|
|
|
|
InputPath string
|
2018-12-10 02:09:20 +03:00
|
|
|
Height uint
|
|
|
|
Width uint
|
|
|
|
FrameRate uint
|
2018-02-10 10:08:14 +03:00
|
|
|
HttpAddress string
|
2018-12-10 02:09:20 +03:00
|
|
|
Quantization uint
|
|
|
|
IntraRefreshPeriod uint
|
2018-11-21 05:16:01 +03:00
|
|
|
RtpAddress string
|
2018-09-11 10:26:33 +03:00
|
|
|
Logger Logger
|
2018-12-14 13:20:26 +03:00
|
|
|
SendRetry bool
|
2019-02-03 16:13:51 +03:00
|
|
|
BurstPeriod uint
|
2019-03-04 04:41:34 +03:00
|
|
|
Rotation uint
|
2019-03-15 09:54:29 +03:00
|
|
|
Brightness uint
|
|
|
|
Saturation int
|
|
|
|
Exposure string
|
2019-03-15 10:28:24 +03:00
|
|
|
AutoWhiteBalance string
|
2019-03-15 09:54:29 +03:00
|
|
|
}
|
|
|
|
|
2019-03-15 10:28:24 +03:00
|
|
|
// Possible modes for raspivid --exposure parameter.
|
2019-03-15 09:54:29 +03:00
|
|
|
var ExposureModes = [...]string{
|
|
|
|
"auto",
|
|
|
|
"night",
|
|
|
|
"nightpreview",
|
|
|
|
"backlight",
|
|
|
|
"spotlight",
|
|
|
|
"sports",
|
|
|
|
"snow",
|
|
|
|
"beach",
|
|
|
|
"verylong",
|
|
|
|
"fixedfps",
|
|
|
|
"antishake",
|
|
|
|
"fireworks",
|
|
|
|
}
|
2019-03-15 05:14:13 +03:00
|
|
|
|
2019-03-15 10:28:24 +03:00
|
|
|
// Possible modes for raspivid --awb parameter.
|
|
|
|
var AutoWhiteBalanceModes = [...]string{
|
2019-03-15 09:54:29 +03:00
|
|
|
"off",
|
|
|
|
"auto",
|
|
|
|
"sun",
|
|
|
|
"cloud",
|
|
|
|
"shade",
|
|
|
|
"tungsten",
|
|
|
|
"fluorescent",
|
|
|
|
"incandescent",
|
|
|
|
"flash",
|
|
|
|
"horizon",
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enums for config struct
|
|
|
|
const (
|
2018-06-07 14:50:57 +03:00
|
|
|
NothingDefined = iota
|
|
|
|
Raspivid
|
2019-01-17 08:34:04 +03:00
|
|
|
V4L
|
2018-06-07 14:50:57 +03:00
|
|
|
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-11-18 05:52:48 +03:00
|
|
|
Udp
|
2018-11-21 05:22:28 +03:00
|
|
|
MpegtsRtp
|
2018-11-18 05:52:48 +03:00
|
|
|
Rtp
|
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-12-10 02:09:20 +03:00
|
|
|
defaultFrameRate = 25
|
|
|
|
defaultWidth = 1280
|
|
|
|
defaultHeight = 720
|
|
|
|
defaultIntraRefreshPeriod = 100
|
|
|
|
defaultTimeout = 0
|
|
|
|
defaultQuantization = 40
|
|
|
|
defaultBitrate = 400000
|
2018-03-14 04:02:22 +03:00
|
|
|
defaultQuantizationMode = QuantizationOff
|
2018-06-20 08:08:34 +03:00
|
|
|
defaultFramesPerClip = 1
|
2018-12-07 18:24:20 +03:00
|
|
|
httpFramesPerClip = 560
|
2018-09-15 05:56:05 +03:00
|
|
|
defaultInputCodec = H264
|
2019-05-05 13:09:56 +03:00
|
|
|
defaultVerbosity = logger.Error
|
2018-11-21 05:16:01 +03:00
|
|
|
defaultRtpAddr = "localhost:6970"
|
2019-02-03 16:13:51 +03:00
|
|
|
defaultBurstPeriod = 10 // Seconds
|
2019-03-04 04:41:34 +03:00
|
|
|
defaultRotation = 0 // Degrees
|
2019-03-18 03:32:11 +03:00
|
|
|
defaultBrightness = 50
|
|
|
|
defaultExposure = "auto"
|
|
|
|
defaultAutoWhiteBalance = "auto"
|
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 {
|
2019-05-05 13:09:56 +03:00
|
|
|
case logger.Debug:
|
|
|
|
case logger.Info:
|
|
|
|
case logger.Warning:
|
|
|
|
case logger.Error:
|
|
|
|
case logger.Fatal:
|
|
|
|
default:
|
2018-09-22 08:16:54 +03:00
|
|
|
c.LogLevel = defaultVerbosity
|
2019-05-05 13:09:56 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"bad LogLevel mode defined, defaulting",
|
2018-09-22 08:16:54 +03:00
|
|
|
"LogLevel", defaultVerbosity)
|
2018-03-17 16:29:42 +03:00
|
|
|
}
|
|
|
|
|
2018-06-09 05:01:21 +03:00
|
|
|
switch c.Input {
|
2019-01-17 08:34:04 +03:00
|
|
|
case Raspivid, V4L, File:
|
2018-02-10 10:08:14 +03:00
|
|
|
case NothingDefined:
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, 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-12-10 02:09:20 +03:00
|
|
|
// FIXME(kortschak): This is not really what we want.
|
|
|
|
// Configuration really needs to be rethought here.
|
|
|
|
if c.Quantize && c.Quantization == 0 {
|
|
|
|
c.Quantization = defaultQuantization
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
2018-12-10 02:09:20 +03:00
|
|
|
|
2019-02-25 08:26:51 +03:00
|
|
|
if (c.Bitrate > 0 && c.Quantize) || (c.Bitrate == 0 && !c.Quantize) {
|
2018-12-10 02:09:20 +03:00
|
|
|
return errors.New("bad bitrate and quantization combination for H264 input")
|
|
|
|
}
|
|
|
|
|
2018-02-10 10:08:14 +03:00
|
|
|
case Mjpeg:
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.Quantization > 0 || c.Bitrate == 0 {
|
|
|
|
return errors.New("bad bitrate or quantization for mjpeg input")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
2018-12-10 02:09:20 +03:00
|
|
|
|
2018-02-10 10:08:14 +03:00
|
|
|
case NothingDefined:
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no input codec defined, defaulting",
|
2018-09-15 05:56:05 +03:00
|
|
|
"inputCodec", defaultInputCodec)
|
|
|
|
c.InputCodec = defaultInputCodec
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, 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-12-10 02:09:20 +03:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-19 12:09:43 +03:00
|
|
|
for i, o := range c.Outputs {
|
|
|
|
switch o {
|
|
|
|
case File:
|
|
|
|
case Udp:
|
|
|
|
case Rtmp, FfmpegRtmp:
|
|
|
|
if c.RtmpUrl == "" {
|
|
|
|
c.Logger.Log(logger.Info, pkg+"no RTMP URL: falling back to HTTP")
|
|
|
|
c.Outputs[i] = Http
|
|
|
|
// FIXME(kortschak): Does this want the same line as below?
|
|
|
|
// c.FramesPerClip = httpFramesPerClip
|
|
|
|
break
|
|
|
|
}
|
|
|
|
c.Logger.Log(logger.Info, pkg+"defaulting frames per clip for rtmp out",
|
|
|
|
"framesPerClip", defaultFramesPerClip)
|
|
|
|
c.FramesPerClip = defaultFramesPerClip
|
2019-02-25 10:56:20 +03:00
|
|
|
c.Packetization = Flv
|
2019-03-03 09:08:03 +03:00
|
|
|
c.SendRetry = true
|
2019-01-19 12:09:43 +03:00
|
|
|
case NothingDefined:
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no output defined, defaulting", "output",
|
2019-01-19 12:09:43 +03:00
|
|
|
defaultOutput)
|
|
|
|
c.Outputs[i] = defaultOutput
|
2019-02-25 10:56:20 +03:00
|
|
|
c.Packetization = defaultPacketization
|
2019-01-19 12:09:43 +03:00
|
|
|
fallthrough
|
|
|
|
case Http, Rtp:
|
|
|
|
c.Logger.Log(logger.Info, pkg+"defaulting frames per clip for http out",
|
|
|
|
"framesPerClip", httpFramesPerClip)
|
|
|
|
c.FramesPerClip = httpFramesPerClip
|
2019-02-25 10:56:20 +03:00
|
|
|
c.Packetization = Mpegts
|
2019-01-19 12:09:43 +03:00
|
|
|
default:
|
|
|
|
return errors.New("bad output type defined in config")
|
2018-06-08 03:02:13 +03:00
|
|
|
}
|
2018-11-25 15:40:38 +03:00
|
|
|
}
|
|
|
|
|
2019-02-03 16:13:51 +03:00
|
|
|
if c.BurstPeriod == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no burst period defined, defaulting", "burstPeriod", defaultBurstPeriod)
|
2019-02-03 16:13:51 +03:00
|
|
|
c.BurstPeriod = defaultBurstPeriod
|
|
|
|
}
|
|
|
|
|
2018-06-20 08:08:34 +03:00
|
|
|
if c.FramesPerClip < 1 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, 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
|
|
|
}
|
|
|
|
|
2019-03-04 04:41:34 +03:00
|
|
|
if c.Rotation > 359 {
|
|
|
|
c.Logger.Log(logger.Warning, pkg+"bad rotate angle, defaulting", "angle", defaultRotation)
|
|
|
|
c.Rotation = defaultRotation
|
2019-03-04 04:37:28 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.Width == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no width defined, defaulting", "width", defaultWidth)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Width = defaultWidth
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.Height == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no height defined, defaulting", "height", defaultHeight)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Height = defaultHeight
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.FrameRate == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no frame rate defined, defaulting", "fps", defaultFrameRate)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.FrameRate = defaultFrameRate
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.Bitrate == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no bitrate defined, defaulting", "bitrate", defaultBitrate)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Bitrate = defaultBitrate
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.IntraRefreshPeriod == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no intra refresh defined, defaulting", "intraRefresh", defaultIntraRefreshPeriod)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.IntraRefreshPeriod = defaultIntraRefreshPeriod
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
|
|
|
|
2018-12-10 02:09:20 +03:00
|
|
|
if c.Quantization == 0 {
|
2019-03-04 05:17:57 +03:00
|
|
|
c.Logger.Log(logger.Info, pkg+"no quantization defined, defaulting", "quantization", defaultQuantization)
|
2018-06-09 05:01:21 +03:00
|
|
|
c.Quantization = defaultQuantization
|
2018-12-10 02:09:20 +03:00
|
|
|
} else if c.Quantization > 51 {
|
|
|
|
return errors.New("quantisation is over threshold")
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
2018-11-20 17:37:38 +03:00
|
|
|
|
2018-11-21 05:16:01 +03:00
|
|
|
if c.RtpAddress == "" {
|
|
|
|
c.RtpAddress = defaultRtpAddr
|
2018-11-20 17:37:38 +03:00
|
|
|
}
|
2019-03-15 09:54:29 +03:00
|
|
|
|
2019-03-18 03:32:11 +03:00
|
|
|
switch {
|
|
|
|
case c.Brightness == 0:
|
|
|
|
c.Logger.Log(logger.Info, pkg+"brightness undefined, defaulting", "brightness", defaultBrightness)
|
|
|
|
c.Brightness = defaultBrightness
|
|
|
|
case c.Brightness < 0 || c.Brightness > 100:
|
|
|
|
return errors.New(pkg + "bad brightness defined in config")
|
2019-03-15 09:54:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Saturation < -100 || c.Saturation > 100 {
|
2019-03-18 03:32:11 +03:00
|
|
|
return errors.New(pkg + "bad saturation setting in config")
|
2019-03-15 09:54:29 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 03:32:11 +03:00
|
|
|
switch {
|
|
|
|
case c.Exposure == "":
|
|
|
|
c.Logger.Log(logger.Info, pkg+"exposure undefined, defaulting", "exposure", defaultExposure)
|
|
|
|
c.Exposure = defaultExposure
|
|
|
|
case !stringInSlice(c.Exposure, ExposureModes[:]):
|
|
|
|
return errors.New(pkg + "bad exposure setting in config")
|
2019-03-15 09:54:29 +03:00
|
|
|
}
|
|
|
|
|
2019-03-18 03:32:11 +03:00
|
|
|
switch {
|
|
|
|
case c.AutoWhiteBalance == "":
|
|
|
|
c.Logger.Log(logger.Info, pkg+"auto white balance undefined, defaulting", "autoWhiteBalance", defaultAutoWhiteBalance)
|
|
|
|
c.AutoWhiteBalance = defaultAutoWhiteBalance
|
|
|
|
case !stringInSlice(c.AutoWhiteBalance, AutoWhiteBalanceModes[:]):
|
|
|
|
return errors.New(pkg + "bad auto white balance setting in config")
|
2019-03-15 09:54:29 +03:00
|
|
|
}
|
|
|
|
|
2018-02-12 10:58:29 +03:00
|
|
|
return nil
|
2018-02-10 10:08:14 +03:00
|
|
|
}
|
2019-03-15 09:54:29 +03:00
|
|
|
|
2019-03-15 10:28:24 +03:00
|
|
|
// stringInSlice returns true if want is in slice.
|
2019-03-15 09:54:29 +03:00
|
|
|
func stringInSlice(want string, slice []string) bool {
|
|
|
|
for _, s := range slice {
|
|
|
|
if s == want {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|