Rewritten streams creation
This commit is contained in:
@@ -3,10 +3,11 @@ package streams
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
type state byte
|
||||
@@ -35,6 +36,24 @@ type Producer struct {
|
||||
workerID int
|
||||
}
|
||||
|
||||
const SourceTemplate = "{input}"
|
||||
|
||||
func NewProducer(source string) *Producer {
|
||||
if strings.Contains(source, SourceTemplate) {
|
||||
return &Producer{template: source}
|
||||
}
|
||||
|
||||
return &Producer{url: source}
|
||||
}
|
||||
|
||||
func (p *Producer) SetSource(s string) {
|
||||
if p.template == "" {
|
||||
p.url = s
|
||||
} else {
|
||||
p.url = strings.Replace(p.template, SourceTemplate, s, 1)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Producer) Dial() error {
|
||||
p.mu.Lock()
|
||||
defer p.mu.Unlock()
|
||||
@@ -112,13 +131,6 @@ func (p *Producer) AddTrack(media *core.Media, codec *core.Codec, track *core.Re
|
||||
return nil
|
||||
}
|
||||
|
||||
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) MarshalJSON() ([]byte, error) {
|
||||
if p.conn != nil {
|
||||
return json.Marshal(p.conn)
|
||||
|
||||
@@ -3,10 +3,11 @@ package streams
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
)
|
||||
|
||||
type Stream struct {
|
||||
@@ -19,15 +20,13 @@ type Stream struct {
|
||||
func NewStream(source any) *Stream {
|
||||
switch source := source.(type) {
|
||||
case string:
|
||||
s := new(Stream)
|
||||
prod := &Producer{url: source}
|
||||
s.producers = append(s.producers, prod)
|
||||
return s
|
||||
return &Stream{
|
||||
producers: []*Producer{NewProducer(source)},
|
||||
}
|
||||
case []any:
|
||||
s := new(Stream)
|
||||
for _, source := range source {
|
||||
prod := &Producer{url: source.(string)}
|
||||
s.producers = append(s.producers, prod)
|
||||
s.producers = append(s.producers, NewProducer(source.(string)))
|
||||
}
|
||||
return s
|
||||
case map[string]any:
|
||||
|
||||
@@ -1,19 +1,38 @@
|
||||
package streams
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/require"
|
||||
"net/url"
|
||||
"testing"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/pkg/core"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestTemplate(t *testing.T) {
|
||||
source1 := "does not matter"
|
||||
|
||||
stream1 := New("from_yaml", source1)
|
||||
func TestRecursion(t *testing.T) {
|
||||
// create stream with some source
|
||||
stream1 := New("from_yaml", "does not matter")
|
||||
require.Len(t, streams, 1)
|
||||
|
||||
stream2 := NewTemplate("camera.from_hass", "rtsp://localhost:8554/from_yaml?video")
|
||||
// ask another unnamed stream that links go2rtc
|
||||
query, err := url.ParseQuery("src=rtsp://localhost:8554/from_yaml?video")
|
||||
require.Nil(t, err)
|
||||
stream2 := GetOrPatch(query)
|
||||
|
||||
// check stream is same
|
||||
require.Equal(t, stream1, stream2)
|
||||
require.Equal(t, stream2.producers[0].url, source1)
|
||||
// check stream urls is same
|
||||
require.Equal(t, stream1.producers[0].url, stream2.producers[0].url)
|
||||
require.Len(t, streams, 2)
|
||||
}
|
||||
|
||||
func TestTempate(t *testing.T) {
|
||||
HandleFunc("rtsp", func(url string) (core.Producer, error) { return nil, nil }) // bypass HasProducer
|
||||
|
||||
// config from yaml
|
||||
stream1 := New("camera.from_hass", "ffmpeg:{input}#video=copy")
|
||||
// request from hass
|
||||
stream2 := Patch("camera.from_hass", "rtsp://example.com")
|
||||
|
||||
require.Equal(t, stream1, stream2)
|
||||
require.Equal(t, "ffmpeg:rtsp://example.com#video=copy", stream1.producers[0].url)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
package streams
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/url"
|
||||
"sync"
|
||||
|
||||
"github.com/AlexxIT/go2rtc/internal/api"
|
||||
"github.com/AlexxIT/go2rtc/internal/app"
|
||||
"github.com/AlexxIT/go2rtc/internal/app/store"
|
||||
"github.com/rs/zerolog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func Init() {
|
||||
@@ -39,18 +41,56 @@ func New(name string, source any) *Stream {
|
||||
return stream
|
||||
}
|
||||
|
||||
func NewTemplate(name string, source any) *Stream {
|
||||
func Patch(name string, source string) *Stream {
|
||||
streamsMu.Lock()
|
||||
defer streamsMu.Unlock()
|
||||
|
||||
// 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" && len(u.Path) > 1 {
|
||||
if stream, ok := streams[u.Path[1:]]; ok {
|
||||
streams[name] = stream
|
||||
return stream
|
||||
}
|
||||
if u, err := url.Parse(source); err == nil && u.Scheme == "rtsp" && len(u.Path) > 1 {
|
||||
rtspName := u.Path[1:]
|
||||
if stream, ok := streams[rtspName]; ok {
|
||||
// link (alias) stream[name] to stream[rtspName]
|
||||
streams[name] = stream
|
||||
return stream
|
||||
}
|
||||
}
|
||||
|
||||
return New(name, "{input}")
|
||||
// check if src has supported scheme
|
||||
if !HasProducer(source) {
|
||||
return nil
|
||||
}
|
||||
|
||||
// check an existing stream with this name
|
||||
if stream, ok := streams[name]; ok {
|
||||
stream.SetSource(source)
|
||||
return stream
|
||||
}
|
||||
|
||||
// create new stream with this name
|
||||
return New(name, source)
|
||||
}
|
||||
|
||||
func GetOrPatch(query url.Values) *Stream {
|
||||
// check if src param exists
|
||||
source := query.Get("src")
|
||||
if source == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
// check if src is stream name
|
||||
if stream, ok := streams[source]; ok {
|
||||
return stream
|
||||
}
|
||||
|
||||
// check if name param provided
|
||||
if name := query.Get("name"); name == "" {
|
||||
log.Info().Msgf("[streams] create new stream url=%s", source)
|
||||
|
||||
return Patch(name, source)
|
||||
}
|
||||
|
||||
// return new stream with src as name
|
||||
return Patch(source, source)
|
||||
}
|
||||
|
||||
func GetAll() (names []string) {
|
||||
@@ -91,11 +131,7 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// support {input} templates: https://github.com/AlexxIT/go2rtc#module-hass
|
||||
stream := Get(name)
|
||||
if stream == nil {
|
||||
stream = NewTemplate(name, src)
|
||||
}
|
||||
stream.SetSource(src)
|
||||
Patch(name, src)
|
||||
|
||||
case "POST":
|
||||
// with dst - redirect source to dst
|
||||
@@ -120,3 +156,4 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var log zerolog.Logger
|
||||
var streams = map[string]*Stream{}
|
||||
var streamsMu sync.Mutex
|
||||
Reference in New Issue
Block a user