Extend streams API to allow multiple sources

This commit is contained in:
Robert Resch
2024-10-22 16:31:31 +02:00
parent ef7d898747
commit 95a5283c86
4 changed files with 42 additions and 10 deletions

View File

@@ -101,7 +101,7 @@ func apiPair(id, url string) error {
return err return err
} }
streams.New(id, conn.URL()) streams.New(id, []string{conn.URL()})
return app.PatchConfig(id, conn.URL(), "streams") return app.PatchConfig(id, conn.URL(), "streams")
} }

View File

@@ -1,6 +1,7 @@
package streams package streams
import ( import (
"encoding/json"
"net/http" "net/http"
"github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api"
@@ -8,13 +9,18 @@ import (
"github.com/AlexxIT/go2rtc/pkg/probe" "github.com/AlexxIT/go2rtc/pkg/probe"
) )
func returnAllStreams(w http.ResponseWriter) {
api.ResponseJSON(w, streams)
}
func apiStreams(w http.ResponseWriter, r *http.Request) { func apiStreams(w http.ResponseWriter, r *http.Request) {
query := r.URL.Query() query := r.URL.Query()
src := query.Get("src") src := query.Get("src")
// without source - return all streams list // without source - return all streams list
if src == "" && r.Method != "POST" { // PUT checks first body for sources
api.ResponseJSON(w, streams) if src == "" && r.Method != "POST" && r.Method != "PUT" {
returnAllStreams(w)
return return
} }
@@ -47,13 +53,31 @@ func apiStreams(w http.ResponseWriter, r *http.Request) {
if name == "" { if name == "" {
name = src 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) http.Error(w, "", http.StatusBadRequest)
return 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) http.Error(w, err.Error(), http.StatusBadRequest)
} }

View File

@@ -21,6 +21,12 @@ func NewStream(source any) *Stream {
return &Stream{ return &Stream{
producers: []*Producer{NewProducer(source)}, producers: []*Producer{NewProducer(source)},
} }
case []string:
s := new(Stream)
for _, str := range source {
s.producers = append(s.producers, NewProducer(str))
}
return s
case []any: case []any:
s := new(Stream) s := new(Stream)
for _, src := range source { for _, src := range source {

View File

@@ -56,12 +56,14 @@ func Validate(source string) error {
return nil return nil
} }
func New(name string, source string) *Stream { func New(name string, sources []string) *Stream {
if Validate(source) != nil { for _, source := range sources {
return nil if Validate(source) != nil {
return nil
}
} }
stream := NewStream(source) stream := NewStream(sources)
streamsMu.Lock() streamsMu.Lock()
streams[name] = stream streams[name] = stream
@@ -105,7 +107,7 @@ func Patch(name string, source string) *Stream {
} }
// create new stream with this name // create new stream with this name
return New(name, source) return New(name, []string{source})
} }
func GetOrPatch(query url.Values) *Stream { func GetOrPatch(query url.Values) *Stream {