mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-04 07:56:33 +08:00
Support adding streams on the fly
This commit is contained in:
@@ -83,7 +83,7 @@ func fileServerHandlder(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func statsHandler(w http.ResponseWriter, _ *http.Request) {
|
||||
v := map[string]interface{}{
|
||||
"streams": streams.Streams,
|
||||
"streams": streams.All(),
|
||||
}
|
||||
data, err := json.Marshal(v)
|
||||
if err != nil {
|
||||
|
@@ -79,7 +79,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
stream := streams.NewStream(url)
|
||||
stream := streams.Get(url)
|
||||
str, err = webrtc.ExchangeSDP(stream, string(offer), r.UserAgent())
|
||||
if err != nil {
|
||||
log.Error().Err(err).Msg("[api.hass] exchange SDP")
|
||||
|
@@ -17,6 +17,11 @@ func HandleFunc(scheme string, handler Handler) {
|
||||
handlers[scheme] = handler
|
||||
}
|
||||
|
||||
func HasProducer(url string) bool {
|
||||
i := strings.IndexByte(url, ':')
|
||||
return handlers[url[:i]] != nil
|
||||
}
|
||||
|
||||
func GetProducer(url string) (streamer.Producer, error) {
|
||||
i := strings.IndexByte(url, ':')
|
||||
handler := handlers[url[:i]]
|
||||
|
@@ -120,6 +120,20 @@ func (s *Stream) RemoveProducer(prod streamer.Producer) {
|
||||
panic("not implemented")
|
||||
}
|
||||
|
||||
func (s *Stream) Active() bool {
|
||||
if len(s.consumers) > 0{
|
||||
return true
|
||||
}
|
||||
|
||||
for _, prod := range s.producers {
|
||||
if prod.element != nil {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *Stream) MarshalJSON() ([]byte, error) {
|
||||
var v []interface{}
|
||||
for _, prod := range s.producers {
|
||||
|
@@ -5,8 +5,6 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
)
|
||||
|
||||
var Streams = map[string]*Stream{}
|
||||
|
||||
func Init() {
|
||||
var cfg struct {
|
||||
Mod map[string]interface{} `yaml:"streams"`
|
||||
@@ -17,12 +15,34 @@ func Init() {
|
||||
log = app.GetLogger("streams")
|
||||
|
||||
for name, item := range cfg.Mod {
|
||||
Streams[name] = NewStream(item)
|
||||
streams[name] = NewStream(item)
|
||||
}
|
||||
}
|
||||
|
||||
func Get(name string) *Stream {
|
||||
return Streams[name]
|
||||
if stream, ok := streams[name]; ok {
|
||||
return stream
|
||||
}
|
||||
|
||||
if HasProducer(name) {
|
||||
log.Info().Str("url", name).Msg("[streams] create new stream")
|
||||
stream := NewStream(name)
|
||||
streams[name] = stream
|
||||
return stream
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func All() map[string]interface{} {
|
||||
active := map[string]interface{}{}
|
||||
for name, stream := range streams {
|
||||
if stream.Active() {
|
||||
active[name] = stream
|
||||
}
|
||||
}
|
||||
return active
|
||||
}
|
||||
|
||||
var log zerolog.Logger
|
||||
var streams = map[string]*Stream{}
|
||||
|
Reference in New Issue
Block a user