Fix new stream from camera entity from Hass

This commit is contained in:
Alexey Khit
2023-04-15 07:34:38 +03:00
parent 7d3fbf2ee0
commit d633d331bb
4 changed files with 41 additions and 65 deletions
+20 -4
View File
@@ -7,6 +7,7 @@ import (
"github.com/AlexxIT/go2rtc/cmd/app/store"
"github.com/rs/zerolog"
"net/http"
"net/url"
)
func Init() {
@@ -39,6 +40,20 @@ func New(name string, source any) *Stream {
return stream
}
func NewTemplate(name string, source any) *Stream {
// check if source links to some stream name from go2rtc
if rawURL, ok := source.(string); ok {
if u, err := url.Parse(rawURL); err == nil && u.Scheme == "rtsp" {
if stream, ok := streams[u.Path[1:]]; ok {
streams[name] = stream
return stream
}
}
}
return New(name, "{input}")
}
func GetOrNew(src string) *Stream {
if stream, ok := streams[src]; ok {
return stream
@@ -85,11 +100,12 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
return
}
if stream := Get(name); stream != nil {
stream.SetSource(src)
} else {
New(name, src)
// support {input} templates: https://github.com/AlexxIT/go2rtc#module-hass
stream := Get(name)
if stream == nil {
stream = NewTemplate(name, src)
}
stream.SetSource(src)
case "POST":
// with dst - redirect source to dst
-2
View File
@@ -31,8 +31,6 @@ func NewStream(source any) *Stream {
s.producers = append(s.producers, prod)
}
return s
case *Stream:
return source
case map[string]any:
return NewStream(source["url"])
case nil:
+19
View File
@@ -0,0 +1,19 @@
package streams
import (
"github.com/stretchr/testify/require"
"testing"
)
func TestTemplate(t *testing.T) {
source1 := "does not matter"
stream1 := New("from_yaml", source1)
require.Len(t, streams, 1)
stream2 := NewTemplate("camera.from_hass", "rtsp://localhost:8554/from_yaml?video")
require.Equal(t, stream1, stream2)
require.Equal(t, stream2.producers[0].url, source1)
require.Len(t, streams, 2)
}