diff --git a/flv.go b/flv.go index 0def99b..694418c 100644 --- a/flv.go +++ b/flv.go @@ -43,8 +43,9 @@ func SaveFlv(streamPath string, append bool) error { } // return avformat.WriteFLVTag(file, packet) p := Subscriber{ - ID: filePath, - Type: "FlvRecord", + ID: filePath, + Type: "FlvRecord", + ByteStreamFormat: true, } var offsetTime uint32 if append { @@ -56,24 +57,23 @@ func SaveFlv(streamPath string, append bool) error { if err == nil { recordings.Store(filePath, &p) if err := p.Subscribe(streamPath); err == nil { - at, vt := p.WaitAudioTrack("aac", "pcma", "pcmu"), p.WaitVideoTrack("h264") - tag0 := at.RtmpTag[0] + vt, at := p.WaitVideoTrack(), p.WaitAudioTrack() p.OnAudio = func(audio AudioPack) { - if !append && tag0>>4 == 10 { //AAC格式需要发送AAC头 - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, 0, at.RtmpTag) + if !append && at.CodecID == 10 { //AAC格式需要发送AAC头 + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, 0, at.ExtraData) } - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, audio.Timestamp+offsetTime, audio.ToRTMPTag(tag0)) + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, audio.Timestamp+offsetTime, audio.Payload) p.OnAudio = func(audio AudioPack) { - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, audio.Timestamp+offsetTime, audio.ToRTMPTag(tag0)) + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, audio.Timestamp+offsetTime, audio.Payload) } } p.OnVideo = func(video VideoPack) { if !append { - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, 0, vt.RtmpTag) + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, 0, vt.ExtraData.Payload) } - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, video.Timestamp+offsetTime, video.ToRTMPTag()) + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, video.Timestamp+offsetTime, video.Payload) p.OnVideo = func(video VideoPack) { - codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, video.Timestamp+offsetTime, video.ToRTMPTag()) + codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_VIDEO, video.Timestamp+offsetTime, video.Payload) } } go func() { diff --git a/go.mod b/go.mod index 39572c1..fead900 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/Monibuca/plugin-record/v3 go 1.13 require ( - github.com/Monibuca/engine/v3 v3.0.0-beta - github.com/Monibuca/utils/v3 v3.0.0-alpha5 + github.com/Monibuca/engine/v3 v3.0.0-beta5 + github.com/Monibuca/utils/v3 v3.0.0-beta github.com/pion/rtp v1.6.5 // indirect ) diff --git a/go.sum b/go.sum index b7cb607..6434f45 100644 --- a/go.sum +++ b/go.sum @@ -2,9 +2,13 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/Monibuca/engine/v3 v3.0.0-beta h1:Nquu3/ByNsZtnIJHG1LUWuaZkKNMLnh+uBuGvN0gCDE= github.com/Monibuca/engine/v3 v3.0.0-beta/go.mod h1:eonu3UFn3W7NpHzSrACipxdAyOBCUwzlFUe1R7JjttE= +github.com/Monibuca/engine/v3 v3.0.0-beta5 h1:b27ZQDfvf5dBMZbCSIUXItUwVIFs95fpkAV4xjN7BNE= +github.com/Monibuca/engine/v3 v3.0.0-beta5/go.mod h1:SMgnlwih4pBA/HkTLjKXZFYkv3ukRzFjv65CARRLVIk= github.com/Monibuca/utils/v3 v3.0.0-alpha4/go.mod h1:3xYmhQbgAZBHLyIMteUCd1va+1z/xnd72B585mCaT3c= github.com/Monibuca/utils/v3 v3.0.0-alpha5 h1:IOyW/KJSRdRg+TPcgwkHLBynqfNQOV6p3iP7LgXEMFc= github.com/Monibuca/utils/v3 v3.0.0-alpha5/go.mod h1:3xYmhQbgAZBHLyIMteUCd1va+1z/xnd72B585mCaT3c= +github.com/Monibuca/utils/v3 v3.0.0-beta h1:z4p/BSH5J9Ja/gwoDmj1RyN+b0q28Nmn/fqXiwq2hGY= +github.com/Monibuca/utils/v3 v3.0.0-beta/go.mod h1:mQYP/OMox1tkWP6Qut7pBfARr1TXSRkK662dexQl6kI= github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0= github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo= github.com/funny/utest v0.0.0-20161029064919-43870a374500 h1:Z0r1CZnoIWFB/Uiwh1BU5FYmuFe6L5NPi6XWQEmsTRg= @@ -23,6 +27,8 @@ github.com/pion/rtp v1.6.5 h1:o2cZf8OascA5HF/b0PAbTxRKvOWxTQxWYt7SlToxFGI= github.com/pion/rtp v1.6.5/go.mod h1:bDb5n+BFZxXx0Ea7E5qe+klMuqiBrP+w8XSjiWtCUko= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/q191201771/naza v0.19.1 h1:4KLcxT2CHztO+7miPRtBG3FFgadSQYQw1gPPPKN7rnY= +github.com/q191201771/naza v0.19.1/go.mod h1:5LeGupZZFtYP1g/S203n9vXoUNVdlRnPIfM6rExjqt0= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/main.go b/main.go index c553558..9f4fb3d 100644 --- a/main.go +++ b/main.go @@ -15,7 +15,6 @@ import ( var config struct { Path string - AutoPublish bool AutoRecord bool } var recordings sync.Map @@ -32,9 +31,6 @@ func init() { Config: &config, Run: run, HotConfig: map[string]func(interface{}){ - "AutoPublish": func(v interface{}) { - config.AutoPublish = v.(bool) - }, "AutoRecord": func(v interface{}) { config.AutoRecord = v.(bool) }, @@ -42,7 +38,6 @@ func init() { }) } func run() { - go AddHook(HOOK_SUBSCRIBE, onSubscribe) go AddHook(HOOK_PUBLISH, onPublish) os.MkdirAll(config.Path, 0755) http.HandleFunc("/api/record/flv/list", func(w http.ResponseWriter, r *http.Request) { @@ -116,15 +111,7 @@ func run() { } }) } -func onSubscribe(v interface{}) { - s := v.(*Subscriber) - if config.AutoPublish { - filePath := filepath.Join(config.Path, s.StreamPath+".flv") - if s.Publisher == nil && Exist(filePath) { - PublishFlvFile(s.StreamPath) - } - } -} + func onPublish(v interface{}) { p := v.(*Stream) if config.AutoRecord { diff --git a/publisher.go b/publisher.go index f415c52..99f86d9 100644 --- a/publisher.go +++ b/publisher.go @@ -8,39 +8,35 @@ import ( "time" . "github.com/Monibuca/engine/v3" - . "github.com/Monibuca/utils/v3" "github.com/Monibuca/utils/v3/codec" ) -type FlvFile struct { - Publisher -} - func PublishFlvFile(streamPath string) error { flvPath := filepath.Join(config.Path, streamPath+".flv") os.MkdirAll(filepath.Dir(flvPath), 0755) if file, err := os.Open(flvPath); err == nil { - var stream FlvFile - if stream.Publish(streamPath) { - stream.Type = "FlvFile" + stream := Stream{ + Type: "FlvFile", + StreamPath: streamPath, + } + if stream.Publish() { defer stream.Close() file.Seek(int64(len(codec.FLVHeader)), io.SeekStart) var lastTime uint32 - at := NewAudioTrack() - vt := NewVideoTrack() - stream.SetOriginAT(at) + at := stream.NewAudioTrack(0) + vt := stream.NewVideoTrack(0) for { if t, timestamp, payload, err := codec.ReadFLVTag(file); err == nil { switch t { case codec.FLV_TAG_TYPE_AUDIO: - at.Push(timestamp, payload) + at.PushByteStream(AudioPack{Timestamp: timestamp, Payload: payload}) case codec.FLV_TAG_TYPE_VIDEO: if timestamp != 0 { if lastTime == 0 { lastTime = timestamp } } - vt.Push(VideoPack{Timestamp: timestamp, CompositionTime: BigEndian.Uint24(payload[2:5]), Payload: payload[5:]}) + vt.PushByteStream(VideoPack{Timestamp: timestamp, Payload: payload}) time.Sleep(time.Duration(timestamp-lastTime) * time.Millisecond) lastTime = timestamp }