rtmp: replace first half of C_RTMP_SendPacket pointer walk with index walk

This commit is contained in:
Dan Kortschak 2018-09-26 16:01:16 +09:30
parent d101946cfe
commit 9c72a93763
1 changed files with 40 additions and 21 deletions

View File

@ -57,6 +57,19 @@ func pp2b(b, e *byte) []byte {
return (*[_Gi]byte)(base)[:len] 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 { func pl2b(b *byte, l int) []byte {
if b == nil { if b == nil {
return nil return nil
@ -1338,16 +1351,17 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
return false return false
} }
var header, hend *byte var headBytes []byte
var origIdx int
if packet.m_body != nil { if packet.m_body != nil {
// Span from -packetsize for the type to the start of the body. // Span from -packetsize for the type to the start of the body.
header = decBytePtr(&packet.m_body[0], packetSize[packet.m_headerType]) headBytes = packet.m_header[:RTMP_MAX_HEADER_SIZE]
hend = &packet.m_body[0] origIdx = RTMP_MAX_HEADER_SIZE - packetSize[packet.m_headerType]
} else { } else {
// Allocate a new header and allow 6 bytes of movement backward. // Allocate a new header and allow 6 bytes of movement backward.
var hbuf [RTMP_MAX_HEADER_SIZE]byte var hbuf [RTMP_MAX_HEADER_SIZE]byte
header = incBytePtr(&hbuf[0], 6) headBytes = hbuf[:]
hend = incBytePtr(&hbuf[0], RTMP_MAX_HEADER_SIZE) origIdx = 6
} }
var cSize int 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] hSize := packetSize[packet.m_headerType]
if cSize != 0 { if cSize != 0 {
header = decBytePtr(header, cSize) origIdx -= cSize
hSize += cSize hSize += cSize
} }
t := uint32(int(packet.m_nTimeStamp) - last) t := uint32(int(packet.m_nTimeStamp) - last)
if t >= 0xffffff { if t >= 0xffffff {
header = decBytePtr(header, 4) origIdx -= 4
hSize += 4 hSize += 4
log.Printf("Larger timestamp than 24-bit: 0x%v", t) log.Printf("Larger timestamp than 24-bit: 0x%v", t)
} }
hptr := header headerIdx := origIdx
c := packet.m_headerType << 6
c := packet.m_headerType << 6
switch cSize { switch cSize {
case 0: case 0:
c |= byte(packet.m_nChannel) 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: case 2:
c |= 1 c |= 1
} }
*hptr = c headBytes[headerIdx] = c
hptr = incBytePtr(hptr, 1) headerIdx++
if cSize != 0 { if cSize != 0 {
tmp := packet.m_nChannel - 64 tmp := packet.m_nChannel - 64
*hptr = byte(tmp & 0xff) headBytes[headerIdx] = byte(tmp & 0xff)
hptr = incBytePtr(hptr, 1) headerIdx++
if cSize == 2 { if cSize == 2 {
*hptr = byte(tmp >> 8) headBytes[headerIdx] = byte(tmp >> 8)
hptr = incBytePtr(hptr, 1) headerIdx++
} }
} }
@ -1401,21 +1415,25 @@ func C_RTMP_SendPacket(r *C_RTMP, packet *C_RTMPPacket, queue int) (ok bool) {
if t > 0xffffff { if t > 0xffffff {
res = 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 { if packetSize[packet.m_headerType] > 4 {
hptr = bAddr(C_AMF_EncodeInt24(pp2b(hptr, hend), int32(packet.m_nBodySize))) C_AMF_EncodeInt24(headBytes[headerIdx:], int32(packet.m_nBodySize))
*hptr = packet.m_packetType headerIdx += 3 // 24bits
hptr = incBytePtr(hptr, 1) headBytes[headerIdx] = packet.m_packetType
headerIdx++
} }
if packetSize[packet.m_headerType] > 8 { 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 { 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) 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) log.Printf("C_RTMP_SendPacket: fd=%v, size=%v", r.m_sb.sb_socket, nSize)
} }
header := bAddr(headBytes[origIdx:])
for (nSize + hSize) != 0 { for (nSize + hSize) != 0 {
if nSize < nChunkSize { if nSize < nChunkSize {
nChunkSize = nSize nChunkSize = nSize