From 9c1c5cb60113ddfd555a83c8e6768a8a95c1e867 Mon Sep 17 00:00:00 2001 From: Jake Lane Date: Fri, 20 Jul 2018 10:23:40 +0930 Subject: [PATCH] rtmp: Implemented afmEncodeInt24 --- rtmp/rtmp.go | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 6c0c9894..76a8adf5 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -793,12 +793,11 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { if t > 0xffffff { res = 0xffffff } - hptr = unsafe.Pointer(C.AMF_EncodeInt24((*C.char)(hptr), (*C.char)(hend), C.int(res))) + hptr = unsafe.Pointer(afmEncodeInt24((*byte)(hptr), (*byte)(hend), res)) } if nSize > 4 { - hptr = unsafe.Pointer(C.AMF_EncodeInt24((*C.char)(hptr), (*C.char)(hend), - C.int(packet.m_nBodySize))) + hptr = unsafe.Pointer(afmEncodeInt24((*byte)(hptr), (*byte)(hend), (int32(packet.m_nBodySize)))) *(*byte)(hptr) = byte(packet.m_packetType) hptr = incBytePtr(hptr, 1) } @@ -962,6 +961,29 @@ func afmDecodeInt16(data *byte) uint16 { return (uint16(*(*uint8)(c)) << 8) | *(*uint16)(incBytePtr(c, 1)) } +// afmEncodeInt24 encodes a int24 into data +func afmEncodeInt24(output *byte, outend *byte, nVal int32) *byte { + outputPtr := unsafe.Pointer(output) + outendPtr := unsafe.Pointer(outend) + if uintptr(outputPtr)+3 > uintptr(outendPtr) { + // length < 3 + return nil + } + + // Assign output[2] + third := (*byte)(incBytePtr(outputPtr, 2)) + *third = (byte)(nVal & 0xff) + + // Assign output[1] + second := (*byte)(incBytePtr(outputPtr, 1)) + *second = (byte)(nVal >> 8) + + // Assign output[0] + *output = (byte)(nVal >> 16) + + return (*byte)(incBytePtr(outputPtr, 3)) +} + func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int { ptr := buffer for n > 0 {