Skip to content

Commit

Permalink
Create multiple stream sources via API (#8)
Browse files Browse the repository at this point in the history
* Ignore .goreload

* API support multiple stream sources
  • Loading branch information
dnjooiopa committed Jun 4, 2024
1 parent 491db1e commit 00e531e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ go2rtc_win*
0_test.go

.DS_Store

.goreload
33 changes: 24 additions & 9 deletions internal/streams/streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"net/url"
"regexp"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -58,8 +59,9 @@ func Validate(source string) error {
return nil
}

func New(name string, source string) *Stream {
if Validate(source) != nil {
func New(name string, source any) *Stream {
// not allow creating dynamic streams with spaces in the source
if src, ok := source.(string); ok && Validate(src) != nil {
return nil
}

Expand Down Expand Up @@ -192,13 +194,26 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
name = src
}

if New(name, src) == nil {
http.Error(w, "", http.StatusBadRequest)
return
}

if err := app.PatchConfig(name, src, "streams"); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
if srcs := strings.Split(src, ","); len(srcs) == 1 {
if New(name, src) == nil {
http.Error(w, "", http.StatusBadRequest)
return
}
if err := app.PatchConfig(name, src, "streams"); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
} else {
srcAny := make([]any, len(srcs))
for i, s := range srcs {
srcAny[i] = s
}
if New(name, srcAny) == nil {
http.Error(w, "", http.StatusBadRequest)
return
}
if err := app.PatchConfig(name, srcAny, "streams"); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
}
}

case "PATCH":
Expand Down

0 comments on commit 00e531e

Please sign in to comment.