diff --git a/go.mod b/go.mod index 8f7f816..a9912fa 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module github.com/Monibuca/plugin-hdl/v4 +module m7s.live/plugin/hdl/v4 go 1.18 diff --git a/main.go b/main.go index 2f589af..00d6d36 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -package hdl +package hdl // import "m7s.live/plugin/hdl/v4" import ( "bytes" @@ -8,13 +8,13 @@ import ( "strings" "time" - . "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" "go.uber.org/zap" + . "m7s.live/engine/v4" + "m7s.live/engine/v4/codec" + "m7s.live/engine/v4/config" + "m7s.live/engine/v4/util" ) type HDLConfig struct { @@ -34,18 +34,17 @@ func (c *HDLConfig) OnEvent(event any) { plugin.Info(Green("HDL start reuse engine port").String()) } case PullerPromise: - puller := v.Value client := &HDLPuller{ - Puller: puller, + Puller: v.Value, } err := client.connect() if err == nil { - if err = plugin.Publish(puller.StreamPath, client); err == nil { + if err = plugin.Publish(client.StreamPath, client); err == nil { v.Resolve(util.Null) break } } - client.Error(puller.RemoteURL, zap.Error(err)) + client.Error(client.RemoteURL, zap.Error(err)) v.Reject(err) } } @@ -73,14 +72,16 @@ func (sub *HDLSubscriber) OnEvent(event any) { switch v := event.(type) { case HaveFLV: flvTag := v.GetFLV() - flvTag.WriteTo(sub) + if _, err := flvTag.WriteTo(sub); err != nil { + sub.Stop() + } default: sub.Subscriber.OnEvent(event) } } func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { - streamPath := strings.TrimPrefix(r.URL.Path, "/hls") + streamPath := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/hdl/"), ".flv") w.Header().Set("Transfer-Encoding", "chunked") w.Header().Set("Content-Type", "video/x-flv") sub := &HDLSubscriber{} @@ -113,22 +114,19 @@ func (*HDLConfig) ServeHTTP(w http.ResponseWriter, r *http.Request) { var flags byte if hasAudio { flags |= (1 << 2) - } - if hasVideo { - flags |= 1 - } - w.Write([]byte{'F', 'L', 'V', 0x01, flags, 0, 0, 0, 9, 0, 0, 0, 0}) - if hasVideo { - metaData["videocodecid"] = int(vt.CodecID) - metaData["width"] = vt.SPSInfo.Width - metaData["height"] = vt.SPSInfo.Height - } - if hasVideo { metaData["audiocodecid"] = int(at.CodecID) metaData["audiosamplerate"] = at.SampleRate metaData["audiosamplesize"] = at.SampleSize metaData["stereo"] = at.Channels == 2 } + if hasVideo { + flags |= 1 + metaData["videocodecid"] = int(vt.CodecID) + metaData["width"] = vt.SPSInfo.Width + metaData["height"] = vt.SPSInfo.Height + } + // 写入FLV头 + w.Write([]byte{'F', 'L', 'V', 0x01, flags, 0, 0, 0, 9, 0, 0, 0, 0}) codec.WriteFLVTag(w, codec.FLV_TAG_TYPE_SCRIPT, 0, net.Buffers{buffer.Bytes()}) sub.PlayBlock(sub) } else { diff --git a/pull.go b/pull.go index 8ae2992..1a788b3 100644 --- a/pull.go +++ b/pull.go @@ -7,11 +7,11 @@ import ( "os" "strings" - . "github.com/Monibuca/engine/v4" - "github.com/Monibuca/engine/v4/codec" - "github.com/Monibuca/engine/v4/log" - "github.com/Monibuca/engine/v4/util" "go.uber.org/zap" + . "m7s.live/engine/v4" + "m7s.live/engine/v4/codec" + "m7s.live/engine/v4/log" + "m7s.live/engine/v4/util" ) func (puller *HDLPuller) connect() (err error) { @@ -36,7 +36,9 @@ func (puller *HDLPuller) connect() (err error) { func (puller *HDLPuller) pull() { var err error defer func() { - puller.Closer.Close() + if puller.Closer != nil { + puller.Closer.Close() + } if !puller.Stream.IsClosed() { if err = puller.connect(); err == nil { go puller.pull()