Add get request to preload endpoint for listing them
This commit is contained in:
@@ -238,6 +238,14 @@ paths:
|
|||||||
|
|
||||||
|
|
||||||
/api/preload:
|
/api/preload:
|
||||||
|
get:
|
||||||
|
summary: Get all preloaded streams
|
||||||
|
tags: [ Streams list ]
|
||||||
|
responses:
|
||||||
|
"200":
|
||||||
|
description: ""
|
||||||
|
content:
|
||||||
|
application/json: { example: { camera1: "video&audio", camera2: "video" } }
|
||||||
put:
|
put:
|
||||||
summary: Preload new stream
|
summary: Preload new stream
|
||||||
tags: [ Streams list ]
|
tags: [ Streams list ]
|
||||||
|
|||||||
@@ -133,6 +133,12 @@ func apiPreload(w http.ResponseWriter, r *http.Request) {
|
|||||||
query := r.URL.Query()
|
query := r.URL.Query()
|
||||||
src := query.Get("src")
|
src := query.Get("src")
|
||||||
|
|
||||||
|
// GET - return all preloads
|
||||||
|
if r.Method == "GET" {
|
||||||
|
api.ResponseJSON(w, GetPreloads())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// check if stream exists
|
// check if stream exists
|
||||||
stream := Get(src)
|
stream := Get(src)
|
||||||
if stream == nil {
|
if stream == nil {
|
||||||
|
|||||||
@@ -8,7 +8,12 @@ import (
|
|||||||
"github.com/AlexxIT/go2rtc/pkg/probe"
|
"github.com/AlexxIT/go2rtc/pkg/probe"
|
||||||
)
|
)
|
||||||
|
|
||||||
var preloads = map[*Stream]*probe.Probe{}
|
type preload struct {
|
||||||
|
cons *probe.Probe
|
||||||
|
query string
|
||||||
|
}
|
||||||
|
|
||||||
|
var preloads = map[*Stream]*preload{}
|
||||||
var preloadsMu sync.Mutex
|
var preloadsMu sync.Mutex
|
||||||
|
|
||||||
func Preload(stream *Stream, rawQuery string) {
|
func Preload(stream *Stream, rawQuery string) {
|
||||||
@@ -30,8 +35,8 @@ func AddPreload(stream *Stream, rawQuery string) error {
|
|||||||
preloadsMu.Lock()
|
preloadsMu.Lock()
|
||||||
defer preloadsMu.Unlock()
|
defer preloadsMu.Unlock()
|
||||||
|
|
||||||
if cons := preloads[stream]; cons != nil {
|
if p := preloads[stream]; p != nil {
|
||||||
stream.RemoveConsumer(cons)
|
stream.RemoveConsumer(p.cons)
|
||||||
}
|
}
|
||||||
|
|
||||||
cons := probe.Create("preload", query)
|
cons := probe.Create("preload", query)
|
||||||
@@ -40,7 +45,7 @@ func AddPreload(stream *Stream, rawQuery string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
preloads[stream] = cons
|
preloads[stream] = &preload{cons: cons, query: rawQuery}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,11 +53,33 @@ func DelPreload(stream *Stream) error {
|
|||||||
preloadsMu.Lock()
|
preloadsMu.Lock()
|
||||||
defer preloadsMu.Unlock()
|
defer preloadsMu.Unlock()
|
||||||
|
|
||||||
if cons := preloads[stream]; cons != nil {
|
if p := preloads[stream]; p != nil {
|
||||||
stream.RemoveConsumer(cons)
|
stream.RemoveConsumer(p.cons)
|
||||||
delete(preloads, stream)
|
delete(preloads, stream)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return errors.New("streams: preload not found")
|
return errors.New("streams: preload not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetPreloads() map[string]string {
|
||||||
|
streamsMu.Lock()
|
||||||
|
defer streamsMu.Unlock()
|
||||||
|
|
||||||
|
preloadsMu.Lock()
|
||||||
|
defer preloadsMu.Unlock()
|
||||||
|
|
||||||
|
// build reverse lookup: stream -> name
|
||||||
|
streamNames := make(map[*Stream]string, len(streams))
|
||||||
|
for name, stream := range streams {
|
||||||
|
streamNames[stream] = name
|
||||||
|
}
|
||||||
|
|
||||||
|
result := make(map[string]string, len(preloads))
|
||||||
|
for stream, p := range preloads {
|
||||||
|
if name, ok := streamNames[stream]; ok {
|
||||||
|
result[name] = p.query
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user