优化HLS写入内存优化

This commit is contained in:
dexter
2023-02-11 22:10:19 +08:00
parent 6544002a4b
commit 4bced734db

29
hls.go
View File

@@ -11,6 +11,7 @@ import (
. "m7s.live/engine/v4" . "m7s.live/engine/v4"
"m7s.live/engine/v4/codec" "m7s.live/engine/v4/codec"
"m7s.live/engine/v4/codec/mpegts" "m7s.live/engine/v4/codec/mpegts"
"m7s.live/engine/v4/util"
"m7s.live/plugin/hls/v4" "m7s.live/plugin/hls/v4"
) )
@@ -20,6 +21,7 @@ type HLSRecorder struct {
packet mpegts.MpegTsPESPacket packet mpegts.MpegTsPESPacket
Recorder Recorder
tsWriter io.WriteCloser tsWriter io.WriteCloser
MemoryTs
} }
func (h *HLSRecorder) Start(streamPath string) error { func (h *HLSRecorder) Start(streamPath string) error {
@@ -28,6 +30,7 @@ func (h *HLSRecorder) Start(streamPath string) error {
if _, ok := RecordPluginConfig.recordings.Load(h.ID); ok { if _, ok := RecordPluginConfig.recordings.Load(h.ID); ok {
return ErrRecordExist return ErrRecordExist
} }
h.BytesPool = make(util.BytesPool, 17)
return plugin.Subscribe(streamPath, h) return plugin.Subscribe(streamPath, h)
} }
@@ -54,27 +57,21 @@ func (h *HLSRecorder) OnEvent(event any) {
if err = h.createHlsTsSegmentFile(); err != nil { if err = h.createHlsTsSegmentFile(); err != nil {
return return
} }
go h.start() go h.start()
case AudioFrame: case AudioFrame:
if h.packet, err = hls.AudioPacketToPES(&v, &h.Audio.AudioSpecificConfig); err != nil {
return
}
pes := &mpegts.MpegtsPESFrame{ pes := &mpegts.MpegtsPESFrame{
Pid: mpegts.PID_AUDIO, Pid: mpegts.PID_AUDIO,
IsKeyFrame: false, IsKeyFrame: false,
ContinuityCounter: h.audio_cc, ContinuityCounter: h.audio_cc,
ProgramClockReferenceBase: uint64(v.DTS - h.SkipTS*90), ProgramClockReferenceBase: uint64(v.DTS),
}
//frame.ProgramClockReferenceBase = 0
if err = mpegts.WritePESPacket(h.tsWriter, pes, h.packet); err != nil {
return
} }
h.WriteAudioFrame(&v, &h.Audio.AudioSpecificConfig, pes)
h.BLL.WriteTo(h.tsWriter)
h.Recycle()
h.Clear()
h.audio_cc = pes.ContinuityCounter h.audio_cc = pes.ContinuityCounter
case VideoFrame: case VideoFrame:
h.packet, err = hls.VideoPacketToPES(&v, h.Video)
if err != nil {
return
}
if h.Fragment != 0 && h.newFile { if h.Fragment != 0 && h.newFile {
h.newFile = false h.newFile = false
h.tsWriter.Close() h.tsWriter.Close()
@@ -86,14 +83,16 @@ func (h *HLSRecorder) OnEvent(event any) {
Pid: mpegts.PID_VIDEO, Pid: mpegts.PID_VIDEO,
IsKeyFrame: v.IFrame, IsKeyFrame: v.IFrame,
ContinuityCounter: h.video_cc, ContinuityCounter: h.video_cc,
ProgramClockReferenceBase: uint64(v.DTS - h.SkipTS*90), ProgramClockReferenceBase: uint64(v.DTS),
} }
if err = mpegts.WritePESPacket(h.tsWriter, pes, h.packet); err != nil { if err = h.WriteVideoFrame(&v, h.Video.ParamaterSets, pes); err != nil {
return return
} }
h.BLL.WriteTo(h.tsWriter)
h.Recycle()
h.Clear()
h.video_cc = pes.ContinuityCounter h.video_cc = pes.ContinuityCounter
} }
} }
// 创建一个新的ts文件 // 创建一个新的ts文件