mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-07 09:11:28 +08:00
Add active publish logic to streams
This commit is contained in:
@@ -73,3 +73,25 @@ func Location(url string) (string, error) {
|
|||||||
|
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: rework
|
||||||
|
|
||||||
|
type ConsumerHandler func(url string) (core.Consumer, func(), error)
|
||||||
|
|
||||||
|
var consumerHandlers = map[string]ConsumerHandler{}
|
||||||
|
|
||||||
|
func HandleConsumerFunc(scheme string, handler ConsumerHandler) {
|
||||||
|
consumerHandlers[scheme] = handler
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetConsumer(url string) (core.Consumer, func(), error) {
|
||||||
|
if i := strings.IndexByte(url, ':'); i > 0 {
|
||||||
|
scheme := url[:i]
|
||||||
|
|
||||||
|
if handler, ok := consumerHandlers[scheme]; ok {
|
||||||
|
return handler(url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil, errors.New("streams: unsupported scheme: " + url)
|
||||||
|
}
|
||||||
|
19
internal/streams/publish.go
Normal file
19
internal/streams/publish.go
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package streams
|
||||||
|
|
||||||
|
func (s *Stream) Publish(url string) error {
|
||||||
|
cons, run, err := GetConsumer(url)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = s.AddConsumer(cons); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
go func() {
|
||||||
|
run()
|
||||||
|
s.RemoveConsumer(cons)
|
||||||
|
}()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
@@ -172,6 +172,10 @@ func streamsHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
} else {
|
} else {
|
||||||
api.ResponseJSON(w, stream)
|
api.ResponseJSON(w, stream)
|
||||||
}
|
}
|
||||||
|
} else if stream = Get(src); stream != nil {
|
||||||
|
if err := stream.Publish(dst); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
http.Error(w, "", http.StatusNotFound)
|
http.Error(w, "", http.StatusNotFound)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user