Adds URL templates to integration with Hass

This commit is contained in:
Alexey Khit
2022-09-16 17:03:03 +03:00
parent 428628fcce
commit 407ccc45bc
4 changed files with 60 additions and 48 deletions
+11 -1
View File
@@ -2,6 +2,7 @@ package streams
import (
"github.com/AlexxIT/go2rtc/pkg/streamer"
"strings"
"sync"
)
@@ -17,7 +18,9 @@ const (
type Producer struct {
streamer.Element
url string
url string
template string
element streamer.Producer
tracks []*streamer.Track
@@ -25,6 +28,13 @@ type Producer struct {
mx sync.Mutex
}
func (p *Producer) SetSource(s string) {
if p.template == "" {
p.template = p.url
}
p.url = strings.Replace(p.template, "{input}", s, 1)
}
func (p *Producer) GetMedias() []*streamer.Media {
p.mx.Lock()
defer p.mx.Unlock()
+9 -6
View File
@@ -17,30 +17,33 @@ type Stream struct {
}
func NewStream(source interface{}) *Stream {
s := new(Stream)
switch source := source.(type) {
case string:
s := new(Stream)
prod := &Producer{url: source}
s.producers = append(s.producers, prod)
return s
case []interface{}:
s := new(Stream)
for _, source := range source {
prod := &Producer{url: source.(string)}
s.producers = append(s.producers, prod)
}
return s
case *Stream:
return source
case map[string]interface{}:
return NewStream(source["url"])
case nil:
return new(Stream)
default:
panic("wrong source type")
}
return s
}
func (s *Stream) SetSource(source string) {
if len(s.producers) > 0 {
s.producers[0].url = source
for _, prod := range s.producers {
prod.SetSource(source)
}
}
+12 -25
View File
@@ -4,7 +4,6 @@ import (
"github.com/AlexxIT/go2rtc/cmd/app"
"github.com/AlexxIT/go2rtc/cmd/app/store"
"github.com/rs/zerolog"
"strings"
)
func Init() {
@@ -25,7 +24,17 @@ func Init() {
}
}
func Get(src string) *Stream {
func Get(name string) *Stream {
return streams[name]
}
func New(name string, source interface{}) *Stream {
stream := NewStream(source)
streams[name] = stream
return stream
}
func GetOrNew(src string) *Stream {
if stream, ok := streams[src]; ok {
return stream
}
@@ -35,30 +44,8 @@ func Get(src string) *Stream {
}
log.Info().Str("url", src).Msg("[streams] create new stream")
stream := NewStream(src)
streams[src] = stream
return stream
}
func Has(src string) bool {
return streams[src] != nil
}
func New(name string, source interface{}) {
switch source := source.(type) {
case string:
// check if new stream already link on our other stream
if strings.HasPrefix(source, "rtsp://") {
if i := strings.IndexByte(source[7:], '/'); i > 0 {
if stream, ok := streams[source[8+i:]]; ok {
streams[name] = stream
return
}
}
}
}
streams[name] = NewStream(source)
return New(src, src)
}
func Delete(name string) {