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 May 16, 2024
1 parent a1f7468 commit 2b77c6c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ go2rtc.json
0_test.go

.DS_Store

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

Expand Down Expand Up @@ -49,9 +50,9 @@ func Get(name string) *Stream {

var sanitize = regexp.MustCompile(`\s`)

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

Expand Down Expand Up @@ -184,13 +185,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 2b77c6c

Please sign in to comment.