2020-10-24 17:55:47 +00:00
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
2023-03-31 14:22:08 +00:00
|
|
|
"encoding/json"
|
2020-10-24 17:55:47 +00:00
|
|
|
"fmt"
|
2023-03-21 10:12:43 +00:00
|
|
|
"net"
|
2022-06-04 23:36:29 +00:00
|
|
|
gourl "net/url"
|
2023-02-13 11:12:04 +00:00
|
|
|
"reflect"
|
2020-10-24 17:55:47 +00:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2020-11-01 15:51:12 +00:00
|
|
|
"time"
|
2020-10-24 17:55:47 +00:00
|
|
|
|
2023-11-12 23:09:42 +00:00
|
|
|
"github.com/bluenviron/gortsplib/v4/pkg/base"
|
2020-10-24 17:55:47 +00:00
|
|
|
)
|
|
|
|
|
2021-07-22 15:47:59 +00:00
|
|
|
var rePathName = regexp.MustCompile(`^[0-9a-zA-Z_\-/\.~]+$`)
|
2021-01-16 14:42:54 +00:00
|
|
|
|
2024-01-23 19:52:05 +00:00
|
|
|
func isValidPathName(name string) error {
|
2021-01-16 14:42:54 +00:00
|
|
|
if name == "" {
|
|
|
|
return fmt.Errorf("cannot be empty")
|
|
|
|
}
|
|
|
|
|
|
|
|
if name[0] == '/' {
|
|
|
|
return fmt.Errorf("can't begin with a slash")
|
|
|
|
}
|
|
|
|
|
|
|
|
if name[len(name)-1] == '/' {
|
|
|
|
return fmt.Errorf("can't end with a slash")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !rePathName.MatchString(name) {
|
2021-07-24 22:24:22 +00:00
|
|
|
return fmt.Errorf("can contain only alphanumeric characters, underscore, dot, tilde, minus or slash")
|
2021-01-16 14:42:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-31 12:06:39 +00:00
|
|
|
|
2023-09-23 10:55:45 +00:00
|
|
|
func srtCheckPassphrase(passphrase string) error {
|
|
|
|
switch {
|
|
|
|
case len(passphrase) < 10 || len(passphrase) > 79:
|
|
|
|
return fmt.Errorf("must be between 10 and 79 characters")
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-23 19:52:05 +00:00
|
|
|
// FindPathConf returns the configuration corresponding to the given path name.
|
2024-09-08 18:33:18 +00:00
|
|
|
func FindPathConf(pathConfs map[string]*Path, name string) (*Path, []string, error) {
|
2024-01-23 19:52:05 +00:00
|
|
|
err := isValidPathName(name)
|
|
|
|
if err != nil {
|
2024-09-08 18:33:18 +00:00
|
|
|
return nil, nil, fmt.Errorf("invalid path name: %w (%s)", err, name)
|
2024-01-23 19:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// normal path
|
|
|
|
if pathConf, ok := pathConfs[name]; ok {
|
2024-09-08 18:33:18 +00:00
|
|
|
return pathConf, nil, nil
|
2024-01-23 19:52:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// regular expression-based path
|
|
|
|
for pathConfName, pathConf := range pathConfs {
|
|
|
|
if pathConf.Regexp != nil && pathConfName != "all" && pathConfName != "all_others" {
|
|
|
|
m := pathConf.Regexp.FindStringSubmatch(name)
|
|
|
|
if m != nil {
|
2024-09-08 18:33:18 +00:00
|
|
|
return pathConf, m, nil
|
2024-01-23 19:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 18:33:18 +00:00
|
|
|
// process all_others after every other entry
|
2024-01-23 19:52:05 +00:00
|
|
|
for pathConfName, pathConf := range pathConfs {
|
|
|
|
if pathConfName == "all" || pathConfName == "all_others" {
|
|
|
|
m := pathConf.Regexp.FindStringSubmatch(name)
|
|
|
|
if m != nil {
|
2024-09-08 18:33:18 +00:00
|
|
|
return pathConf, m, nil
|
2024-01-23 19:52:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-08 18:33:18 +00:00
|
|
|
return nil, nil, fmt.Errorf("path '%s' is not configured", name)
|
2024-01-23 19:52:05 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// Path is a path configuration.
|
2024-05-04 08:12:43 +00:00
|
|
|
// WARNING: Avoid using slices directly due to https://github.com/golang/go/issues/21092
|
2023-10-07 21:32:15 +00:00
|
|
|
type Path struct {
|
2023-10-28 13:06:06 +00:00
|
|
|
Regexp *regexp.Regexp `json:"-"` // filled by Check()
|
|
|
|
Name string `json:"name"` // filled by Check()
|
2021-04-16 20:46:22 +00:00
|
|
|
|
2023-08-30 09:24:14 +00:00
|
|
|
// General
|
|
|
|
Source string `json:"source"`
|
2021-09-27 08:36:28 +00:00
|
|
|
SourceFingerprint string `json:"sourceFingerprint"`
|
|
|
|
SourceOnDemand bool `json:"sourceOnDemand"`
|
|
|
|
SourceOnDemandStartTimeout StringDuration `json:"sourceOnDemandStartTimeout"`
|
|
|
|
SourceOnDemandCloseAfter StringDuration `json:"sourceOnDemandCloseAfter"`
|
2023-08-05 15:41:54 +00:00
|
|
|
MaxReaders int `json:"maxReaders"`
|
2023-09-28 17:40:24 +00:00
|
|
|
SRTReadPassphrase string `json:"srtReadPassphrase"`
|
2023-11-16 22:37:43 +00:00
|
|
|
Fallback string `json:"fallback"`
|
2023-10-07 21:48:37 +00:00
|
|
|
|
2024-04-09 08:04:12 +00:00
|
|
|
// Record
|
2023-10-07 21:48:37 +00:00
|
|
|
Record bool `json:"record"`
|
2024-04-09 08:04:12 +00:00
|
|
|
Playback *bool `json:"playback,omitempty"` // deprecated
|
2023-10-07 21:48:37 +00:00
|
|
|
RecordPath string `json:"recordPath"`
|
2023-10-14 20:52:10 +00:00
|
|
|
RecordFormat RecordFormat `json:"recordFormat"`
|
2023-10-07 21:48:37 +00:00
|
|
|
RecordPartDuration StringDuration `json:"recordPartDuration"`
|
|
|
|
RecordSegmentDuration StringDuration `json:"recordSegmentDuration"`
|
|
|
|
RecordDeleteAfter StringDuration `json:"recordDeleteAfter"`
|
2021-04-16 20:46:22 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
// Authentication (deprecated)
|
|
|
|
PublishUser *Credential `json:"publishUser,omitempty"` // deprecated
|
|
|
|
PublishPass *Credential `json:"publishPass,omitempty"` // deprecated
|
|
|
|
PublishIPs *IPNetworks `json:"publishIPs,omitempty"` // deprecated
|
|
|
|
ReadUser *Credential `json:"readUser,omitempty"` // deprecated
|
|
|
|
ReadPass *Credential `json:"readPass,omitempty"` // deprecated
|
|
|
|
ReadIPs *IPNetworks `json:"readIPs,omitempty"` // deprecated
|
2021-04-11 17:05:08 +00:00
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// Publisher source
|
2023-08-06 19:29:16 +00:00
|
|
|
OverridePublisher bool `json:"overridePublisher"`
|
2023-10-07 21:32:15 +00:00
|
|
|
DisablePublisherOverride *bool `json:"disablePublisherOverride,omitempty"` // deprecated
|
2023-09-23 10:55:45 +00:00
|
|
|
SRTPublishPassphrase string `json:"srtPublishPassphrase"`
|
2023-05-14 15:02:03 +00:00
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// RTSP source
|
2023-11-04 10:12:22 +00:00
|
|
|
RTSPTransport RTSPTransport `json:"rtspTransport"`
|
|
|
|
RTSPAnyPort bool `json:"rtspAnyPort"`
|
|
|
|
SourceProtocol *RTSPTransport `json:"sourceProtocol,omitempty"` // deprecated
|
|
|
|
SourceAnyPortEnable *bool `json:"sourceAnyPortEnable,omitempty"` // deprecated
|
2023-09-23 10:48:34 +00:00
|
|
|
RTSPRangeType RTSPRangeType `json:"rtspRangeType"`
|
|
|
|
RTSPRangeStart string `json:"rtspRangeStart"`
|
2023-05-14 15:02:03 +00:00
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// Redirect source
|
2023-05-14 15:02:03 +00:00
|
|
|
SourceRedirect string `json:"sourceRedirect"`
|
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// Raspberry Pi Camera source
|
2024-08-20 22:08:54 +00:00
|
|
|
RPICameraCamID uint `json:"rpiCameraCamID"`
|
|
|
|
RPICameraWidth uint `json:"rpiCameraWidth"`
|
|
|
|
RPICameraHeight uint `json:"rpiCameraHeight"`
|
2024-01-28 20:48:21 +00:00
|
|
|
RPICameraHFlip bool `json:"rpiCameraHFlip"`
|
|
|
|
RPICameraVFlip bool `json:"rpiCameraVFlip"`
|
|
|
|
RPICameraBrightness float64 `json:"rpiCameraBrightness"`
|
|
|
|
RPICameraContrast float64 `json:"rpiCameraContrast"`
|
|
|
|
RPICameraSaturation float64 `json:"rpiCameraSaturation"`
|
|
|
|
RPICameraSharpness float64 `json:"rpiCameraSharpness"`
|
|
|
|
RPICameraExposure string `json:"rpiCameraExposure"`
|
|
|
|
RPICameraAWB string `json:"rpiCameraAWB"`
|
|
|
|
RPICameraAWBGains []float64 `json:"rpiCameraAWBGains"`
|
|
|
|
RPICameraDenoise string `json:"rpiCameraDenoise"`
|
2024-08-20 22:08:54 +00:00
|
|
|
RPICameraShutter uint `json:"rpiCameraShutter"`
|
2024-01-28 20:48:21 +00:00
|
|
|
RPICameraMetering string `json:"rpiCameraMetering"`
|
|
|
|
RPICameraGain float64 `json:"rpiCameraGain"`
|
|
|
|
RPICameraEV float64 `json:"rpiCameraEV"`
|
|
|
|
RPICameraROI string `json:"rpiCameraROI"`
|
|
|
|
RPICameraHDR bool `json:"rpiCameraHDR"`
|
|
|
|
RPICameraTuningFile string `json:"rpiCameraTuningFile"`
|
|
|
|
RPICameraMode string `json:"rpiCameraMode"`
|
|
|
|
RPICameraFPS float64 `json:"rpiCameraFPS"`
|
|
|
|
RPICameraAfMode string `json:"rpiCameraAfMode"`
|
|
|
|
RPICameraAfRange string `json:"rpiCameraAfRange"`
|
|
|
|
RPICameraAfSpeed string `json:"rpiCameraAfSpeed"`
|
|
|
|
RPICameraLensPosition float64 `json:"rpiCameraLensPosition"`
|
|
|
|
RPICameraAfWindow string `json:"rpiCameraAfWindow"`
|
2024-08-20 22:08:54 +00:00
|
|
|
RPICameraFlickerPeriod uint `json:"rpiCameraFlickerPeriod"`
|
2024-01-28 20:48:21 +00:00
|
|
|
RPICameraTextOverlayEnable bool `json:"rpiCameraTextOverlayEnable"`
|
|
|
|
RPICameraTextOverlay string `json:"rpiCameraTextOverlay"`
|
2024-08-20 22:05:40 +00:00
|
|
|
RPICameraCodec string `json:"rpiCameraCodec"`
|
2024-08-20 22:08:54 +00:00
|
|
|
RPICameraIDRPeriod uint `json:"rpiCameraIDRPeriod"`
|
|
|
|
RPICameraBitrate uint `json:"rpiCameraBitrate"`
|
2024-08-20 22:05:40 +00:00
|
|
|
RPICameraProfile string `json:"rpiCameraProfile"`
|
|
|
|
RPICameraLevel string `json:"rpiCameraLevel"`
|
2023-05-14 15:02:03 +00:00
|
|
|
|
2023-09-23 10:49:08 +00:00
|
|
|
// Hooks
|
2023-09-29 16:24:10 +00:00
|
|
|
RunOnInit string `json:"runOnInit"`
|
|
|
|
RunOnInitRestart bool `json:"runOnInitRestart"`
|
|
|
|
RunOnDemand string `json:"runOnDemand"`
|
|
|
|
RunOnDemandRestart bool `json:"runOnDemandRestart"`
|
|
|
|
RunOnDemandStartTimeout StringDuration `json:"runOnDemandStartTimeout"`
|
|
|
|
RunOnDemandCloseAfter StringDuration `json:"runOnDemandCloseAfter"`
|
2023-11-04 12:07:51 +00:00
|
|
|
RunOnUnDemand string `json:"runOnUnDemand"`
|
2023-09-29 16:24:10 +00:00
|
|
|
RunOnReady string `json:"runOnReady"`
|
|
|
|
RunOnReadyRestart bool `json:"runOnReadyRestart"`
|
|
|
|
RunOnNotReady string `json:"runOnNotReady"`
|
|
|
|
RunOnRead string `json:"runOnRead"`
|
|
|
|
RunOnReadRestart bool `json:"runOnReadRestart"`
|
|
|
|
RunOnUnread string `json:"runOnUnread"`
|
2023-10-14 14:48:02 +00:00
|
|
|
RunOnRecordSegmentCreate string `json:"runOnRecordSegmentCreate"`
|
2023-09-29 16:24:10 +00:00
|
|
|
RunOnRecordSegmentComplete string `json:"runOnRecordSegmentComplete"`
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
func (pconf *Path) setDefaults() {
|
|
|
|
// General
|
|
|
|
pconf.Source = "publisher"
|
|
|
|
pconf.SourceOnDemandStartTimeout = 10 * StringDuration(time.Second)
|
|
|
|
pconf.SourceOnDemandCloseAfter = 10 * StringDuration(time.Second)
|
2023-10-07 21:48:37 +00:00
|
|
|
|
2024-04-09 08:04:12 +00:00
|
|
|
// Record
|
2023-10-07 21:48:37 +00:00
|
|
|
pconf.RecordPath = "./recordings/%path/%Y-%m-%d_%H-%M-%S-%f"
|
2023-10-14 20:52:10 +00:00
|
|
|
pconf.RecordFormat = RecordFormatFMP4
|
2024-04-17 22:11:28 +00:00
|
|
|
pconf.RecordPartDuration = StringDuration(1 * time.Second)
|
2023-10-07 21:48:37 +00:00
|
|
|
pconf.RecordSegmentDuration = 3600 * StringDuration(time.Second)
|
|
|
|
pconf.RecordDeleteAfter = 24 * 3600 * StringDuration(time.Second)
|
2023-10-07 21:32:15 +00:00
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// Publisher source
|
2023-10-07 21:32:15 +00:00
|
|
|
pconf.OverridePublisher = true
|
|
|
|
|
2023-10-28 12:53:23 +00:00
|
|
|
// Raspberry Pi Camera source
|
2023-10-07 21:32:15 +00:00
|
|
|
pconf.RPICameraWidth = 1920
|
|
|
|
pconf.RPICameraHeight = 1080
|
|
|
|
pconf.RPICameraContrast = 1
|
|
|
|
pconf.RPICameraSaturation = 1
|
|
|
|
pconf.RPICameraSharpness = 1
|
|
|
|
pconf.RPICameraExposure = "normal"
|
|
|
|
pconf.RPICameraAWB = "auto"
|
2024-01-28 20:48:21 +00:00
|
|
|
pconf.RPICameraAWBGains = []float64{0, 0}
|
2023-10-07 21:32:15 +00:00
|
|
|
pconf.RPICameraDenoise = "off"
|
|
|
|
pconf.RPICameraMetering = "centre"
|
|
|
|
pconf.RPICameraFPS = 30
|
2023-10-28 10:32:39 +00:00
|
|
|
pconf.RPICameraAfMode = "continuous"
|
2023-10-07 21:32:15 +00:00
|
|
|
pconf.RPICameraAfRange = "normal"
|
|
|
|
pconf.RPICameraAfSpeed = "normal"
|
|
|
|
pconf.RPICameraTextOverlay = "%Y-%m-%d %H:%M:%S - MediaMTX"
|
2024-08-20 22:05:40 +00:00
|
|
|
pconf.RPICameraCodec = "auto"
|
|
|
|
pconf.RPICameraIDRPeriod = 60
|
2024-10-20 23:25:32 +00:00
|
|
|
pconf.RPICameraBitrate = 5000000
|
2024-08-20 22:05:40 +00:00
|
|
|
pconf.RPICameraProfile = "main"
|
|
|
|
pconf.RPICameraLevel = "4.1"
|
2023-10-07 21:32:15 +00:00
|
|
|
|
|
|
|
// Hooks
|
|
|
|
pconf.RunOnDemandStartTimeout = 10 * StringDuration(time.Second)
|
|
|
|
pconf.RunOnDemandCloseAfter = 10 * StringDuration(time.Second)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newPath(defaults *Path, partial *OptionalPath) *Path {
|
|
|
|
pconf := &Path{}
|
|
|
|
copyStructFields(pconf, defaults)
|
|
|
|
copyStructFields(pconf, partial.Values)
|
|
|
|
return pconf
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone clones the configuration.
|
|
|
|
func (pconf Path) Clone() *Path {
|
|
|
|
enc, err := json.Marshal(pconf)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var dest Path
|
|
|
|
err = json.Unmarshal(enc, &dest)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dest.Regexp = pconf.Regexp
|
|
|
|
|
|
|
|
return &dest
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
func (pconf *Path) validate(
|
|
|
|
conf *Conf,
|
|
|
|
name string,
|
|
|
|
deprecatedCredentialsMode bool,
|
|
|
|
) error {
|
2023-10-28 13:06:06 +00:00
|
|
|
pconf.Name = name
|
|
|
|
|
2023-07-19 11:33:05 +00:00
|
|
|
switch {
|
2023-10-09 16:13:44 +00:00
|
|
|
case name == "all_others", name == "all":
|
2023-07-19 11:33:05 +00:00
|
|
|
pconf.Regexp = regexp.MustCompile("^.*$")
|
|
|
|
|
|
|
|
case name == "" || name[0] != '~': // normal path
|
2024-01-23 19:52:05 +00:00
|
|
|
err := isValidPathName(name)
|
2020-10-24 17:55:47 +00:00
|
|
|
if err != nil {
|
2024-01-03 20:13:20 +00:00
|
|
|
return fmt.Errorf("invalid path name '%s': %w", name, err)
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
|
|
|
|
2023-07-19 11:33:05 +00:00
|
|
|
default: // regular expression-based path
|
2023-10-07 21:32:15 +00:00
|
|
|
regexp, err := regexp.Compile(name[1:])
|
2020-10-24 17:55:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid regular expression: %s", name[1:])
|
|
|
|
}
|
2023-10-07 21:32:15 +00:00
|
|
|
pconf.Regexp = regexp
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
|
|
|
|
2023-08-30 09:24:14 +00:00
|
|
|
// General
|
|
|
|
|
2023-12-10 20:23:41 +00:00
|
|
|
if pconf.Source != "publisher" && pconf.Source != "redirect" &&
|
|
|
|
pconf.Regexp != nil && !pconf.SourceOnDemand {
|
|
|
|
return fmt.Errorf("a path with a regular expression (or path 'all') and a static source" +
|
|
|
|
" must have 'sourceOnDemand' set to true")
|
|
|
|
}
|
2021-03-20 13:14:41 +00:00
|
|
|
switch {
|
2021-08-07 14:08:29 +00:00
|
|
|
case pconf.Source == "publisher":
|
2020-11-01 15:51:12 +00:00
|
|
|
|
2021-03-20 13:14:41 +00:00
|
|
|
case strings.HasPrefix(pconf.Source, "rtsp://") ||
|
|
|
|
strings.HasPrefix(pconf.Source, "rtsps://"):
|
2023-11-12 23:09:42 +00:00
|
|
|
_, err := base.ParseURL(pconf.Source)
|
2020-10-24 17:55:47 +00:00
|
|
|
if err != nil {
|
2023-08-03 21:12:05 +00:00
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2020-10-27 23:29:53 +00:00
|
|
|
|
2022-08-16 11:53:04 +00:00
|
|
|
case strings.HasPrefix(pconf.Source, "rtmp://") ||
|
|
|
|
strings.HasPrefix(pconf.Source, "rtmps://"):
|
2022-06-04 23:36:29 +00:00
|
|
|
u, err := gourl.Parse(pconf.Source)
|
2020-10-24 17:55:47 +00:00
|
|
|
if err != nil {
|
2023-08-03 21:12:05 +00:00
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2020-10-27 23:29:53 +00:00
|
|
|
|
2020-10-24 17:55:47 +00:00
|
|
|
if u.User != nil {
|
|
|
|
pass, _ := u.User.Password()
|
|
|
|
user := u.User.Username()
|
|
|
|
if user != "" && pass == "" ||
|
|
|
|
user == "" && pass != "" {
|
2020-12-05 19:42:59 +00:00
|
|
|
return fmt.Errorf("username and password must be both provided")
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-05 15:35:04 +00:00
|
|
|
case strings.HasPrefix(pconf.Source, "http://") ||
|
|
|
|
strings.HasPrefix(pconf.Source, "https://"):
|
2022-06-04 23:36:29 +00:00
|
|
|
u, err := gourl.Parse(pconf.Source)
|
2021-09-05 15:35:04 +00:00
|
|
|
if err != nil {
|
2023-08-03 21:12:05 +00:00
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
if u.Scheme != "http" && u.Scheme != "https" {
|
2023-08-03 21:12:05 +00:00
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
2021-09-05 15:35:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if u.User != nil {
|
|
|
|
pass, _ := u.User.Password()
|
|
|
|
user := u.User.Username()
|
|
|
|
if user != "" && pass == "" ||
|
|
|
|
user == "" && pass != "" {
|
|
|
|
return fmt.Errorf("username and password must be both provided")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 10:12:43 +00:00
|
|
|
case strings.HasPrefix(pconf.Source, "udp://"):
|
2023-08-05 14:40:51 +00:00
|
|
|
_, _, err := net.SplitHostPort(pconf.Source[len("udp://"):])
|
2023-03-21 10:12:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'%s' is not a valid UDP URL", pconf.Source)
|
|
|
|
}
|
|
|
|
|
2023-07-31 19:20:09 +00:00
|
|
|
case strings.HasPrefix(pconf.Source, "srt://"):
|
|
|
|
|
|
|
|
_, err := gourl.Parse(pconf.Source)
|
|
|
|
if err != nil {
|
2023-08-03 21:12:05 +00:00
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
case strings.HasPrefix(pconf.Source, "whep://") ||
|
|
|
|
strings.HasPrefix(pconf.Source, "wheps://"):
|
|
|
|
_, err := gourl.Parse(pconf.Source)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'%s' is not a valid URL", pconf.Source)
|
2023-07-31 19:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 13:14:41 +00:00
|
|
|
case pconf.Source == "redirect":
|
2020-10-29 22:01:04 +00:00
|
|
|
|
2022-08-15 15:37:49 +00:00
|
|
|
case pconf.Source == "rpiCamera":
|
2022-10-23 14:44:34 +00:00
|
|
|
|
2021-03-20 13:14:41 +00:00
|
|
|
default:
|
2020-11-01 15:51:12 +00:00
|
|
|
return fmt.Errorf("invalid source: '%s'", pconf.Source)
|
|
|
|
}
|
2021-02-09 21:13:16 +00:00
|
|
|
if pconf.SourceOnDemand {
|
2021-08-07 14:08:29 +00:00
|
|
|
if pconf.Source == "publisher" {
|
|
|
|
return fmt.Errorf("'sourceOnDemand' is useless when source is 'publisher'")
|
2021-02-09 21:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-29 16:24:10 +00:00
|
|
|
if pconf.SRTReadPassphrase != "" {
|
|
|
|
err := srtCheckPassphrase(pconf.SRTReadPassphrase)
|
|
|
|
if err != nil {
|
2024-01-03 20:13:20 +00:00
|
|
|
return fmt.Errorf("invalid 'readRTPassphrase': %w", err)
|
2023-09-29 16:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
2023-11-16 22:37:43 +00:00
|
|
|
if pconf.Fallback != "" {
|
|
|
|
if strings.HasPrefix(pconf.Fallback, "/") {
|
2024-01-23 19:52:05 +00:00
|
|
|
err := isValidPathName(pconf.Fallback[1:])
|
2023-11-16 22:37:43 +00:00
|
|
|
if err != nil {
|
2024-01-03 20:13:20 +00:00
|
|
|
return fmt.Errorf("'%s': %w", pconf.Fallback, err)
|
2023-11-16 22:37:43 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_, err := base.ParseURL(pconf.Fallback)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'%s' is not a valid RTSP URL", pconf.Fallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-02-09 21:13:16 +00:00
|
|
|
|
2024-05-14 15:40:45 +00:00
|
|
|
// Record
|
|
|
|
|
|
|
|
if conf.Playback {
|
|
|
|
if !strings.Contains(pconf.RecordPath, "%Y") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%m") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%d") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%H") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%M") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%S") ||
|
|
|
|
!strings.Contains(pconf.RecordPath, "%f") {
|
|
|
|
return fmt.Errorf("record path '%s' is missing one of the mandatory elements"+
|
|
|
|
" for the playback server to work: %%Y %%m %%d %%H %%M %%S %%f",
|
|
|
|
pconf.RecordPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
// Authentication (deprecated)
|
2023-11-04 10:12:22 +00:00
|
|
|
|
2024-03-04 13:20:34 +00:00
|
|
|
if deprecatedCredentialsMode {
|
|
|
|
func() {
|
|
|
|
var user Credential = "any"
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.PublishUser != nil && *pconf.PublishUser != "" {
|
2024-03-04 13:20:34 +00:00
|
|
|
user = *pconf.PublishUser
|
|
|
|
}
|
|
|
|
|
|
|
|
var pass Credential
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.PublishPass != nil && *pconf.PublishPass != "" {
|
2024-03-04 13:20:34 +00:00
|
|
|
pass = *pconf.PublishPass
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := IPNetworks{mustParseCIDR("0.0.0.0/0")}
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.PublishIPs != nil && len(*pconf.PublishIPs) != 0 {
|
2024-03-04 13:20:34 +00:00
|
|
|
ips = *pconf.PublishIPs
|
|
|
|
}
|
|
|
|
|
|
|
|
pathName := name
|
|
|
|
if name == "all_others" || name == "all" {
|
|
|
|
pathName = "~^.*$"
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.AuthInternalUsers = append(conf.AuthInternalUsers, AuthInternalUser{
|
|
|
|
User: user,
|
|
|
|
Pass: pass,
|
|
|
|
IPs: ips,
|
|
|
|
Permissions: []AuthInternalUserPermission{{
|
|
|
|
Action: AuthActionPublish,
|
|
|
|
Path: pathName,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
}()
|
|
|
|
|
|
|
|
func() {
|
|
|
|
var user Credential = "any"
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.ReadUser != nil && *pconf.ReadUser != "" {
|
2024-03-04 13:20:34 +00:00
|
|
|
user = *pconf.ReadUser
|
|
|
|
}
|
|
|
|
|
|
|
|
var pass Credential
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.ReadPass != nil && *pconf.ReadPass != "" {
|
2024-03-04 13:20:34 +00:00
|
|
|
pass = *pconf.ReadPass
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := IPNetworks{mustParseCIDR("0.0.0.0/0")}
|
2024-06-12 15:38:55 +00:00
|
|
|
if pconf.ReadIPs != nil && len(*pconf.ReadIPs) != 0 {
|
2024-03-04 13:20:34 +00:00
|
|
|
ips = *pconf.ReadIPs
|
|
|
|
}
|
|
|
|
|
|
|
|
pathName := name
|
|
|
|
if name == "all_others" || name == "all" {
|
|
|
|
pathName = "~^.*$"
|
|
|
|
}
|
|
|
|
|
|
|
|
conf.AuthInternalUsers = append(conf.AuthInternalUsers, AuthInternalUser{
|
|
|
|
User: user,
|
|
|
|
Pass: pass,
|
|
|
|
IPs: ips,
|
|
|
|
Permissions: []AuthInternalUserPermission{{
|
|
|
|
Action: AuthActionRead,
|
|
|
|
Path: pathName,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
}()
|
2023-11-04 10:12:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Publisher source
|
2023-08-30 09:24:14 +00:00
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
if pconf.DisablePublisherOverride != nil {
|
|
|
|
pconf.OverridePublisher = !*pconf.DisablePublisherOverride
|
2023-08-06 19:29:16 +00:00
|
|
|
}
|
2023-09-29 16:24:10 +00:00
|
|
|
if pconf.SRTPublishPassphrase != "" {
|
|
|
|
if pconf.Source != "publisher" {
|
|
|
|
return fmt.Errorf("'srtPublishPassphase' can only be used when source is 'publisher'")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := srtCheckPassphrase(pconf.SRTPublishPassphrase)
|
|
|
|
if err != nil {
|
2024-01-03 20:13:20 +00:00
|
|
|
return fmt.Errorf("invalid 'srtPublishPassphrase': %w", err)
|
2023-09-29 16:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-01 16:48:00 +00:00
|
|
|
|
2023-11-04 10:12:22 +00:00
|
|
|
// RTSP source
|
2023-08-30 09:24:14 +00:00
|
|
|
|
2023-11-04 10:12:22 +00:00
|
|
|
if pconf.SourceProtocol != nil {
|
|
|
|
pconf.RTSPTransport = *pconf.SourceProtocol
|
2020-12-31 20:50:18 +00:00
|
|
|
}
|
2023-11-04 10:12:22 +00:00
|
|
|
if pconf.SourceAnyPortEnable != nil {
|
|
|
|
pconf.RTSPAnyPort = *pconf.SourceAnyPortEnable
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2023-11-04 10:12:22 +00:00
|
|
|
|
|
|
|
// Redirect source
|
|
|
|
|
|
|
|
if pconf.Source == "redirect" {
|
|
|
|
if pconf.SourceRedirect == "" {
|
|
|
|
return fmt.Errorf("source redirect must be filled")
|
|
|
|
}
|
|
|
|
|
2023-11-12 23:09:42 +00:00
|
|
|
_, err := base.ParseURL(pconf.SourceRedirect)
|
2023-11-04 10:12:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("'%s' is not a valid RTSP URL", pconf.SourceRedirect)
|
2023-05-08 15:04:14 +00:00
|
|
|
}
|
2021-12-22 18:13:56 +00:00
|
|
|
}
|
2023-11-04 10:12:22 +00:00
|
|
|
|
|
|
|
// Raspberry Pi Camera source
|
|
|
|
|
|
|
|
if pconf.Source == "rpiCamera" {
|
|
|
|
for otherName, otherPath := range conf.Paths {
|
|
|
|
if otherPath != pconf && otherPath != nil &&
|
|
|
|
otherPath.Source == "rpiCamera" && otherPath.RPICameraCamID == pconf.RPICameraCamID {
|
|
|
|
return fmt.Errorf("'rpiCamera' with same camera ID %d is used as source in two paths, '%s' and '%s'",
|
|
|
|
pconf.RPICameraCamID, name, otherName)
|
|
|
|
}
|
2023-05-08 15:04:14 +00:00
|
|
|
}
|
2021-12-22 18:13:56 +00:00
|
|
|
}
|
2023-11-04 10:12:22 +00:00
|
|
|
switch pconf.RPICameraExposure {
|
|
|
|
case "normal", "short", "long", "custom":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraExposure' value")
|
|
|
|
}
|
|
|
|
switch pconf.RPICameraAWB {
|
|
|
|
case "auto", "incandescent", "tungsten", "fluorescent", "indoor", "daylight", "cloudy", "custom":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraAWB' value")
|
|
|
|
}
|
2024-01-28 20:48:21 +00:00
|
|
|
if len(pconf.RPICameraAWBGains) != 2 {
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraAWBGains' value")
|
|
|
|
}
|
2023-11-04 10:12:22 +00:00
|
|
|
switch pconf.RPICameraDenoise {
|
|
|
|
case "off", "cdn_off", "cdn_fast", "cdn_hq":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraDenoise' value")
|
|
|
|
}
|
|
|
|
switch pconf.RPICameraMetering {
|
|
|
|
case "centre", "spot", "matrix", "custom":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraMetering' value")
|
|
|
|
}
|
|
|
|
switch pconf.RPICameraAfMode {
|
|
|
|
case "auto", "manual", "continuous":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraAfMode' value")
|
|
|
|
}
|
|
|
|
switch pconf.RPICameraAfRange {
|
|
|
|
case "normal", "macro", "full":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraAfRange' value")
|
|
|
|
}
|
|
|
|
switch pconf.RPICameraAfSpeed {
|
|
|
|
case "normal", "fast":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraAfSpeed' value")
|
|
|
|
}
|
2024-08-20 22:05:40 +00:00
|
|
|
switch pconf.RPICameraCodec {
|
|
|
|
case "auto", "hardwareH264", "softwareH264":
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("invalid 'rpiCameraCodec' value")
|
|
|
|
}
|
2021-12-22 18:13:56 +00:00
|
|
|
|
2023-09-23 10:49:08 +00:00
|
|
|
// Hooks
|
2023-08-30 09:24:14 +00:00
|
|
|
|
2020-11-01 15:51:12 +00:00
|
|
|
if pconf.RunOnInit != "" && pconf.Regexp != nil {
|
2023-09-29 16:24:10 +00:00
|
|
|
return fmt.Errorf("a path with a regular expression (or path 'all')" +
|
|
|
|
" does not support option 'runOnInit'; use another path")
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2023-11-04 12:07:51 +00:00
|
|
|
if (pconf.RunOnDemand != "" || pconf.RunOnUnDemand != "") && pconf.Source != "publisher" {
|
|
|
|
return fmt.Errorf("'runOnDemand' and 'runOnUnDemand' can be used only when source is 'publisher'")
|
2021-08-03 20:40:47 +00:00
|
|
|
}
|
|
|
|
|
2020-10-24 17:55:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-07 21:32:15 +00:00
|
|
|
// Equal checks whether two Paths are equal.
|
|
|
|
func (pconf *Path) Equal(other *Path) bool {
|
2023-02-13 11:12:04 +00:00
|
|
|
return reflect.DeepEqual(pconf, other)
|
2020-10-24 17:55:47 +00:00
|
|
|
}
|
2023-03-21 10:12:43 +00:00
|
|
|
|
|
|
|
// HasStaticSource checks whether the path has a static source.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (pconf Path) HasStaticSource() bool {
|
2024-10-14 12:47:51 +00:00
|
|
|
return pconf.Source != "publisher" && pconf.Source != "redirect"
|
2023-03-21 10:12:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasOnDemandStaticSource checks whether the path has a on demand static source.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (pconf Path) HasOnDemandStaticSource() bool {
|
2023-03-21 10:12:43 +00:00
|
|
|
return pconf.HasStaticSource() && pconf.SourceOnDemand
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasOnDemandPublisher checks whether the path has a on-demand publisher.
|
2023-10-07 21:32:15 +00:00
|
|
|
func (pconf Path) HasOnDemandPublisher() bool {
|
2023-03-21 10:12:43 +00:00
|
|
|
return pconf.RunOnDemand != ""
|
|
|
|
}
|