diff --git a/go.mod b/go.mod index ff94ae1..122505e 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Monibuca/plugin-hdl/v3 go 1.13 require ( - github.com/Monibuca/engine/v3 v3.4.0 + github.com/Monibuca/engine/v3 v3.4.1 github.com/Monibuca/utils/v3 v3.0.5 github.com/logrusorgru/aurora v2.0.3+incompatible github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 diff --git a/go.sum b/go.sum index 677082b..782e311 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,8 @@ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw= github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= -github.com/Monibuca/engine/v3 v3.4.0 h1:dXh9ZRtnW6hrIGcGoG05MG6mNQsX2k27ftzgNczOyhE= -github.com/Monibuca/engine/v3 v3.4.0/go.mod h1:rgAUey5ziRhlh6WugWyA5fYKyGOvcwhtTMDk4sukE7E= +github.com/Monibuca/engine/v3 v3.4.1 h1:Ap2VbwTkMUkv80NPeUX2sNdV5Vz5nPVoU/6RU51PSAc= +github.com/Monibuca/engine/v3 v3.4.1/go.mod h1:rgAUey5ziRhlh6WugWyA5fYKyGOvcwhtTMDk4sukE7E= github.com/Monibuca/utils/v3 v3.0.5 h1:w14x0HkWTbF4MmHbINLlOwe4VJNoSOeaQChMk5E/4es= github.com/Monibuca/utils/v3 v3.0.5/go.mod h1:RpNS95gapWs6gimwh8Xn2x72FN5tO7Powabj7dTFyvE= github.com/cnotch/apirouter v0.0.0-20200731232942-89e243a791f3/go.mod h1:5deJPLON/x/s2dLOQfuKS0lenhOIT4xX0pvtN/OEIuY= diff --git a/main.go b/main.go index d902429..326d390 100644 --- a/main.go +++ b/main.go @@ -3,8 +3,10 @@ package hdl import ( "bytes" "encoding/binary" + "encoding/json" "net/http" "regexp" + "time" . "github.com/Monibuca/engine/v3" "github.com/Monibuca/utils/v3" @@ -30,13 +32,38 @@ var pconfig = PluginConfig{ func init() { pconfig.Install(run) } +func getHDList() (info []*Stream) { + for _, s := range Streams.ToList() { + if _, ok := s.ExtraProp.(*HDLPuller); ok { + info = append(info, s) + } + } + return +} func run() { + http.HandleFunc("/api/hdl/list", func(rw http.ResponseWriter, r *http.Request) { + utils.CORS(rw, r) + if r.URL.Query().Get("json") != "" { + if jsonData, err := json.Marshal(getHDList()); err == nil { + rw.Write(jsonData) + } else { + rw.WriteHeader(500) + } + return + } + sse := utils.NewSSE(rw, r.Context()) + var err error + for tick := time.NewTicker(time.Second); err == nil; <-tick.C { + err = sse.WriteJSON(getHDList()) + } + }) http.HandleFunc("/api/hdl/pull", func(rw http.ResponseWriter, r *http.Request) { + utils.CORS(rw, r) targetURL := r.URL.Query().Get("target") streamPath := r.URL.Query().Get("streamPath") save := r.URL.Query().Get("save") if err := PullStream(streamPath, targetURL); err == nil { - if save != "" { + if save == "1" { if config.AutoPullList == nil { config.AutoPullList = make(map[string]string) } diff --git a/pull.go b/pull.go index 548ec75..2430531 100644 --- a/pull.go +++ b/pull.go @@ -36,10 +36,15 @@ func pull(at *AudioTrack, vt *VideoTrack, reader io.Reader, lastDisconnect uint3 } } } + +type HDLPuller struct{} + func PullStream(streamPath, url string) error { stream := Stream{ + URL: url, Type: "HDL Pull", StreamPath: streamPath, + ExtraProp: &HDLPuller{}, } at := stream.NewAudioTrack(0) vt := stream.NewVideoTrack(0)