mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-28 02:21:36 +08:00
add api endpoints and change config syntax
This commit is contained in:
@@ -122,3 +122,65 @@ func apiStreamsDOT(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
api.Response(w, dot, "text/vnd.graphviz")
|
api.Response(w, dot, "text/vnd.graphviz")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func apiPreload(w http.ResponseWriter, r *http.Request) {
|
||||||
|
query := r.URL.Query()
|
||||||
|
src := query.Get("src")
|
||||||
|
query.Del("src")
|
||||||
|
|
||||||
|
videoQuery := query.Get("video")
|
||||||
|
audioQuery := query.Get("audio")
|
||||||
|
micQuery := query.Get("microphone")
|
||||||
|
|
||||||
|
if src == "" {
|
||||||
|
http.Error(w, "no source", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
switch r.Method {
|
||||||
|
case "PUT":
|
||||||
|
// check if stream exists
|
||||||
|
if stream := Get(src); stream == nil {
|
||||||
|
http.Error(w, "stream not found", http.StatusNotFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if consumer exists
|
||||||
|
if cons, ok := preloads[src]; ok {
|
||||||
|
cons.Stop()
|
||||||
|
delete(preloads, src)
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawQuery string
|
||||||
|
if videoQuery != "" {
|
||||||
|
rawQuery += "video=" + videoQuery + "#"
|
||||||
|
}
|
||||||
|
if audioQuery != "" {
|
||||||
|
rawQuery += "audio=" + audioQuery + "#"
|
||||||
|
}
|
||||||
|
if micQuery != "" {
|
||||||
|
rawQuery += "microphone=" + micQuery
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.PatchConfig([]string{"preload", src}, rawQuery); err != nil {
|
||||||
|
log.Error().Err(err).Str("src", src).Msg("Failed to patch config for PUT")
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Preload(src, rawQuery)
|
||||||
|
|
||||||
|
case "DELETE":
|
||||||
|
if cons, ok := preloads[src]; ok {
|
||||||
|
cons.Stop()
|
||||||
|
delete(preloads, src)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.PatchConfig([]string{"preload", src}, nil); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
http.Error(w, "", http.StatusMethodNotAllowed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -2,13 +2,15 @@ package streams
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/AlexxIT/go2rtc/pkg/preload"
|
"github.com/AlexxIT/go2rtc/pkg/preload"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *Stream) Preload(query url.Values) error {
|
var preloads = map[string]*preload.Preload{}
|
||||||
cons := preload.NewPreload(query)
|
|
||||||
|
func (s *Stream) Preload(name string, query url.Values) error {
|
||||||
|
cons := preload.NewPreload(name, query)
|
||||||
|
preloads[name] = cons
|
||||||
|
|
||||||
if err := s.AddConsumer(cons); err != nil {
|
if err := s.AddConsumer(cons); err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -17,14 +19,16 @@ func (s *Stream) Preload(query url.Values) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Preload(src string) {
|
func Preload(src string, rawQuery string) {
|
||||||
name, rawQuery, _ := strings.Cut(src, "#")
|
// skip if exists
|
||||||
query := ParseQuery(rawQuery)
|
if _, ok := preloads[src]; ok {
|
||||||
|
|
||||||
if stream := Get(name); stream != nil {
|
|
||||||
if err := stream.Preload(query); err != nil {
|
|
||||||
log.Error().Err(err).Caller().Send()
|
|
||||||
}
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if stream := Get(src); stream != nil {
|
||||||
|
query := ParseQuery(rawQuery)
|
||||||
|
if err := stream.Preload(src, query); err != nil {
|
||||||
|
log.Error().Err(err).Caller().Send()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ import (
|
|||||||
|
|
||||||
func Init() {
|
func Init() {
|
||||||
var cfg struct {
|
var cfg struct {
|
||||||
Streams map[string]any `yaml:"streams"`
|
Streams map[string]any `yaml:"streams"`
|
||||||
Publish map[string]any `yaml:"publish"`
|
Publish map[string]any `yaml:"publish"`
|
||||||
Preload []string `yaml:"preload"`
|
Preload map[string]string `yaml:"preload"`
|
||||||
}
|
}
|
||||||
|
|
||||||
app.LoadConfig(&cfg)
|
app.LoadConfig(&cfg)
|
||||||
@@ -29,6 +29,7 @@ func Init() {
|
|||||||
|
|
||||||
api.HandleFunc("api/streams", apiStreams)
|
api.HandleFunc("api/streams", apiStreams)
|
||||||
api.HandleFunc("api/streams.dot", apiStreamsDOT)
|
api.HandleFunc("api/streams.dot", apiStreamsDOT)
|
||||||
|
api.HandleFunc("api/preload", apiPreload)
|
||||||
|
|
||||||
if cfg.Publish == nil && cfg.Preload == nil {
|
if cfg.Publish == nil && cfg.Preload == nil {
|
||||||
return
|
return
|
||||||
@@ -44,8 +45,8 @@ func Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cfg.Preload != nil {
|
if cfg.Preload != nil {
|
||||||
for _, src := range cfg.Preload {
|
for name, rawQuery := range cfg.Preload {
|
||||||
Preload(src)
|
Preload(name, rawQuery)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -9,11 +9,10 @@ import (
|
|||||||
|
|
||||||
type Preload struct {
|
type Preload struct {
|
||||||
core.Connection
|
core.Connection
|
||||||
|
|
||||||
Closed core.Waiter
|
Closed core.Waiter
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPreload(query url.Values) *Preload {
|
func NewPreload(name string, query url.Values) *Preload {
|
||||||
medias := core.ParseQuery(query)
|
medias := core.ParseQuery(query)
|
||||||
|
|
||||||
for _, value := range query["microphone"] {
|
for _, value := range query["microphone"] {
|
||||||
@@ -49,11 +48,10 @@ func NewPreload(query url.Values) *Preload {
|
|||||||
return &Preload{
|
return &Preload{
|
||||||
Connection: core.Connection{
|
Connection: core.Connection{
|
||||||
ID: core.NewID(),
|
ID: core.NewID(),
|
||||||
FormatName: "preload",
|
|
||||||
Medias: medias,
|
Medias: medias,
|
||||||
Protocol: "native",
|
Protocol: "native",
|
||||||
RemoteAddr: "localhost",
|
RemoteAddr: "localhost",
|
||||||
UserAgent: "go2rtc",
|
UserAgent: "go2rtc/preload",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user