From 95a5283c86ff8945b193101610be16433e132adc Mon Sep 17 00:00:00 2001 From: Robert Resch Date: Tue, 22 Oct 2024 16:31:31 +0200 Subject: [PATCH 1/2] Extend streams API to allow multiple sources --- internal/homekit/api.go | 2 +- internal/streams/api.go | 32 ++++++++++++++++++++++++++++---- internal/streams/stream.go | 6 ++++++ internal/streams/streams.go | 12 +++++++----- 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/internal/homekit/api.go b/internal/homekit/api.go index abd8e97c..bd01259d 100644 --- a/internal/homekit/api.go +++ b/internal/homekit/api.go @@ -101,7 +101,7 @@ func apiPair(id, url string) error { return err } - streams.New(id, conn.URL()) + streams.New(id, []string{conn.URL()}) return app.PatchConfig(id, conn.URL(), "streams") } diff --git a/internal/streams/api.go b/internal/streams/api.go index d64c4846..7c635089 100644 --- a/internal/streams/api.go +++ b/internal/streams/api.go @@ -1,6 +1,7 @@ package streams import ( + "encoding/json" "net/http" "github.com/AlexxIT/go2rtc/internal/api" @@ -8,13 +9,18 @@ import ( "github.com/AlexxIT/go2rtc/pkg/probe" ) +func returnAllStreams(w http.ResponseWriter) { + api.ResponseJSON(w, streams) +} + func apiStreams(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() src := query.Get("src") // without source - return all streams list - if src == "" && r.Method != "POST" { - api.ResponseJSON(w, streams) + // PUT checks first body for sources + if src == "" && r.Method != "POST" && r.Method != "PUT" { + returnAllStreams(w) return } @@ -47,13 +53,31 @@ func apiStreams(w http.ResponseWriter, r *http.Request) { if name == "" { name = src } + var sources []string + if src != "" { + sources = []string{src} + } else if r.Header.Get("Content-Type") == "application/json" { + var data struct { + Sources []string `json:"sources"` + } + if err := json.NewDecoder(r.Body).Decode(&data); err != nil { + log.Error().Err(err).Caller().Send() + http.Error(w, err.Error(), http.StatusBadRequest) + return + } + sources = data.Sources + } else { + // without source(s) - return all streams list + returnAllStreams(w) + return + } - if New(name, src) == nil { + if New(name, sources) == nil { http.Error(w, "", http.StatusBadRequest) return } - if err := app.PatchConfig(name, src, "streams"); err != nil { + if err := app.PatchConfig(name, sources, "streams"); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } diff --git a/internal/streams/stream.go b/internal/streams/stream.go index bb832694..e194e0ac 100644 --- a/internal/streams/stream.go +++ b/internal/streams/stream.go @@ -21,6 +21,12 @@ func NewStream(source any) *Stream { return &Stream{ producers: []*Producer{NewProducer(source)}, } + case []string: + s := new(Stream) + for _, str := range source { + s.producers = append(s.producers, NewProducer(str)) + } + return s case []any: s := new(Stream) for _, src := range source { diff --git a/internal/streams/streams.go b/internal/streams/streams.go index 1bab036e..91c20f40 100644 --- a/internal/streams/streams.go +++ b/internal/streams/streams.go @@ -56,12 +56,14 @@ func Validate(source string) error { return nil } -func New(name string, source string) *Stream { - if Validate(source) != nil { - return nil +func New(name string, sources []string) *Stream { + for _, source := range sources { + if Validate(source) != nil { + return nil + } } - stream := NewStream(source) + stream := NewStream(sources) streamsMu.Lock() streams[name] = stream @@ -105,7 +107,7 @@ func Patch(name string, source string) *Stream { } // create new stream with this name - return New(name, source) + return New(name, []string{source}) } func GetOrPatch(query url.Values) *Stream { From a8d394efd78b6eb7458e428c6067de436c6586bf Mon Sep 17 00:00:00 2001 From: Alex X Date: Thu, 24 Oct 2024 20:44:37 +0300 Subject: [PATCH 2/2] Update PUT /api/streams for support multiple src params --- internal/homekit/api.go | 2 +- internal/streams/api.go | 32 ++++---------------------------- internal/streams/streams.go | 4 ++-- 3 files changed, 7 insertions(+), 31 deletions(-) diff --git a/internal/homekit/api.go b/internal/homekit/api.go index bd01259d..abd8e97c 100644 --- a/internal/homekit/api.go +++ b/internal/homekit/api.go @@ -101,7 +101,7 @@ func apiPair(id, url string) error { return err } - streams.New(id, []string{conn.URL()}) + streams.New(id, conn.URL()) return app.PatchConfig(id, conn.URL(), "streams") } diff --git a/internal/streams/api.go b/internal/streams/api.go index 7c635089..d6042974 100644 --- a/internal/streams/api.go +++ b/internal/streams/api.go @@ -1,7 +1,6 @@ package streams import ( - "encoding/json" "net/http" "github.com/AlexxIT/go2rtc/internal/api" @@ -9,18 +8,13 @@ import ( "github.com/AlexxIT/go2rtc/pkg/probe" ) -func returnAllStreams(w http.ResponseWriter) { - api.ResponseJSON(w, streams) -} - func apiStreams(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() src := query.Get("src") // without source - return all streams list - // PUT checks first body for sources - if src == "" && r.Method != "POST" && r.Method != "PUT" { - returnAllStreams(w) + if src == "" && r.Method != "POST" { + api.ResponseJSON(w, streams) return } @@ -53,31 +47,13 @@ func apiStreams(w http.ResponseWriter, r *http.Request) { if name == "" { name = src } - var sources []string - if src != "" { - sources = []string{src} - } else if r.Header.Get("Content-Type") == "application/json" { - var data struct { - Sources []string `json:"sources"` - } - if err := json.NewDecoder(r.Body).Decode(&data); err != nil { - log.Error().Err(err).Caller().Send() - http.Error(w, err.Error(), http.StatusBadRequest) - return - } - sources = data.Sources - } else { - // without source(s) - return all streams list - returnAllStreams(w) - return - } - if New(name, sources) == nil { + if New(name, query["src"]...) == nil { http.Error(w, "", http.StatusBadRequest) return } - if err := app.PatchConfig(name, sources, "streams"); err != nil { + if err := app.PatchConfig(name, query["src"], "streams"); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } diff --git a/internal/streams/streams.go b/internal/streams/streams.go index 91c20f40..ae50e6c9 100644 --- a/internal/streams/streams.go +++ b/internal/streams/streams.go @@ -56,7 +56,7 @@ func Validate(source string) error { return nil } -func New(name string, sources []string) *Stream { +func New(name string, sources ...string) *Stream { for _, source := range sources { if Validate(source) != nil { return nil @@ -107,7 +107,7 @@ func Patch(name string, source string) *Stream { } // create new stream with this name - return New(name, []string{source}) + return New(name, source) } func GetOrPatch(query url.Values) *Stream {