mirror of
https://github.com/AlexxIT/go2rtc.git
synced 2025-10-27 02:01:46 +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")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user