diff --git a/rtmp/packet.go b/rtmp/packet.go index 0f3ae241..a92f7022 100644 --- a/rtmp/packet.go +++ b/rtmp/packet.go @@ -199,9 +199,7 @@ func (pkt *packet) readFrom(c *Conn) error { hSize += 4 } - if pkt.bodySize > 0 && pkt.body == nil { - pkt.resize(pkt.bodySize, (hbuf[0]&0xc0)>>6) - } + pkt.resize(pkt.bodySize, pkt.headerType) if pkt.bodySize > c.inChunkSize { c.log(WarnLevel, pkg+"reading large packet", "size", int(pkt.bodySize)) @@ -234,10 +232,12 @@ func (pkt *packet) readFrom(c *Conn) error { return nil } -// resize adjusts the packet's storage to accommodate a body of the given size and header type. +// resize adjusts the packet's storage (if necessary) to accommodate a body of the given size and header type. // When headerSizeAuto is specified, the header type is computed based on packet type. func (pkt *packet) resize(size uint32, ht uint8) { - pkt.buf = make([]byte, fullHeaderSize+size) + if cap(pkt.buf) < fullHeaderSize+int(size) { + pkt.buf = make([]byte, fullHeaderSize+size) + } pkt.body = pkt.buf[fullHeaderSize:] if ht != headerSizeAuto { pkt.headerType = ht