适配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 go 1.13
require ( 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/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 h1:TJmC6eZA1lR1MScWgempZLiEZD4T6aY/nn/rlQ9UdK8=
github.com/Monibuca/engine v1.2.1/go.mod h1:WbDkXENLjcPjyjCR1Mix1GA+uAlwORkv/+8aMVrDX2g= 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 h1:G0m3OIz70MZUWq3EgK3CesDbo8upS2Vm9/P3FtgI+Jk=
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= 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= github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=

43
main.go
View File

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