2022-09-17 19:19:45 +00:00
|
|
|
// Package conf contains the struct that holds the configuration of the software.
|
2020-10-13 22:50:08 +00:00
|
|
|
package conf
|
2020-07-12 11:16:33 +00:00
|
|
|
|
|
|
|
import (
|
2023-05-06 21:00:42 +00:00
|
|
|
"bytes"
|
2021-09-26 13:50:35 +00:00
|
|
|
"encoding/json"
|
2023-06-06 19:17:40 +00:00
|
|
|
"errors"
|
2020-07-12 11:16:33 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
2022-12-12 10:49:11 +00:00
|
|
|
"sort"
|
2021-12-22 18:13:56 +00:00
|
|
|
"strings"
|
2020-07-12 11:16:33 +00:00
|
|
|
"time"
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
"github.com/bluenviron/gohlslib"
|
2023-04-01 16:39:12 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v3"
|
|
|
|
"github.com/bluenviron/gortsplib/v3/pkg/headers"
|
2020-10-13 21:36:27 +00:00
|
|
|
|
2023-05-16 14:14:20 +00:00
|
|
|
"github.com/bluenviron/mediamtx/internal/conf/decrypt"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/conf/env"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/conf/yaml"
|
|
|
|
"github.com/bluenviron/mediamtx/internal/logger"
|
2020-07-12 11:16:33 +00:00
|
|
|
)
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
func getSortedKeys(paths map[string]*PathConf) []string {
|
|
|
|
ret := make([]string, len(paths))
|
|
|
|
i := 0
|
|
|
|
for name := range paths {
|
|
|
|
ret[i] = name
|
|
|
|
i++
|
2021-01-16 14:42:54 +00:00
|
|
|
}
|
2023-05-06 21:00:42 +00:00
|
|
|
sort.Strings(ret)
|
|
|
|
return ret
|
2021-01-16 14:42:54 +00:00
|
|
|
}
|
|
|
|
|
2021-09-26 13:50:35 +00:00
|
|
|
func loadFromFile(fpath string, conf *Conf) (bool, error) {
|
2023-04-01 17:32:10 +00:00
|
|
|
if fpath == "mediamtx.yml" {
|
|
|
|
// give priority to the legacy configuration file, in order not to break
|
|
|
|
// existing setups
|
|
|
|
if _, err := os.Stat("rtsp-simple-server.yml"); err == nil {
|
|
|
|
fpath = "rtsp-simple-server.yml"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// mediamtx.yml is optional
|
2021-09-26 13:50:35 +00:00
|
|
|
// other configuration files are not
|
2023-04-01 17:32:10 +00:00
|
|
|
if fpath == "mediamtx.yml" || fpath == "rtsp-simple-server.yml" {
|
2023-06-06 19:17:40 +00:00
|
|
|
if _, err := os.Stat(fpath); errors.Is(err, os.ErrNotExist) {
|
2023-05-06 21:00:42 +00:00
|
|
|
conf.UnmarshalJSON(nil) // load defaults
|
2021-09-26 13:50:35 +00:00
|
|
|
return false, nil
|
2021-07-04 16:13:49 +00:00
|
|
|
}
|
2021-09-26 13:50:35 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2022-09-17 19:19:45 +00:00
|
|
|
byts, err := os.ReadFile(fpath)
|
2021-09-26 13:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
2023-04-01 17:32:10 +00:00
|
|
|
if key, ok := os.LookupEnv("RTSP_CONFKEY"); ok { // legacy format
|
2023-05-06 21:00:42 +00:00
|
|
|
byts, err = decrypt.Decrypt(key, byts)
|
2023-04-01 17:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if key, ok := os.LookupEnv("MTX_CONFKEY"); ok {
|
2023-05-06 21:00:42 +00:00
|
|
|
byts, err = decrypt.Decrypt(key, byts)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
2021-09-26 13:50:35 +00:00
|
|
|
}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = yaml.Load(byts, conf)
|
2021-09-26 13:50:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2023-07-19 11:33:05 +00:00
|
|
|
func contains(list []headers.AuthMethod, item headers.AuthMethod) bool {
|
|
|
|
for _, i := range list {
|
|
|
|
if i == item {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-09-26 13:50:35 +00:00
|
|
|
// Conf is a configuration.
|
|
|
|
type Conf struct {
|
|
|
|
// general
|
2021-12-22 18:13:56 +00:00
|
|
|
LogLevel LogLevel `json:"logLevel"`
|
|
|
|
LogDestinations LogDestinations `json:"logDestinations"`
|
|
|
|
LogFile string `json:"logFile"`
|
|
|
|
ReadTimeout StringDuration `json:"readTimeout"`
|
|
|
|
WriteTimeout StringDuration `json:"writeTimeout"`
|
|
|
|
ReadBufferCount int `json:"readBufferCount"`
|
2023-03-31 09:53:49 +00:00
|
|
|
UDPMaxPayloadSize int `json:"udpMaxPayloadSize"`
|
2021-12-22 18:13:56 +00:00
|
|
|
ExternalAuthenticationURL string `json:"externalAuthenticationURL"`
|
|
|
|
API bool `json:"api"`
|
|
|
|
APIAddress string `json:"apiAddress"`
|
|
|
|
Metrics bool `json:"metrics"`
|
|
|
|
MetricsAddress string `json:"metricsAddress"`
|
|
|
|
PPROF bool `json:"pprof"`
|
|
|
|
PPROFAddress string `json:"pprofAddress"`
|
|
|
|
RunOnConnect string `json:"runOnConnect"`
|
|
|
|
RunOnConnectRestart bool `json:"runOnConnectRestart"`
|
2021-09-26 13:50:35 +00:00
|
|
|
|
|
|
|
// RTSP
|
2021-09-27 08:36:28 +00:00
|
|
|
RTSPDisable bool `json:"rtspDisable"`
|
|
|
|
Protocols Protocols `json:"protocols"`
|
|
|
|
Encryption Encryption `json:"encryption"`
|
|
|
|
RTSPAddress string `json:"rtspAddress"`
|
|
|
|
RTSPSAddress string `json:"rtspsAddress"`
|
|
|
|
RTPAddress string `json:"rtpAddress"`
|
|
|
|
RTCPAddress string `json:"rtcpAddress"`
|
|
|
|
MulticastIPRange string `json:"multicastIPRange"`
|
|
|
|
MulticastRTPPort int `json:"multicastRTPPort"`
|
|
|
|
MulticastRTCPPort int `json:"multicastRTCPPort"`
|
|
|
|
ServerKey string `json:"serverKey"`
|
|
|
|
ServerCert string `json:"serverCert"`
|
|
|
|
AuthMethods AuthMethods `json:"authMethods"`
|
2021-09-26 13:50:35 +00:00
|
|
|
|
|
|
|
// RTMP
|
2022-08-16 11:53:04 +00:00
|
|
|
RTMPDisable bool `json:"rtmpDisable"`
|
|
|
|
RTMPAddress string `json:"rtmpAddress"`
|
|
|
|
RTMPEncryption Encryption `json:"rtmpEncryption"`
|
|
|
|
RTMPSAddress string `json:"rtmpsAddress"`
|
|
|
|
RTMPServerKey string `json:"rtmpServerKey"`
|
|
|
|
RTMPServerCert string `json:"rtmpServerCert"`
|
2021-09-26 13:50:35 +00:00
|
|
|
|
|
|
|
// HLS
|
|
|
|
HLSDisable bool `json:"hlsDisable"`
|
|
|
|
HLSAddress string `json:"hlsAddress"`
|
2022-12-15 23:50:47 +00:00
|
|
|
HLSEncryption bool `json:"hlsEncryption"`
|
|
|
|
HLSServerKey string `json:"hlsServerKey"`
|
|
|
|
HLSServerCert string `json:"hlsServerCert"`
|
2021-09-26 13:50:35 +00:00
|
|
|
HLSAlwaysRemux bool `json:"hlsAlwaysRemux"`
|
2022-05-31 17:17:26 +00:00
|
|
|
HLSVariant HLSVariant `json:"hlsVariant"`
|
2021-09-26 13:50:35 +00:00
|
|
|
HLSSegmentCount int `json:"hlsSegmentCount"`
|
|
|
|
HLSSegmentDuration StringDuration `json:"hlsSegmentDuration"`
|
2022-05-31 17:17:26 +00:00
|
|
|
HLSPartDuration StringDuration `json:"hlsPartDuration"`
|
2022-01-29 15:10:08 +00:00
|
|
|
HLSSegmentMaxSize StringSize `json:"hlsSegmentMaxSize"`
|
2021-09-26 13:50:35 +00:00
|
|
|
HLSAllowOrigin string `json:"hlsAllowOrigin"`
|
2022-06-21 11:41:15 +00:00
|
|
|
HLSTrustedProxies IPsOrCIDRs `json:"hlsTrustedProxies"`
|
2023-03-19 23:22:21 +00:00
|
|
|
HLSDirectory string `json:"hlsDirectory"`
|
2021-09-26 13:50:35 +00:00
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
// WebRTC
|
2023-06-30 14:47:10 +00:00
|
|
|
WebRTCDisable bool `json:"webrtcDisable"`
|
|
|
|
WebRTCAddress string `json:"webrtcAddress"`
|
|
|
|
WebRTCEncryption bool `json:"webrtcEncryption"`
|
|
|
|
WebRTCServerKey string `json:"webrtcServerKey"`
|
|
|
|
WebRTCServerCert string `json:"webrtcServerCert"`
|
|
|
|
WebRTCAllowOrigin string `json:"webrtcAllowOrigin"`
|
|
|
|
WebRTCTrustedProxies IPsOrCIDRs `json:"webrtcTrustedProxies"`
|
|
|
|
WebRTCICEServers []string `json:"webrtcICEServers"` // deprecated
|
|
|
|
WebRTCICEServers2 []WebRTCICEServer `json:"webrtcICEServers2"`
|
|
|
|
WebRTCICEHostNAT1To1IPs []string `json:"webrtcICEHostNAT1To1IPs"`
|
|
|
|
WebRTCICEUDPMuxAddress string `json:"webrtcICEUDPMuxAddress"`
|
|
|
|
WebRTCICETCPMuxAddress string `json:"webrtcICETCPMuxAddress"`
|
2022-12-15 23:50:47 +00:00
|
|
|
|
2021-09-26 13:50:35 +00:00
|
|
|
// paths
|
|
|
|
Paths map[string]*PathConf `json:"paths"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads a Conf.
|
|
|
|
func Load(fpath string) (*Conf, bool, error) {
|
|
|
|
conf := &Conf{}
|
2021-07-04 16:13:49 +00:00
|
|
|
|
2021-09-26 13:50:35 +00:00
|
|
|
found, err := loadFromFile(fpath, conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = env.Load("RTSP", conf) // legacy prefix
|
2023-04-01 17:31:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = env.Load("MTX", conf)
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err = conf.Check()
|
2021-07-04 16:13:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, found, nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 14:22:08 +00:00
|
|
|
// Clone clones the configuration.
|
|
|
|
func (conf Conf) Clone() *Conf {
|
|
|
|
enc, err := json.Marshal(conf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dest Conf
|
|
|
|
err = json.Unmarshal(enc, &dest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &dest
|
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
// Check checks the configuration for errors.
|
|
|
|
func (conf *Conf) Check() error {
|
2022-12-15 23:50:47 +00:00
|
|
|
// general
|
2022-07-05 21:45:57 +00:00
|
|
|
if (conf.ReadBufferCount & (conf.ReadBufferCount - 1)) != 0 {
|
2023-03-31 09:53:49 +00:00
|
|
|
return fmt.Errorf("'readBufferCount' must be a power of two")
|
|
|
|
}
|
2023-04-15 11:45:20 +00:00
|
|
|
if conf.UDPMaxPayloadSize > 1472 {
|
|
|
|
return fmt.Errorf("'udpMaxPayloadSize' must be less than 1472")
|
2022-07-05 21:45:57 +00:00
|
|
|
}
|
2021-12-22 18:13:56 +00:00
|
|
|
if conf.ExternalAuthenticationURL != "" {
|
|
|
|
if !strings.HasPrefix(conf.ExternalAuthenticationURL, "http://") &&
|
|
|
|
!strings.HasPrefix(conf.ExternalAuthenticationURL, "https://") {
|
|
|
|
return fmt.Errorf("'externalAuthenticationURL' must be a HTTP URL")
|
|
|
|
}
|
2023-05-08 15:04:14 +00:00
|
|
|
|
|
|
|
if contains(conf.AuthMethods, headers.AuthDigest) {
|
|
|
|
return fmt.Errorf("'externalAuthenticationURL' can't be used when 'digest' is in authMethods")
|
|
|
|
}
|
2021-12-22 18:13:56 +00:00
|
|
|
}
|
2021-03-27 10:21:30 +00:00
|
|
|
|
2022-12-15 23:50:47 +00:00
|
|
|
// RTSP
|
2021-09-27 08:36:28 +00:00
|
|
|
if conf.Encryption == EncryptionStrict {
|
2021-10-22 16:41:10 +00:00
|
|
|
if _, ok := conf.Protocols[Protocol(gortsplib.TransportUDP)]; ok {
|
2021-11-05 16:11:40 +00:00
|
|
|
return fmt.Errorf("strict encryption can't be used with the UDP transport protocol")
|
|
|
|
}
|
|
|
|
if _, ok := conf.Protocols[Protocol(gortsplib.TransportUDPMulticast)]; ok {
|
|
|
|
return fmt.Errorf("strict encryption can't be used with the UDP-multicast transport protocol")
|
2020-12-13 22:43:31 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-15 23:50:47 +00:00
|
|
|
|
2023-05-15 08:51:00 +00:00
|
|
|
// WebRTC
|
|
|
|
for _, server := range conf.WebRTCICEServers {
|
2023-06-30 14:47:10 +00:00
|
|
|
parts := strings.Split(server, ":")
|
|
|
|
if len(parts) == 5 {
|
|
|
|
conf.WebRTCICEServers2 = append(conf.WebRTCICEServers2, WebRTCICEServer{
|
|
|
|
URL: parts[0] + ":" + parts[3] + ":" + parts[4],
|
|
|
|
Username: parts[1],
|
|
|
|
Password: parts[2],
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
conf.WebRTCICEServers2 = append(conf.WebRTCICEServers2, WebRTCICEServer{
|
|
|
|
URL: server,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
conf.WebRTCICEServers = nil
|
|
|
|
for _, server := range conf.WebRTCICEServers2 {
|
|
|
|
if !strings.HasPrefix(server.URL, "stun:") &&
|
|
|
|
!strings.HasPrefix(server.URL, "turn:") &&
|
|
|
|
!strings.HasPrefix(server.URL, "turns:") {
|
|
|
|
return fmt.Errorf("invalid ICE server: '%s'", server.URL)
|
2023-05-15 08:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 11:51:38 +00:00
|
|
|
// do not add automatically "all", since user may want to
|
|
|
|
// initialize all paths through API or hot reloading.
|
|
|
|
if conf.Paths == nil {
|
|
|
|
conf.Paths = make(map[string]*PathConf)
|
2020-07-12 11:16:33 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
for _, name := range getSortedKeys(conf.Paths) {
|
2022-12-12 10:49:11 +00:00
|
|
|
pconf := conf.Paths[name]
|
2020-09-19 15:13:45 +00:00
|
|
|
if pconf == nil {
|
2022-12-12 10:49:11 +00:00
|
|
|
pconf = &PathConf{}
|
2023-05-06 21:00:42 +00:00
|
|
|
pconf.UnmarshalJSON(nil) // fill defaults
|
2022-12-12 10:49:11 +00:00
|
|
|
conf.Paths[name] = pconf
|
2020-07-13 09:51:17 +00:00
|
|
|
}
|
|
|
|
|
2023-05-06 21:00:42 +00:00
|
|
|
err := pconf.check(conf, name)
|
2020-10-24 17:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-07-12 11:16:33 +00:00
|
|
|
}
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2020-07-12 11:16:33 +00:00
|
|
|
|
2020-10-24 17:55:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-05-06 21:00:42 +00:00
|
|
|
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler. It is used to set default values.
|
|
|
|
func (conf *Conf) UnmarshalJSON(b []byte) error {
|
|
|
|
// general
|
|
|
|
conf.LogLevel = LogLevel(logger.Info)
|
|
|
|
conf.LogDestinations = LogDestinations{logger.DestinationStdout}
|
|
|
|
conf.LogFile = "mediamtx.log"
|
|
|
|
conf.ReadTimeout = 10 * StringDuration(time.Second)
|
|
|
|
conf.WriteTimeout = 10 * StringDuration(time.Second)
|
|
|
|
conf.ReadBufferCount = 512
|
|
|
|
conf.UDPMaxPayloadSize = 1472
|
|
|
|
conf.APIAddress = "127.0.0.1:9997"
|
|
|
|
conf.MetricsAddress = "127.0.0.1:9998"
|
|
|
|
conf.PPROFAddress = "127.0.0.1:9999"
|
|
|
|
|
|
|
|
// RTSP
|
|
|
|
conf.Protocols = Protocols{
|
|
|
|
Protocol(gortsplib.TransportUDP): {},
|
|
|
|
Protocol(gortsplib.TransportUDPMulticast): {},
|
|
|
|
Protocol(gortsplib.TransportTCP): {},
|
|
|
|
}
|
|
|
|
conf.RTSPAddress = ":8554"
|
|
|
|
conf.RTSPSAddress = ":8322"
|
|
|
|
conf.RTPAddress = ":8000"
|
|
|
|
conf.RTCPAddress = ":8001"
|
|
|
|
conf.MulticastIPRange = "224.1.0.0/16"
|
|
|
|
conf.MulticastRTPPort = 8002
|
|
|
|
conf.MulticastRTCPPort = 8003
|
|
|
|
conf.ServerKey = "server.key"
|
|
|
|
conf.ServerCert = "server.crt"
|
2023-05-08 15:04:14 +00:00
|
|
|
conf.AuthMethods = AuthMethods{headers.AuthBasic}
|
2023-05-06 21:00:42 +00:00
|
|
|
|
|
|
|
// RTMP
|
|
|
|
conf.RTMPAddress = ":1935"
|
|
|
|
conf.RTMPSAddress = ":1936"
|
|
|
|
|
|
|
|
// HLS
|
|
|
|
conf.HLSAddress = ":8888"
|
|
|
|
conf.HLSServerKey = "server.key"
|
|
|
|
conf.HLSServerCert = "server.crt"
|
|
|
|
conf.HLSVariant = HLSVariant(gohlslib.MuxerVariantLowLatency)
|
|
|
|
conf.HLSSegmentCount = 7
|
|
|
|
conf.HLSSegmentDuration = 1 * StringDuration(time.Second)
|
|
|
|
conf.HLSPartDuration = 200 * StringDuration(time.Millisecond)
|
|
|
|
conf.HLSSegmentMaxSize = 50 * 1024 * 1024
|
|
|
|
conf.HLSAllowOrigin = "*"
|
|
|
|
|
|
|
|
// WebRTC
|
|
|
|
conf.WebRTCAddress = ":8889"
|
|
|
|
conf.WebRTCServerKey = "server.key"
|
|
|
|
conf.WebRTCServerCert = "server.crt"
|
|
|
|
conf.WebRTCAllowOrigin = "*"
|
2023-06-30 14:47:10 +00:00
|
|
|
conf.WebRTCICEServers2 = []WebRTCICEServer{{URL: "stun:stun.l.google.com:19302"}}
|
2023-05-06 21:00:42 +00:00
|
|
|
|
|
|
|
type alias Conf
|
|
|
|
d := json.NewDecoder(bytes.NewReader(b))
|
|
|
|
d.DisallowUnknownFields()
|
|
|
|
return d.Decode((*alias)(conf))
|
|
|
|
}
|