mirror of
https://github.com/Monibuca/plugin-hdl.git
synced 2025-10-05 08:47:00 +08:00
适配引擎变更
This commit is contained in:
59
main.go
59
main.go
@@ -2,7 +2,6 @@ package hdl
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"net"
|
||||
"net/http"
|
||||
@@ -11,31 +10,23 @@ import (
|
||||
|
||||
. "github.com/Monibuca/engine/v4"
|
||||
"github.com/Monibuca/engine/v4/codec"
|
||||
"github.com/Monibuca/engine/v4/config"
|
||||
"github.com/Monibuca/engine/v4/util"
|
||||
. "github.com/logrusorgru/aurora"
|
||||
amf "github.com/zhangpeihao/goamf"
|
||||
)
|
||||
|
||||
type HDLConfig struct {
|
||||
HTTPConfig
|
||||
PublishConfig
|
||||
SubscribeConfig
|
||||
PullConfig
|
||||
context.Context
|
||||
context.CancelFunc
|
||||
config.HTTP
|
||||
config.Publish
|
||||
config.Subscribe
|
||||
config.Pull
|
||||
}
|
||||
|
||||
var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`)
|
||||
var config = &HDLConfig{
|
||||
PublishConfig: DefaultPublishConfig,
|
||||
SubscribeConfig: DefaultSubscribeConfig,
|
||||
}
|
||||
|
||||
func (config *HDLConfig) Update(override Config) {
|
||||
func (config *HDLConfig) Update(override config.Config) {
|
||||
override.Unmarshal(config)
|
||||
needListen := false
|
||||
if config.CancelFunc == nil {
|
||||
needListen = config.ListenAddr != "" || config.ListenAddrTLS != ""
|
||||
if config.PullOnStart {
|
||||
for streamPath, url := range config.AutoPullList {
|
||||
if err := PullStream(streamPath, url); err != nil {
|
||||
@@ -43,23 +34,12 @@ func (config *HDLConfig) Update(override Config) {
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if override.Has("ListenAddr") || override.Has("ListenAddrTLS") {
|
||||
config.CancelFunc()
|
||||
needListen = config.ListenAddr != "" || config.ListenAddrTLS != ""
|
||||
}
|
||||
}
|
||||
config.Context, config.CancelFunc = context.WithCancel(Ctx)
|
||||
if needListen {
|
||||
if config.ListenAddr != "" || config.ListenAddrTLS != "" {
|
||||
util.Print(Green("HDL Listen at "), BrightBlue(config.ListenAddr), BrightBlue(config.ListenAddrTLS))
|
||||
config.Listen(config)
|
||||
config.Listen(plugin, config)
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
if plugin := InstallPlugin(config); plugin != nil {
|
||||
plugin.HandleApi("/list", util.GetJsonHandler(getHDList, time.Second))
|
||||
plugin.HandleFunc("/pull", func(rw http.ResponseWriter, r *http.Request) {
|
||||
func (config *HDLConfig) API_pull(rw http.ResponseWriter, r *http.Request) {
|
||||
targetURL := r.URL.Query().Get("target")
|
||||
streamPath := r.URL.Query().Get("streamPath")
|
||||
save := r.URL.Query().Get("save")
|
||||
@@ -77,17 +57,14 @@ func init() {
|
||||
} else {
|
||||
rw.WriteHeader(500)
|
||||
}
|
||||
})
|
||||
plugin.HandleFunc("/", config.ServeHTTP)
|
||||
}
|
||||
}
|
||||
func getHDList() (info []*Stream) {
|
||||
for _, s := range Streams.ToList() {
|
||||
if _, ok := s.Publisher.(*HDLPuller); ok {
|
||||
info = append(info, s)
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
var hdlConfig = new(HDLConfig)
|
||||
var plugin = InstallPlugin(hdlConfig)
|
||||
|
||||
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) {
|
||||
@@ -103,14 +80,14 @@ func (config *HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Transfer-Encoding", "chunked")
|
||||
w.Header().Set("Content-Type", "video/x-flv")
|
||||
sub := Subscriber{ID: r.RemoteAddr, Type: "FLV"}
|
||||
if sub.Subscribe(stringPath, config.SubscribeConfig) {
|
||||
if sub.Subscribe(stringPath, hdlConfig.Subscribe) {
|
||||
vt, at := sub.WaitVideoTrack(), sub.WaitAudioTrack()
|
||||
var buffer bytes.Buffer
|
||||
if _, err := amf.WriteString(&buffer, "onMetaData"); err != nil {
|
||||
return
|
||||
}
|
||||
metaData := amf.Object{
|
||||
"MetaDataCreator": "m7s" + Version,
|
||||
"MetaDataCreator": "m7s" + Engine.Version,
|
||||
"hasVideo": vt != nil,
|
||||
"hasAudio": at != nil,
|
||||
"hasMatadata": true,
|
||||
|
16
pull.go
16
pull.go
@@ -62,15 +62,10 @@ func (puller *HDLPuller) pull() {
|
||||
}
|
||||
|
||||
type HDLPuller struct {
|
||||
Publisher
|
||||
Puller
|
||||
absTS uint32 //绝对时间戳
|
||||
at *track.UnknowAudio
|
||||
vt *track.UnknowVideo
|
||||
io.ReadCloser
|
||||
}
|
||||
|
||||
func (puller *HDLPuller) Close() {
|
||||
puller.ReadCloser.Close()
|
||||
}
|
||||
|
||||
func (puller *HDLPuller) OnStateChange(old StreamState, n StreamState) bool {
|
||||
@@ -94,7 +89,7 @@ func (puller *HDLPuller) OnStateChange(old StreamState, n StreamState) bool {
|
||||
}
|
||||
go puller.pull()
|
||||
case STATE_WAITPUBLISH:
|
||||
if config.AutoReconnect {
|
||||
if hdlConfig.AutoReconnect {
|
||||
if puller.Type == "HDL Pull" {
|
||||
if res, err := http.Get(puller.String()); err == nil {
|
||||
puller.ReadCloser = res.Body
|
||||
@@ -117,17 +112,16 @@ func (puller *HDLPuller) OnStateChange(old StreamState, n StreamState) bool {
|
||||
|
||||
func PullStream(streamPath, address string) (err error) {
|
||||
puller := &HDLPuller{}
|
||||
puller.PullURL, err = url.Parse(address)
|
||||
puller.RemoteURL, err = url.Parse(address)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
puller.Config = config.PublishConfig
|
||||
if strings.HasPrefix(puller.Scheme, "http") {
|
||||
puller.Type = "HDL Pull"
|
||||
puller.Publish(streamPath, puller)
|
||||
puller.Publish(streamPath, puller, hdlConfig.Publish)
|
||||
} else {
|
||||
puller.Type = "FLV File"
|
||||
puller.Publish(streamPath, puller)
|
||||
puller.Publish(streamPath, puller, hdlConfig.Publish)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user