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