更新日志方法

This commit is contained in:
dexter
2022-02-11 00:30:02 +08:00
parent aaf6460659
commit d55db15652
3 changed files with 46 additions and 79 deletions

2
go.mod
View File

@@ -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
View File

@@ -26,47 +26,33 @@ 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) {
var hdlConfig = new(HDLConfig) util.ReturnJson(FilterStreams[*HDLPuller], time.Second, rw, r)
var plugin = InstallPlugin(hdlConfig)
func init() {
plugin.HandleApi("/list", util.GetJsonHandler(FilterStreams[*HDLPuller], time.Second))
plugin.HandleFunc("/", hdlConfig.ServeHTTP)
} }
var Config = new(HDLConfig)
var plugin = InstallPlugin(Config)
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)
if len(parts) == 0 { if len(parts) == 0 {
@@ -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 {

81
pull.go
View File

@@ -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)
} else { func (puller *HDLPuller) Pull(count int) {
puller.Type = "FLV File" if count == 0 {
puller.Publish(streamPath, puller, hdlConfig.Publish) puller.at = puller.NewAudioTrack()
} puller.vt = puller.NewVideoTrack()
return nil }
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 {
return puller.Publish(streamPath, &FLVFile{Puller: puller}, Config.Publish)
}
} }