mirror of
https://github.com/Monibuca/plugin-hdl.git
synced 2025-10-06 01:07:04 +08:00
更新日志方法
This commit is contained in:
2
go.mod
2
go.mod
@@ -4,4 +4,4 @@ go 1.18
|
|||||||
|
|
||||||
require github.com/logrusorgru/aurora v2.0.3+incompatible
|
require github.com/logrusorgru/aurora v2.0.3+incompatible
|
||||||
|
|
||||||
require github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 // indirect
|
require github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8
|
||||||
|
38
main.go
38
main.go
@@ -26,46 +26,32 @@ type HDLConfig struct {
|
|||||||
var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
|
var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
|
||||||
|
|
||||||
func (config *HDLConfig) Update(override config.Config) {
|
func (config *HDLConfig) Update(override config.Config) {
|
||||||
override.Unmarshal(config)
|
|
||||||
if config.PullOnStart {
|
|
||||||
for streamPath, url := range config.AutoPullList {
|
|
||||||
if err := PullStream(streamPath, url); err != nil {
|
|
||||||
util.Println(err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if config.ListenAddr != "" || config.ListenAddrTLS != "" {
|
if config.ListenAddr != "" || config.ListenAddrTLS != "" {
|
||||||
util.Print(Green("HDL Listen at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS))
|
plugin.Infoln(Green("HDL Listen at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS))
|
||||||
config.Listen(plugin, config)
|
config.Listen(plugin, config)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func (config *HDLConfig) API_pull(rw http.ResponseWriter, r *http.Request) {
|
func (config *HDLConfig) API_Pull(rw http.ResponseWriter, r *http.Request) {
|
||||||
targetURL := r.URL.Query().Get("target")
|
targetURL := r.URL.Query().Get("target")
|
||||||
streamPath := r.URL.Query().Get("streamPath")
|
streamPath := r.URL.Query().Get("streamPath")
|
||||||
save := r.URL.Query().Get("save")
|
if config.PullStream(streamPath, Puller{RemoteURL: targetURL, Config: &config.Pull}) {
|
||||||
if err := PullStream(streamPath, targetURL); err == nil {
|
if r.URL.Query().Get("save") != "" {
|
||||||
if save == "1" {
|
|
||||||
if config.AutoPullList == nil {
|
if config.AutoPullList == nil {
|
||||||
config.AutoPullList = make(map[string]string)
|
config.AutoPullList = make(map[string]string)
|
||||||
}
|
}
|
||||||
config.AutoPullList[streamPath] = targetURL
|
config.AutoPullList[streamPath] = targetURL
|
||||||
if err = plugin.Save(); err != nil {
|
if err := plugin.Save(); err != nil {
|
||||||
util.Println(err)
|
plugin.Errorln(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
rw.WriteHeader(200)
|
|
||||||
} else {
|
|
||||||
rw.WriteHeader(500)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func (config *HDLConfig) API_List(rw http.ResponseWriter, r *http.Request) {
|
||||||
|
util.ReturnJson(FilterStreams[*HDLPuller], time.Second, rw, r)
|
||||||
|
}
|
||||||
|
|
||||||
var hdlConfig = new(HDLConfig)
|
var Config = new(HDLConfig)
|
||||||
var plugin = InstallPlugin(hdlConfig)
|
var plugin = InstallPlugin(Config)
|
||||||
|
|
||||||
func init() {
|
|
||||||
plugin.HandleApi("/list", util.GetJsonHandler(FilterStreams[*HDLPuller], time.Second))
|
|
||||||
plugin.HandleFunc("/", hdlConfig.ServeHTTP)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (config *HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (config *HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
parts := streamPathReg.FindStringSubmatch(r.RequestURI)
|
parts := streamPathReg.FindStringSubmatch(r.RequestURI)
|
||||||
@@ -80,7 +66,7 @@ func (config *HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
w.Header().Set("Transfer-Encoding", "chunked")
|
w.Header().Set("Transfer-Encoding", "chunked")
|
||||||
w.Header().Set("Content-Type", "video/x-flv")
|
w.Header().Set("Content-Type", "video/x-flv")
|
||||||
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
||||||
if sub.Subscribe(stringPath, hdlConfig.Subscribe) {
|
if sub.Subscribe(stringPath, Config.Subscribe) {
|
||||||
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
||||||
var buffer bytes.Buffer
|
var buffer bytes.Buffer
|
||||||
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
||||||
|
77
pull.go
77
pull.go
@@ -4,7 +4,6 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -67,61 +66,43 @@ type HDLPuller struct {
|
|||||||
at *track.UnknowAudio
|
at *track.UnknowAudio
|
||||||
vt *track.UnknowVideo
|
vt *track.UnknowVideo
|
||||||
}
|
}
|
||||||
|
type FLVFile HDLPuller
|
||||||
|
|
||||||
func (puller *HDLPuller) OnStateChange(old StreamState, n StreamState) bool {
|
func (puller *FLVFile) pull() {
|
||||||
switch n {
|
(*HDLPuller)(puller).pull()
|
||||||
case STATE_PUBLISHING:
|
}
|
||||||
|
|
||||||
|
func (puller *FLVFile) Pull(count int) {
|
||||||
|
if count == 0 {
|
||||||
puller.at = puller.NewAudioTrack()
|
puller.at = puller.NewAudioTrack()
|
||||||
puller.vt = puller.NewVideoTrack()
|
puller.vt = puller.NewVideoTrack()
|
||||||
if puller.Type == "HDL Pull" {
|
|
||||||
if res, err := http.Get(puller.String()); err == nil {
|
|
||||||
puller.ReadCloser = res.Body
|
|
||||||
} else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
} else {
|
if file, err := os.Open(puller.RemoteURL); err == nil {
|
||||||
if file, err := os.Open(puller.String()); err == nil {
|
puller.Reader = file
|
||||||
puller.ReadCloser = file
|
puller.Closer = file
|
||||||
} else {
|
} else {
|
||||||
file.Close()
|
file.Close()
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
go puller.pull()
|
|
||||||
case STATE_WAITPUBLISH:
|
|
||||||
if hdlConfig.AutoReconnect {
|
|
||||||
if puller.Type == "HDL Pull" {
|
|
||||||
if res, err := http.Get(puller.String()); err == nil {
|
|
||||||
puller.ReadCloser = res.Body
|
|
||||||
} else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if file, err := os.Open(puller.String()); err == nil {
|
|
||||||
puller.ReadCloser = file
|
|
||||||
} else {
|
|
||||||
file.Close()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
go puller.pull()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func PullStream(streamPath, address string) (err error) {
|
|
||||||
puller := &HDLPuller{}
|
|
||||||
puller.RemoteURL, err = url.Parse(address)
|
|
||||||
if err != nil {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(puller.Scheme, "http") {
|
puller.pull()
|
||||||
puller.Type = "HDL Pull"
|
}
|
||||||
puller.Publish(streamPath, puller, hdlConfig.Publish)
|
|
||||||
|
func (puller *HDLPuller) Pull(count int) {
|
||||||
|
if count == 0 {
|
||||||
|
puller.at = puller.NewAudioTrack()
|
||||||
|
puller.vt = puller.NewVideoTrack()
|
||||||
|
}
|
||||||
|
if res, err := http.Get(puller.RemoteURL); err == nil {
|
||||||
|
puller.Reader = res.Body
|
||||||
|
puller.Closer = res.Body
|
||||||
|
}
|
||||||
|
puller.pull()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (config *HDLConfig) PullStream(streamPath string, puller Puller) bool {
|
||||||
|
if strings.HasPrefix(puller.RemoteURL, "http") {
|
||||||
|
return puller.Publish(streamPath, &HDLPuller{Puller: puller}, Config.Publish)
|
||||||
} else {
|
} else {
|
||||||
puller.Type = "FLV File"
|
return puller.Publish(streamPath, &FLVFile{Puller: puller}, Config.Publish)
|
||||||
puller.Publish(streamPath, puller, hdlConfig.Publish)
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user