From a00550b63862de26ae4d0d8d71172845ba1731d5 Mon Sep 17 00:00:00 2001 From: Jake Lane Date: Wed, 18 Jul 2018 13:27:40 +0930 Subject: [PATCH] rtmp: Implemented afmDecodeInt24 and afmDecodeString --- rtmp/rtmp.go | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 1d6bcd67..015f9d60 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -8,6 +8,7 @@ DESCRIPTION AUTHOR Saxon Nelson-Milton Dan Kortschak + Jake Lane LICENSE rtmp.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean) @@ -199,11 +200,9 @@ func rtmpWrite(r *C.RTMP, data []byte) int { pkt.m_packetType = C.uint8_t(*indxBytePtr(buf, 0)) buf = incBytePtr(buf, 1) - // TODO: port this - pkt.m_nBodySize = C.uint32_t(C.AMF_DecodeInt24((*C.char)(buf))) + pkt.m_nBodySize = C.uint32_t(afmDecodeInt24((*byte)(buf))) buf = incBytePtr(buf, 3) - // TODO: replace with ported version - pkt.m_nTimeStamp = C.uint32_t(C.AMF_DecodeInt24((*C.char)(buf))) + pkt.m_nTimeStamp = C.uint32_t(afmDecodeInt24((*byte)(buf))) buf = incBytePtr(buf, 3) pkt.m_nTimeStamp |= C.uint32_t(*indxBytePtr(buf, 0)) << 24 buf = incBytePtr(buf, 4) @@ -231,7 +230,6 @@ func rtmpWrite(r *C.RTMP, data []byte) int { pend = incBytePtr(enc, int(pkt.m_nBodySize)) if pkt.m_packetType == RTMP_PACKET_TYPE_INFO { - // TODO: Port this enc = unsafe.Pointer(afmEncodeString((*byte)(enc), (*byte)(pend), &setDataFrame)) pkt.m_nBytesRead = C.uint32_t(math.Abs(float64(uintptr(enc) - uintptr(unsafe.Pointer(pkt.m_body))))) @@ -269,6 +267,13 @@ func rtmpWrite(r *C.RTMP, data []byte) int { return size + s2 } +// afmDecodeInt24 decodes data into an unsigned int +func afmDecodeInt24(data *byte) uint32 { + dataPtr := unsafe.Pointer(data) + + return (uint32)(*data)<<16 | *(*uint32)(incBytePtr(dataPtr, 1))<<8 | *(*uint32)(incBytePtr(dataPtr, 2)) +} + func afmEncodeString(output *byte, outend *byte, bv *C.AVal) *byte { outputPtr := unsafe.Pointer(output) outendPtr := unsafe.Pointer(outend) @@ -533,8 +538,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { var method C.AVal var ptr unsafe.Pointer ptr = incBytePtr(unsafe.Pointer(packet.m_body), 1) - // TODO: port this - C.AMF_DecodeString((*C.char)(ptr), &method) + afmDecodeString((*byte)(ptr), &method) if debugMode { log.Printf("Invoking %v", method.av_val) @@ -566,6 +570,18 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { return 1 } +// afmDecodeString decodes data into a string inside a AVal +func afmDecodeString(data *byte, bv *C.AVal) { + dataPtr := unsafe.Pointer(data) + bv.av_len = C.int(C.AMF_DecodeInt16((*C.char)(dataPtr))) + + if bv.av_len > 0 { + bv.av_val = (*C.char)(incBytePtr(dataPtr, 2)) + } else { + bv.av_val = nil + } +} + func writeN(r *C.RTMP, buffer unsafe.Pointer, n int) int { ptr := buffer for n > 0 {