mediamtx/internal/conf/conf.go

256 lines
6.6 KiB
Go
Raw Normal View History

2020-10-13 22:50:08 +00:00
package conf
2020-07-12 11:16:33 +00:00
import (
"fmt"
"os"
"time"
"github.com/aler9/gortsplib"
2020-11-15 16:56:54 +00:00
"github.com/aler9/gortsplib/pkg/headers"
2020-07-12 11:16:33 +00:00
"gopkg.in/yaml.v2"
2020-11-01 21:56:56 +00:00
"github.com/aler9/rtsp-simple-server/internal/confenv"
"github.com/aler9/rtsp-simple-server/internal/logger"
2020-07-12 11:16:33 +00:00
)
2020-12-13 22:43:31 +00:00
// Encryption is an encryption policy.
type Encryption int
// encryption policies.
const (
EncryptionNo Encryption = iota
EncryptionOptional
EncryptionStrict
2020-12-13 22:43:31 +00:00
)
2020-11-07 21:47:10 +00:00
// Conf is the main program configuration.
2020-10-13 22:50:08 +00:00
type Conf struct {
LogLevel string `yaml:"logLevel"`
LogLevelParsed logger.Level `yaml:"-" json:"-"`
LogDestinations []string `yaml:"logDestinations"`
LogDestinationsParsed map[logger.Destination]struct{} `yaml:"-" json:"-"`
LogFile string `yaml:"logFile"`
Protocols []string `yaml:"protocols"`
ProtocolsParsed map[gortsplib.StreamProtocol]struct{} `yaml:"-" json:"-"`
2020-12-13 22:43:31 +00:00
Encryption string `yaml:"encryption"`
EncryptionParsed Encryption `yaml:"-" json:"-"`
RtspPort int `yaml:"rtspPort"`
2020-12-13 22:43:31 +00:00
RtspsPort int `yaml:"rtspsPort"`
RtpPort int `yaml:"rtpPort"`
RtcpPort int `yaml:"rtcpPort"`
2020-12-13 22:43:31 +00:00
ServerKey string `yaml:"serverKey"`
ServerCert string `yaml:"serverCert"`
AuthMethods []string `yaml:"authMethods"`
AuthMethodsParsed []headers.AuthMethod `yaml:"-" json:"-"`
2020-12-13 22:43:31 +00:00
ReadTimeout time.Duration `yaml:"readTimeout"`
WriteTimeout time.Duration `yaml:"writeTimeout"`
Metrics bool `yaml:"metrics"`
Pprof bool `yaml:"pprof"`
2020-12-13 22:43:31 +00:00
RunOnConnect string `yaml:"runOnConnect"`
RunOnConnectRestart bool `yaml:"runOnConnectRestart"`
2020-10-13 22:50:08 +00:00
Paths map[string]*PathConf `yaml:"paths"`
2020-07-12 11:16:33 +00:00
}
func (conf *Conf) fillAndCheck() error {
2020-12-13 22:43:31 +00:00
if conf.LogLevel == "" {
conf.LogLevel = "info"
}
switch conf.LogLevel {
case "warn":
conf.LogLevelParsed = logger.Warn
2020-12-13 22:43:31 +00:00
case "info":
conf.LogLevelParsed = logger.Info
case "debug":
conf.LogLevelParsed = logger.Debug
default:
return fmt.Errorf("unsupported log level: %s", conf.LogLevel)
}
if len(conf.LogDestinations) == 0 {
conf.LogDestinations = []string{"stdout"}
}
conf.LogDestinationsParsed = make(map[logger.Destination]struct{})
for _, dest := range conf.LogDestinations {
switch dest {
case "stdout":
conf.LogDestinationsParsed[logger.DestinationStdout] = struct{}{}
case "file":
conf.LogDestinationsParsed[logger.DestinationFile] = struct{}{}
case "syslog":
conf.LogDestinationsParsed[logger.DestinationSyslog] = struct{}{}
default:
return fmt.Errorf("unsupported log destination: %s", dest)
}
}
if conf.LogFile == "" {
conf.LogFile = "rtsp-simple-server.log"
}
2020-07-12 11:16:33 +00:00
if len(conf.Protocols) == 0 {
conf.Protocols = []string{"udp", "tcp"}
}
2020-10-13 22:50:08 +00:00
conf.ProtocolsParsed = make(map[gortsplib.StreamProtocol]struct{})
2020-07-12 11:16:33 +00:00
for _, proto := range conf.Protocols {
switch proto {
case "udp":
2020-10-13 22:50:08 +00:00
conf.ProtocolsParsed[gortsplib.StreamProtocolUDP] = struct{}{}
2020-07-12 11:16:33 +00:00
case "tcp":
2020-10-13 22:50:08 +00:00
conf.ProtocolsParsed[gortsplib.StreamProtocolTCP] = struct{}{}
2020-07-12 11:16:33 +00:00
default:
return fmt.Errorf("unsupported protocol: %s", proto)
2020-07-12 11:16:33 +00:00
}
}
2020-10-13 22:50:08 +00:00
if len(conf.ProtocolsParsed) == 0 {
return fmt.Errorf("no protocols provided")
2020-07-12 11:16:33 +00:00
}
2020-12-13 22:43:31 +00:00
if conf.Encryption == "" {
conf.Encryption = "no"
}
switch conf.Encryption {
case "no", "false":
2020-12-13 22:43:31 +00:00
conf.EncryptionParsed = EncryptionNo
case "optional":
conf.EncryptionParsed = EncryptionOptional
case "strict", "yes", "true":
conf.EncryptionParsed = EncryptionStrict
2020-12-13 22:43:31 +00:00
if _, ok := conf.ProtocolsParsed[gortsplib.StreamProtocolUDP]; ok {
return fmt.Errorf("encryption can't be used with the UDP stream protocol")
}
default:
return fmt.Errorf("unsupported encryption value: '%s'", conf.Encryption)
}
2020-07-12 11:16:33 +00:00
if conf.RtspPort == 0 {
conf.RtspPort = 8554
}
2020-12-13 22:43:31 +00:00
if conf.RtspsPort == 0 {
conf.RtspsPort = 8555
}
2020-07-12 11:16:33 +00:00
if conf.RtpPort == 0 {
conf.RtpPort = 8000
}
if (conf.RtpPort % 2) != 0 {
return fmt.Errorf("rtp port must be even")
2020-07-12 11:16:33 +00:00
}
if conf.RtcpPort == 0 {
conf.RtcpPort = 8001
}
if conf.RtcpPort != (conf.RtpPort + 1) {
return fmt.Errorf("rtcp and rtp ports must be consecutive")
2020-07-12 11:16:33 +00:00
}
2020-12-13 22:43:31 +00:00
if conf.ServerKey == "" {
conf.ServerKey = "server.key"
2020-07-12 11:16:33 +00:00
}
2020-12-13 22:43:31 +00:00
if conf.ServerCert == "" {
conf.ServerCert = "server.crt"
2020-07-12 11:16:33 +00:00
}
if len(conf.AuthMethods) == 0 {
conf.AuthMethods = []string{"basic", "digest"}
}
for _, method := range conf.AuthMethods {
switch method {
case "basic":
2020-10-13 22:50:08 +00:00
conf.AuthMethodsParsed = append(conf.AuthMethodsParsed, headers.AuthBasic)
2020-07-12 11:16:33 +00:00
case "digest":
2020-10-13 22:50:08 +00:00
conf.AuthMethodsParsed = append(conf.AuthMethodsParsed, headers.AuthDigest)
2020-07-12 11:16:33 +00:00
default:
return fmt.Errorf("unsupported authentication method: %s", method)
2020-07-12 11:16:33 +00:00
}
}
2020-12-13 22:43:31 +00:00
if conf.ReadTimeout == 0 {
conf.ReadTimeout = 10 * time.Second
}
if conf.WriteTimeout == 0 {
conf.WriteTimeout = 10 * time.Second
}
2020-07-12 11:16:33 +00:00
if len(conf.Paths) == 0 {
2020-10-13 22:50:08 +00:00
conf.Paths = map[string]*PathConf{
2020-07-12 11:16:33 +00:00
"all": {},
}
}
2020-09-19 15:13:45 +00:00
// "all" is an alias for "~^.*$"
if _, ok := conf.Paths["all"]; ok {
conf.Paths["~^.*$"] = conf.Paths["all"]
delete(conf.Paths, "all")
}
for name, pconf := range conf.Paths {
if pconf == nil {
2020-10-13 22:50:08 +00:00
conf.Paths[name] = &PathConf{}
2020-09-19 15:13:45 +00:00
pconf = conf.Paths[name]
}
err := pconf.fillAndCheck(name)
if err != nil {
return err
2020-07-12 11:16:33 +00:00
}
}
2020-07-12 11:16:33 +00:00
return nil
}
2020-10-03 19:10:41 +00:00
2020-11-05 11:30:25 +00:00
// Load loads a Conf.
func Load(fpath string) (*Conf, bool, error) {
conf := &Conf{}
2020-10-03 19:10:41 +00:00
// read from file
found, err := func() (bool, error) {
// rtsp-simple-server.yml is optional
if fpath == "rtsp-simple-server.yml" {
if _, err := os.Stat(fpath); err != nil {
return false, nil
2020-10-03 19:10:41 +00:00
}
2020-07-30 11:31:18 +00:00
}
f, err := os.Open(fpath)
if err != nil {
return false, err
2020-07-12 11:16:33 +00:00
}
defer f.Close()
2020-10-18 22:08:44 +00:00
err = yaml.NewDecoder(f).Decode(conf)
if err != nil {
return false, err
2020-07-12 11:16:33 +00:00
}
return true, nil
}()
if err != nil {
return nil, false, err
}
2020-10-18 22:08:44 +00:00
// read from environment
err = confenv.Load("RTSP", conf)
if err != nil {
return nil, false, err
}
2020-07-12 11:16:33 +00:00
err = conf.fillAndCheck()
if err != nil {
return nil, false, err
2020-07-12 11:16:33 +00:00
}
return conf, found, nil
2020-07-12 11:16:33 +00:00
}