From f7e4c060abaa439edbeeb3134d3c798871dcad81 Mon Sep 17 00:00:00 2001 From: dexter <178529795@qq.com> Date: Mon, 21 Feb 2022 21:40:31 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=E5=BC=95=E6=93=8E=E5=8D=87?= =?UTF-8?q?=E7=BA=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 11 ++++++++++- go.sum | 11 +++++++++++ main.go | 29 ++++++++++++++--------------- pull.go | 2 +- 4 files changed, 36 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index 7a41f09..8f7f816 100644 --- a/go.mod +++ b/go.mod @@ -4,4 +4,13 @@ go 1.18 require github.com/logrusorgru/aurora v2.0.3+incompatible -require github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 +require ( + github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 + go.uber.org/zap v1.21.0 +) + +require ( + go.uber.org/atomic v1.7.0 // indirect + go.uber.org/multierr v1.6.0 // indirect + gopkg.in/yaml.v2 v2.4.0 // indirect +) diff --git a/go.sum b/go.sum index c1bdab1..7269cc3 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,15 @@ +github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8= github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4= +github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8 h1:r1JUI0wuHlgRb8jNd3zPBBkjUdrjpVKr8SdJWc8ntg8= github.com/zhangpeihao/goamf v0.0.0-20140409082417-3ff2c19514a8/go.mod h1:RZd/IqzNpFANwOB9rVmsnAYpo/6KesK4PqrN1a5cRgg= +go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw= +go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI= +go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= +go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= diff --git a/main.go b/main.go index dc7e177..12a56da 100644 --- a/main.go +++ b/main.go @@ -10,7 +10,6 @@ import ( . "github.com/Monibuca/engine/v4" "github.com/Monibuca/engine/v4/codec" - "github.com/Monibuca/engine/v4/common" "github.com/Monibuca/engine/v4/config" "github.com/Monibuca/engine/v4/util" . "github.com/logrusorgru/aurora" @@ -27,12 +26,18 @@ type HDLConfig struct { var streamPathReg = regexp.MustCompile(`/(hdl/)?((.+)(\.flv)|(.+))`) -func (c *HDLConfig) Update(override config.Config) { - if c.ListenAddr != "" || c.ListenAddrTLS != "" { - plugin.Info(Green("HDL Server Start").String(), zap.String("ListenAddr", c.ListenAddr), zap.String("ListenAddrTLS", c.ListenAddrTLS)) - c.Listen(plugin, c) +func (c *HDLConfig) OnEvent(event any) { + switch event.(type) { + case FirstConfig: + if c.ListenAddr != "" || c.ListenAddrTLS != "" { + plugin.Info(Green("HDL Server Start").String(), zap.String("ListenAddr", c.ListenAddr), zap.String("ListenAddrTLS", c.ListenAddrTLS)) + go c.Listen(plugin, c) + } else { + plugin.Info(Green("HDL start reuse engine port").String()) + } } } + func (c *HDLConfig) API_Pull(rw http.ResponseWriter, r *http.Request) { var puller Puller puller.StreamPath = r.URL.Query().Get("streamPath") @@ -55,7 +60,6 @@ func (*HDLConfig) API_List(rw http.ResponseWriter, r *http.Request) { var Config = new(HDLConfig) // 确保HDLConfig实现了PullPlugin接口 -var _ PullPlugin = Config var plugin = InstallPlugin(Config) @@ -65,19 +69,14 @@ type HDLSubscriber struct { func (sub *HDLSubscriber) OnEvent(event any) { switch v := event.(type) { - case AudioDeConf: - if sub.AudioTrack.IsAAC() { - v.FLV.WriteTo(sub) - } - case VideoDeConf: - v.FLV.WriteTo(sub) - case common.MediaFrame: + case HaveFLV: flvTag := v.GetFLV() flvTag.WriteTo(sub) default: sub.Subscriber.OnEvent(event) } } + func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { parts := streamPathReg.FindStringSubmatch(r.RequestURI) if len(parts) == 0 { @@ -93,7 +92,7 @@ func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { sub := &HDLSubscriber{} sub.ID = r.RemoteAddr sub.OnEvent(r.Context()) - if plugin.Subscribe(stringPath, sub) { + if err := plugin.Subscribe(stringPath, sub); err == nil { if sub.Stream.Publisher == nil { w.WriteHeader(404) return @@ -143,7 +142,7 @@ func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_SCRIPT, 0, net.Buffers{buffer.Bytes()}) sub.PlayBlock(sub) } else { - w.WriteHeader(500) + http.Error(w, err.Error(), http.StatusBadRequest) } } func WriteEcmaArray(w amf.Writer, o amf.Object) (n int, err error) { diff --git a/pull.go b/pull.go index 2ad1e2f..ad034d4 100644 --- a/pull.go +++ b/pull.go @@ -70,7 +70,7 @@ func (puller *HDLPuller) OnEvent(event any) { }(v + 1) } else { // TODO: 发布失败重新发布 - if plugin.Publish(puller.StreamPath, puller) { + if plugin.Publish(puller.StreamPath, puller) == nil { if strings.HasPrefix(puller.RemoteURL, "http") { if res, err := http.Get(puller.RemoteURL); err == nil { puller.Reader = res.Body