diff --git a/internal/streams/publish.go b/internal/streams/publish.go index 259ddb8d..a352352b 100644 --- a/internal/streams/publish.go +++ b/internal/streams/publish.go @@ -17,3 +17,16 @@ func (s *Stream) Publish(url string) error { return nil } + +func Publish(stream *Stream, destination any) { + switch v := destination.(type) { + case string: + if err := stream.Publish(v); err != nil { + log.Error().Err(err).Caller().Send() + } + case []any: + for _, v := range v { + Publish(stream, v) + } + } +} diff --git a/internal/streams/streams.go b/internal/streams/streams.go index 14bc09c8..6aa15dce 100644 --- a/internal/streams/streams.go +++ b/internal/streams/streams.go @@ -5,6 +5,7 @@ import ( "net/url" "regexp" "sync" + "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" @@ -13,18 +14,31 @@ import ( func Init() { var cfg struct { - Mod map[string]any `yaml:"streams"` + Streams map[string]any `yaml:"streams"` + Publish map[string]any `yaml:"publish"` } app.LoadConfig(&cfg) log = app.GetLogger("streams") - for name, item := range cfg.Mod { + for name, item := range cfg.Streams { streams[name] = NewStream(item) } api.HandleFunc("api/streams", streamsHandler) + + if cfg.Publish == nil { + return + } + + time.AfterFunc(5*time.Second, func() { + for name, dst := range cfg.Publish { + if stream := Get(name); stream != nil { + Publish(stream, dst) + } + } + }) } func Get(name string) *Stream {