From 9c72a93763d207d8322970bba23fb46e3ecd7919 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Wed, 26 Sep 2018 16:01:16 +0930 Subject: [PATCH] rtmp: replace first half of C_RTMP_SendPacket pointer walk with index walk --- rtmp/rtmp.go | 61 ++++++++++++++++++++++++++++++++++------------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index ab73169d..3428e848 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -57,6 +57,19 @@ func pp2b(b, e *byte) []byte { return (*[_Gi]byte)(base)[:len] } +func b2pp(buf []byte) (b, e *byte) { + if buf == nil { + return nil, nil + } + if len(buf) == 0 { + b = *(**byte)(unsafe.Pointer(&buf)) + return b, b + } + b = &buf[0] + e = (*byte)(unsafe.Pointer(uintptr(unsafe.Pointer(b)) + uintptr(len(buf)))) + return b, e +} + func pl2b(b *byte, l int) []byte { if b == nil { return nil @@ -1338,16 +1351,17 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { return false } - var header, hend *byte + var headBytes []byte + var origIdx int if packet.m_body != nil { // Span from -packetsize for the type to the start of the body. - header = decBytePtr(&packet.m_body[0], packetSize[packet.m_headerType]) - hend = &packet.m_body[0] + headBytes = packet.m_header[:RTMP_MAX_HEADER_SIZE] + origIdx = RTMP_MAX_HEADER_SIZE - packetSize[packet.m_headerType] } else { // Allocate a new header and allow 6 bytes of movement backward. var hbuf [RTMP_MAX_HEADER_SIZE]byte - header = incBytePtr(&hbuf[0], 6) - hend = incBytePtr(&hbuf[0], RTMP_MAX_HEADER_SIZE) + headBytes = hbuf[:] + origIdx = 6 } var cSize int @@ -1360,20 +1374,20 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { hSize := packetSize[packet.m_headerType] if cSize != 0 { - header = decBytePtr(header, cSize) + origIdx -= cSize hSize += cSize } t := uint32(int(packet.m_nTimeStamp) - last) if t >= 0xffffff { - header = decBytePtr(header, 4) + origIdx -= 4 hSize += 4 log.Printf("Larger timestamp than 24-bit: 0x%v", t) } - hptr := header - c := packet.m_headerType << 6 + headerIdx := origIdx + c := packet.m_headerType << 6 switch cSize { case 0: c |= byte(packet.m_nChannel) @@ -1382,17 +1396,17 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { case 2: c |= 1 } - *hptr = c - hptr = incBytePtr(hptr, 1) + headBytes[headerIdx] = c + headerIdx++ if cSize != 0 { tmp := packet.m_nChannel - 64 - *hptr = byte(tmp & 0xff) - hptr = incBytePtr(hptr, 1) + headBytes[headerIdx] = byte(tmp & 0xff) + headerIdx++ if cSize == 2 { - *hptr = byte(tmp >> 8) - hptr = incBytePtr(hptr, 1) + headBytes[headerIdx] = byte(tmp >> 8) + headerIdx++ } } @@ -1401,21 +1415,25 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { if t > 0xffffff { res = 0xffffff } - hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(res))) + C_AMF_EncodeInt24(headBytes[headerIdx:], int32(res)) + headerIdx += 3 // 24bits } if packetSize[packet.m_headerType] > 4 { - hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(packet.m_nBodySize))) - *hptr = packet.m_packetType - hptr = incBytePtr(hptr, 1) + C_AMF_EncodeInt24(headBytes[headerIdx:], int32(packet.m_nBodySize)) + headerIdx += 3 // 24bits + headBytes[headerIdx] = packet.m_packetType + headerIdx++ } if packetSize[packet.m_headerType] > 8 { - hptr = incBytePtr(hptr, int(C_EncodeInt32LE(pl2b(hptr, 4), packet.m_nInfoField2))) + n := int(C_EncodeInt32LE(headBytes[headerIdx:headerIdx+4], packet.m_nInfoField2)) + headerIdx += n } if t >= 0xffffff { - hptr = bAddr(C_AMF_EncodeInt32(pp2b(hptr, hend), int32(t))) + C_AMF_EncodeInt32(headBytes[headerIdx:], int32(t)) + headerIdx += 4 // 32bits } nSize := int(packet.m_nBodySize) @@ -1426,6 +1444,7 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) { log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize) } + header := bAddr(headBytes[origIdx:]) for (nSize + hSize) != 0 { if nSize < nChunkSize { nChunkSize = nSize