mirror of
https://github.com/langhuihui/monibuca.git
synced 2025-12-24 13:48:04 +08:00
memory leak
This commit is contained in:
@@ -15,13 +15,16 @@ import (
|
||||
|
||||
type HDLPuller struct {
|
||||
*util.BufReader
|
||||
*util.ScalableMemoryAllocator
|
||||
hasAudio bool
|
||||
hasVideo bool
|
||||
absTS uint32 //绝对时间戳
|
||||
}
|
||||
|
||||
func NewHDLPuller() *HDLPuller {
|
||||
return &HDLPuller{}
|
||||
return &HDLPuller{
|
||||
ScalableMemoryAllocator: util.NewScalableMemoryAllocator(1024),
|
||||
}
|
||||
}
|
||||
|
||||
func (puller *HDLPuller) Connect(p *m7s.Client) (err error) {
|
||||
@@ -51,9 +54,8 @@ func (puller *HDLPuller) Connect(p *m7s.Client) (err error) {
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
var head util.RecyclableMemory
|
||||
var head util.Memory
|
||||
head, err = puller.BufReader.ReadBytes(13)
|
||||
defer head.Recycle()
|
||||
if err == nil {
|
||||
var flvHead [3]byte
|
||||
var version, flag byte
|
||||
@@ -102,10 +104,15 @@ func (puller *HDLPuller) Pull(p *m7s.Puller) (err error) {
|
||||
}
|
||||
puller.ReadBE(3) // stream id always 0
|
||||
var frame rtmp.RTMPData
|
||||
frame.RecyclableMemory, err = puller.ReadBytes(int(dataSize))
|
||||
frame.ScalableMemoryAllocator = puller.ScalableMemoryAllocator
|
||||
mem, err := puller.ReadBytes(int(dataSize))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch t {
|
||||
case FLV_TAG_TYPE_AUDIO, FLV_TAG_TYPE_VIDEO:
|
||||
mem.CopyTo(frame.NextN(mem.Size))
|
||||
}
|
||||
puller.absTS = offsetTs + (timestamp - startTs)
|
||||
frame.Timestamp = puller.absTS
|
||||
// fmt.Println(t, offsetTs, timestamp, startTs, puller.absTS)
|
||||
@@ -116,7 +123,6 @@ func (puller *HDLPuller) Pull(p *m7s.Puller) (err error) {
|
||||
p.WriteVideo(frame.WrapVideo())
|
||||
case FLV_TAG_TYPE_SCRIPT:
|
||||
p.Info("script")
|
||||
frame.Recycle()
|
||||
}
|
||||
}
|
||||
return
|
||||
|
||||
@@ -50,7 +50,7 @@ func (avcc *RTMPAudio) Parse(t *AVTrack) (isIDR, isSeq bool, raw any, err error)
|
||||
return
|
||||
}
|
||||
var cloneFrame RTMPAudio
|
||||
cloneFrame.CopyFrom(avcc.Memory)
|
||||
cloneFrame.CopyFrom(&avcc.Memory)
|
||||
ctx.AudioObjectType = b0 >> 3
|
||||
ctx.SamplingFrequencyIndex = (b0 & 0x07 << 1) | (b1 >> 7)
|
||||
ctx.ChannelConfiguration = (b1 >> 3) & 0x0F
|
||||
|
||||
@@ -119,10 +119,9 @@ func (nc *NetConnection) simple_handshake(C1 []byte, checkC2 bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
C2.Recycle()
|
||||
if checkC2 {
|
||||
buf := nc.mediaDataPool.NextN(C2.Size)
|
||||
_, err = C2.Read(buf)
|
||||
C2.CopyTo(buf)
|
||||
if !bytes.Equal(buf[8:], S0S1[9:]) {
|
||||
return errors.New("C2 Error")
|
||||
}
|
||||
@@ -179,9 +178,7 @@ func (nc *NetConnection) complex_handshake(C1 []byte) error {
|
||||
|
||||
buffer := net.Buffers{[]byte{RTMP_HANDSHAKE_VERSION}, S1, S2_Random, S2_Digest}
|
||||
_, err = buffer.WriteTo(nc)
|
||||
b, _ := nc.ReadBytes(1536)
|
||||
b.Recycle()
|
||||
return err
|
||||
return nc.Skip(1536)
|
||||
}
|
||||
|
||||
func validateClient(C1 []byte) (scheme int, challenge []byte, digest []byte, ok bool, err error) {
|
||||
|
||||
@@ -146,16 +146,13 @@ func (conn *NetConnection) readChunk() (msg *Chunk, err error) {
|
||||
if msgLen == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
var mem util.RecyclableMemory
|
||||
var bufSize = 0
|
||||
if unRead := msgLen - chunk.bufLen; unRead < conn.readChunkSize {
|
||||
mem, err = conn.ReadBytes(unRead)
|
||||
bufSize = unRead
|
||||
} else {
|
||||
mem, err = conn.ReadBytes(conn.readChunkSize)
|
||||
bufSize = conn.readChunkSize
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn.readSeqNum += uint32(mem.Size)
|
||||
conn.readSeqNum += uint32(bufSize)
|
||||
if chunk.bufLen == 0 {
|
||||
chunk.AVData.RecyclableMemory = util.RecyclableMemory{
|
||||
ScalableMemoryAllocator: conn.mediaDataPool.ScalableMemoryAllocator,
|
||||
@@ -163,9 +160,12 @@ func (conn *NetConnection) readChunk() (msg *Chunk, err error) {
|
||||
chunk.AVData.NextN(msgLen)
|
||||
}
|
||||
buffer := chunk.AVData.Buffers[0]
|
||||
for _, b := range mem.Buffers {
|
||||
copy(buffer[chunk.bufLen:], b)
|
||||
chunk.bufLen += len(b)
|
||||
for buf := range conn.ReadRange(bufSize) {
|
||||
copy(buffer[chunk.bufLen:], buf)
|
||||
chunk.bufLen += len(buf)
|
||||
}
|
||||
if conn.Err != nil {
|
||||
return nil, conn.Err
|
||||
}
|
||||
if chunk.bufLen == msgLen {
|
||||
msg = chunk
|
||||
@@ -332,7 +332,9 @@ func (conn *NetConnection) sendChunk(data net.Buffers, head *ChunkHeader, headTy
|
||||
head.WriteTo(RTMP_CHUNK_HEAD_1, &chunk3)
|
||||
r := util.NewReadableBuffersFromBytes(data...)
|
||||
for {
|
||||
r.WriteNTo(conn.WriteChunkSize, &chunks)
|
||||
for buf := range r.RangeN(conn.WriteChunkSize) {
|
||||
chunks = append(chunks, buf)
|
||||
}
|
||||
if r.Length <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (avcc *RTMPVideo) Parse(t *AVTrack) (isIDR, isSeq bool, raw any, err error)
|
||||
isSeq = true
|
||||
isIDR = false
|
||||
var cloneFrame RTMPVideo
|
||||
cloneFrame.CopyFrom(avcc.Memory)
|
||||
cloneFrame.CopyFrom(&avcc.Memory)
|
||||
switch fourCC {
|
||||
case codec.FourCC_H264:
|
||||
var ctx H264Ctx
|
||||
@@ -125,7 +125,7 @@ func (avcc *RTMPVideo) DecodeConfig(t *AVTrack, from ICodecCtx) (err error) {
|
||||
b.Write(h264ctx.PPS[0])
|
||||
t.ICodecCtx = &ctx
|
||||
var seqFrame RTMPData
|
||||
seqFrame.ReadFromBytes(b)
|
||||
seqFrame.Append(b)
|
||||
t.SequenceFrame = seqFrame.WrapVideo()
|
||||
if t.Enabled(context.TODO(), TraceLevel) {
|
||||
c := t.FourCC().String()
|
||||
@@ -241,7 +241,7 @@ func createH26xFrame(from *AVFrame, codecID VideoCodecID) (frame IAVFrame, err e
|
||||
naluLenM := rtmpVideo.NextN(4)
|
||||
naluLen := uint32(util.LenOfBuffers(nalu))
|
||||
binary.BigEndian.PutUint32(naluLenM, naluLen)
|
||||
rtmpVideo.ReadFromBytes(nalu...)
|
||||
rtmpVideo.Append(nalu...)
|
||||
}
|
||||
frame = &rtmpVideo
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user