适配2.0

This commit is contained in:
李宇翔
2020-05-01 19:43:58 +08:00
parent 56a98c3218
commit a017f31fd6
3 changed files with 23 additions and 27 deletions

2
go.mod
View File

@@ -3,5 +3,5 @@ module github.com/Monibuca/plugin-ts
go 1.13
require (
github.com/Monibuca/engine v1.2.1
github.com/Monibuca/engine/v2 v2.0.0-alpha2
)

5
go.sum
View File

@@ -2,6 +2,11 @@ github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/Monibuca/engine v1.2.1 h1:TJmC6eZA1lR1MScWgempZLiEZD4T6aY/nn/rlQ9UdK8=
github.com/Monibuca/engine v1.2.1/go.mod h1:WbDkXENLjcPjyjCR1Mix1GA+uAlwORkv/+8aMVrDX2g=
github.com/Monibuca/engine v1.2.2 h1:hNjsrZpOmui8lYhgCJ5ltJU8g/k0Rrdysx2tHNGGnbI=
github.com/Monibuca/engine/v2 v2.0.0-alpha1 h1:RT+rKOJC1MdFNZp6gADGdzGLxEhYN35MKEAx7FwHtps=
github.com/Monibuca/engine/v2 v2.0.0-alpha1/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc=
github.com/Monibuca/engine/v2 v2.0.0-alpha2 h1:45yazqnnxEEcfHcOJGuIr1xtnBzQT6cPvhlymeZrDmA=
github.com/Monibuca/engine/v2 v2.0.0-alpha2/go.mod h1:34EYjjV15G6myuHOKaJkO7y5tJ1Arq/NfC9Weacr2mc=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

43
main.go
View File

@@ -11,10 +11,10 @@ import (
"strings"
"time"
. "github.com/Monibuca/engine"
"github.com/Monibuca/engine/avformat"
"github.com/Monibuca/engine/avformat/mpegts"
"github.com/Monibuca/engine/util"
. "github.com/Monibuca/engine/v2"
"github.com/Monibuca/engine/v2/avformat"
"github.com/Monibuca/engine/v2/avformat/mpegts"
"github.com/Monibuca/engine/v2/util"
)
var config = struct {
@@ -30,7 +30,7 @@ func init() {
Config: &config,
Run: func() {
if config.AutoPublish {
OnSubscribeHooks.AddHook(func(s *OutputStream) {
OnSubscribeHooks.AddHook(func(s *Subscriber) {
if s.Publisher == nil {
go new(TS).PublishDir(s.StreamPath)
}
@@ -48,7 +48,7 @@ type TSDir struct {
TotalSize int64
}
type TS struct {
InputStream
Publisher
*mpegts.MpegTsStream
TSInfo
//TsChan chan io.Reader
@@ -61,7 +61,7 @@ type TSInfo struct {
DTS uint64
PesCount int
BufferLength int
RoomInfo *RoomInfo
StreamInfo *StreamInfo
}
func (ts *TS) run() {
@@ -91,16 +91,13 @@ func (ts *TS) run() {
if frameLen > remainLen {
break
}
av := avformat.NewAVPacket(avformat.FLV_TAG_TYPE_AUDIO)
av.Payload = data[:frameLen]
ts.PushAudio(av)
ts.PushAudio(uint32(tsPesPkt.PesPkt.Header.Pts/90), data[:frameLen])
data = data[frameLen:remainLen]
remainLen = remainLen - frameLen
}
case mpegts.STREAM_ID_VIDEO:
var err error
av := avformat.NewAVPacket(avformat.FLV_TAG_TYPE_VIDEO)
ts.PTS = tsPesPkt.PesPkt.Header.Pts
ts.DTS = tsPesPkt.PesPkt.Header.Dts
lastDts := ts.lastDts
@@ -109,7 +106,6 @@ func (ts *TS) run() {
if dts == 0 {
dts = pts
}
av.Timestamp = uint32(dts / 90)
if ts.lastDts == 0 {
ts.lastDts = dts
}
@@ -141,16 +137,11 @@ func (ts *TS) run() {
util.BigEndian.PutUint16(ppsHead[1:], uint16(vl))
_, err = r.Write(ppsHead)
_, err = r.Write(v)
av.VideoFrameType = 1
av.Payload = r.Bytes()
ts.PushVideo(av)
av = avformat.NewAVPacket(avformat.FLV_TAG_TYPE_VIDEO)
av.Timestamp = uint32(dts / 90)
ts.PushVideo(0, r.Bytes())
r = bytes.NewBuffer([]byte{})
continue
case avformat.NALU_IDR_Picture:
if isFirst {
av.VideoFrameType = 1
util.BigEndian.PutUint24(iframeHead[2:], compostionTime)
_, err = r.Write(iframeHead)
}
@@ -158,7 +149,6 @@ func (ts *TS) run() {
_, err = r.Write(nalLength)
case avformat.NALU_Non_IDR_Picture:
if isFirst {
av.VideoFrameType = 2
util.BigEndian.PutUint24(pframeHead[2:], compostionTime)
_, err = r.Write(pframeHead)
} else {
@@ -174,8 +164,7 @@ func (ts *TS) run() {
if MayBeError(err) {
return
}
av.Payload = r.Bytes()
ts.PushVideo(av)
ts.PushVideo(uint32(dts/90), r.Bytes())
t2 := time.Since(t1)
if duration != 0 && t2 < duration {
if duration < time.Second {
@@ -196,22 +185,24 @@ func (ts *TS) run() {
}
}
}
func (ts *TS) Publish(streamPath string, publisher Publisher) (result bool) {
if result = ts.InputStream.Publish(streamPath, publisher); result {
ts.TSInfo.RoomInfo = &ts.Room.RoomInfo
func (ts *TS) Publish(streamPath string) (result bool) {
ts.Type = "TS"
if result = ts.Publisher.Publish(streamPath); result {
ts.TSInfo.StreamInfo = &ts.Stream.StreamInfo
ts.MpegTsStream = mpegts.NewMpegTsStream(config.BufferLength)
go ts.run()
}
return
}
func (ts *TS) PublishDir(streamPath string) {
ts.Type = "TSFiles"
dirPath := filepath.Join(config.Path, streamPath)
files, err := ioutil.ReadDir(dirPath)
if err != nil || len(files) == 0 {
return
}
if ts.InputStream.Publish(strings.ReplaceAll(streamPath, "\\", "/"), ts) {
ts.TSInfo.RoomInfo = &ts.Room.RoomInfo
if ts.Publisher.Publish(strings.ReplaceAll(streamPath, "\\", "/")) {
ts.TSInfo.StreamInfo = &ts.Stream.StreamInfo
ts.MpegTsStream = mpegts.NewMpegTsStream(0)
go ts.run()
for _, file := range files {