按照duration切片

This commit is contained in:
yangjiechina
2024-03-07 10:07:21 +08:00
parent de6cca48ca
commit 096493aa44
5 changed files with 39 additions and 22 deletions

View File

@@ -13,10 +13,8 @@ type tsContext struct {
writeBuffer []byte
writeBufferSize int
duration int
playlistLength int
url string
path string
url string
path string
file *os.File
}
@@ -31,6 +29,7 @@ type Stream struct {
m3u8Name string
tsFormat string
dir string
duration int
m3u8File *os.File
}
@@ -58,6 +57,7 @@ func NewTransStream(url, m3u8Name, tsFormat, dir string, segmentDuration, playli
m3u8Name: m3u8Name,
tsFormat: tsFormat,
dir: dir,
duration: segmentDuration,
}
muxer := libmpeg.NewTSMuxer()
@@ -68,8 +68,6 @@ func NewTransStream(url, m3u8Name, tsFormat, dir string, segmentDuration, playli
segmentSeq: 0,
writeBuffer: make([]byte, 1024*1024),
writeBufferSize: 0,
duration: segmentDuration,
playlistLength: playlistLength,
}
stream_.muxer = muxer
@@ -83,7 +81,8 @@ func (t *Stream) Input(packet utils.AVPacket) error {
return fmt.Errorf("track not available")
}
if utils.AVMediaTypeVideo == packet.MediaType() && packet.KeyFrame() || t.context.file == nil {
//创建一下个切片
if (!t.ExistVideo || utils.AVMediaTypeVideo == packet.MediaType() && packet.KeyFrame()) && float32(t.muxer.Duration())/90000 >= float32(t.duration) {
if err := t.createSegment(); err != nil {
return err
}
@@ -116,7 +115,7 @@ func (t *Stream) AddTrack(stream utils.AVStream) error {
}
func (t *Stream) WriteHeader() error {
return nil
return t.createSegment()
}
func (t *Stream) onTSWrite(data []byte) {

View File

@@ -59,18 +59,11 @@ type Segment struct {
}
type m3u8Writer struct {
stringBuffer *bytes.Buffer
targetDuration int
playlist *stream.Queue
stringBuffer *bytes.Buffer
playlist *stream.Queue
}
func (m *m3u8Writer) AddSegment(duration float32 /*title string,*/, url string, sequence int) {
//影响播放器缓存.
round := int(math.Ceil(float64(duration)))
if round > m.targetDuration {
m.targetDuration = round
}
if m.playlist.IsFull() {
m.playlist.Pop()
}
@@ -78,6 +71,31 @@ func (m *m3u8Writer) AddSegment(duration float32 /*title string,*/, url string,
m.playlist.Push(Segment{duration: duration, url: url, sequence: sequence})
}
func (m *m3u8Writer) targetDuration() int {
var targetDuration int
head, tail := m.playlist.Data()
compute := func(playlist []interface{}) {
for _, segment := range playlist {
//影响播放器缓存.
round := int(math.Ceil(float64(segment.(Segment).duration)))
if round > targetDuration {
targetDuration = round
}
}
}
if head != nil {
compute(head)
}
if tail != nil {
compute(tail)
}
return targetDuration
}
func (m *m3u8Writer) ToString() string {
//暂时只实现简单的播放列表
head, tail := m.playlist.Data()
@@ -90,7 +108,7 @@ func (m *m3u8Writer) ToString() string {
//暂时只实现第三个版本
m.stringBuffer.WriteString("#EXT-X-VERSION:3\r\n")
m.stringBuffer.WriteString("#EXT-X-TARGETDURATION:")
m.stringBuffer.WriteString(strconv.Itoa(m.targetDuration))
m.stringBuffer.WriteString(strconv.Itoa(m.targetDuration()))
m.stringBuffer.WriteString("\r\n")
m.stringBuffer.WriteString("#ExtXMediaSequence:")
m.stringBuffer.WriteString(strconv.Itoa(head[0].(Segment).sequence))

View File

@@ -21,7 +21,7 @@ func CreateTransStream(source stream.ISource, protocol stream.Protocol, streams
m3u8Name := id + ".m3u8"
tsFormat := id + "_%d.ts"
transStream, err := hls.NewTransStream("/live/hls/", m3u8Name, tsFormat, "../tmp/", 2, 10)
transStream, err := hls.NewTransStream("", m3u8Name, tsFormat, "../tmp/", 2, 10)
if err != nil {
panic(err)
}

View File

@@ -8,7 +8,7 @@ import (
"testing"
)
func CreateTransStream(protocol stream.Protocol, streams []utils.AVStream) stream.ITransStream {
func CreateTransStream(source stream.ISource, protocol stream.Protocol, streams []utils.AVStream) stream.ITransStream {
if stream.ProtocolRtmp == protocol {
return NewTransStream(librtmp.ChunkSize)
}

View File

@@ -89,7 +89,7 @@ type TransStreamImpl struct {
Tracks []utils.AVStream
transBuffer MemoryPool //每个TransStream也缓存封装后的流
Completed bool
existVideo bool
ExistVideo bool
}
func (t *TransStreamImpl) Input(packet utils.AVPacket) error {
@@ -99,7 +99,7 @@ func (t *TransStreamImpl) Input(packet utils.AVPacket) error {
func (t *TransStreamImpl) AddTrack(stream utils.AVStream) error {
t.Tracks = append(t.Tracks, stream)
if utils.AVMediaTypeVideo == stream.Type() {
t.existVideo = true
t.ExistVideo = true
}
return nil
}