适配引擎版本升级

This commit is contained in:
langhuihui
2021-06-15 08:07:09 +08:00
parent ec6c4208ff
commit e02dc36bdd
5 changed files with 29 additions and 40 deletions

22
flv.go
View File

@@ -43,8 +43,9 @@ func SaveFlv(streamPath string, append bool) error {
} }
// return avformat.WriteFLVTag(file, packet) // return avformat.WriteFLVTag(file, packet)
p := Subscriber{ p := Subscriber{
ID: filePath, ID: filePath,
Type: "FlvRecord", Type: "FlvRecord",
ByteStreamFormat: true,
} }
var offsetTime uint32 var offsetTime uint32
if append { if append {
@@ -56,24 +57,23 @@ func SaveFlv(streamPath string, append bool) error {
if err == nil { if err == nil {
recordings.Store(filePath, &p) recordings.Store(filePath, &p)
if err := p.Subscribe(streamPath); err == nil { if err := p.Subscribe(streamPath); err == nil {
at, vt := p.WaitAudioTrack("aac", "pcma", "pcmu"), p.WaitVideoTrack("h264") vt, at := p.WaitVideoTrack(), p.WaitAudioTrack()
tag0 := at.RtmpTag[0]
p.OnAudio = func(audio AudioPack) { p.OnAudio = func(audio AudioPack) {
if !append && tag0>>4 == 10 { //AAC格式需要发送AAC头 if !append && at.CodecID == 10 { //AAC格式需要发送AAC头
codec.WriteFLVTag(file, codec.FLV_TAG_TYPE_AUDIO, 0, at.RtmpTag) 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) { 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) { p.OnVideo = func(video VideoPack) {
if !append { 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) { 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() { go func() {

4
go.mod
View File

@@ -3,7 +3,7 @@ module github.com/Monibuca/plugin-record/v3
go 1.13 go 1.13
require ( require (
github.com/Monibuca/engine/v3 v3.0.0-beta github.com/Monibuca/engine/v3 v3.0.0-beta5
github.com/Monibuca/utils/v3 v3.0.0-alpha5 github.com/Monibuca/utils/v3 v3.0.0-beta
github.com/pion/rtp v1.6.5 // indirect github.com/pion/rtp v1.6.5 // indirect
) )

6
go.sum
View File

@@ -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/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 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-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-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 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-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 h1:Db9StoJ6RZN3YttC0Pm0I4Y5izITRYch3RMbT59BYN0=
github.com/funny/slab v0.0.0-20180511031532-b1fad5e5d478/go.mod h1:0j1+svBH8ABEIPdUP0AIg4qedsybnXGJBakCEw8cfoo= 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= 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/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 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= 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 h1:DcqTD9SDLc+1P/r1EmRBwnVsrOwW+kk2vWf9n+1sGhs=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= 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= golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=

15
main.go
View File

@@ -15,7 +15,6 @@ import (
var config struct { var config struct {
Path string Path string
AutoPublish bool
AutoRecord bool AutoRecord bool
} }
var recordings sync.Map var recordings sync.Map
@@ -32,9 +31,6 @@ func init() {
Config: &config, Config: &config,
Run: run, Run: run,
HotConfig: map[string]func(interface{}){ HotConfig: map[string]func(interface{}){
"AutoPublish": func(v interface{}) {
config.AutoPublish = v.(bool)
},
"AutoRecord": func(v interface{}) { "AutoRecord": func(v interface{}) {
config.AutoRecord = v.(bool) config.AutoRecord = v.(bool)
}, },
@@ -42,7 +38,6 @@ func init() {
}) })
} }
func run() { func run() {
go AddHook(HOOK_SUBSCRIBE, onSubscribe)
go AddHook(HOOK_PUBLISH, onPublish) go AddHook(HOOK_PUBLISH, onPublish)
os.MkdirAll(config.Path, 0755) os.MkdirAll(config.Path, 0755)
http.HandleFunc("/api/record/flv/list", func(w http.ResponseWriter, r *http.Request) { 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{}) { func onPublish(v interface{}) {
p := v.(*Stream) p := v.(*Stream)
if config.AutoRecord { if config.AutoRecord {

View File

@@ -8,39 +8,35 @@ import (
"time" "time"
. "github.com/Monibuca/engine/v3" . "github.com/Monibuca/engine/v3"
. "github.com/Monibuca/utils/v3"
"github.com/Monibuca/utils/v3/codec" "github.com/Monibuca/utils/v3/codec"
) )
type FlvFile struct {
Publisher
}
func PublishFlvFile(streamPath string) error { func PublishFlvFile(streamPath string) error {
flvPath := filepath.Join(config.Path, streamPath+".flv") flvPath := filepath.Join(config.Path, streamPath+".flv")
os.MkdirAll(filepath.Dir(flvPath), 0755) os.MkdirAll(filepath.Dir(flvPath), 0755)
if file, err := os.Open(flvPath); err == nil { if file, err := os.Open(flvPath); err == nil {
var stream FlvFile stream := Stream{
if stream.Publish(streamPath) { Type: "FlvFile",
stream.Type = "FlvFile" StreamPath: streamPath,
}
if stream.Publish() {
defer stream.Close() defer stream.Close()
file.Seek(int64(len(codec.FLVHeader)), io.SeekStart) file.Seek(int64(len(codec.FLVHeader)), io.SeekStart)
var lastTime uint32 var lastTime uint32
at := NewAudioTrack() at := stream.NewAudioTrack(0)
vt := NewVideoTrack() vt := stream.NewVideoTrack(0)
stream.SetOriginAT(at)
for { for {
if t, timestamp, payload, err := codec.ReadFLVTag(file); err == nil { if t, timestamp, payload, err := codec.ReadFLVTag(file); err == nil {
switch t { switch t {
case codec.FLV_TAG_TYPE_AUDIO: case codec.FLV_TAG_TYPE_AUDIO:
at.Push(timestamp, payload) at.PushByteStream(AudioPack{Timestamp: timestamp, Payload: payload})
case codec.FLV_TAG_TYPE_VIDEO: case codec.FLV_TAG_TYPE_VIDEO:
if timestamp != 0 { if timestamp != 0 {
if lastTime == 0 { if lastTime == 0 {
lastTime = timestamp 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) time.Sleep(time.Duration(timestamp-lastTime) * time.Millisecond)
lastTime = timestamp lastTime = timestamp
} }