From b18ae2913234c154e0fc057d722088bc73aff608 Mon Sep 17 00:00:00 2001 From: Jake Lane Date: Thu, 19 Jul 2018 16:56:41 +0930 Subject: [PATCH] rtmp: Implement afmEncodeInt16 --- rtmp/rtmp.go | 63 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 90954404..6850aa2c 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -509,16 +509,14 @@ func afmEncodeString(output *byte, outend *byte, bv *C.AVal) *byte { *(*C.char)(outputPtr) = C.AMF_STRING incBytePtr(outputPtr, 1) - // TODO Encode Int16 - outputPtr = unsafe.Pointer(C.AMF_EncodeInt16((*C.char)(outputPtr), - (*C.char)(outendPtr), (C.short)(bv.av_len))) + outputPtr = unsafe.Pointer(afmEncodeInt16((*byte)(outputPtr), + (*byte)(outendPtr), (int16)(bv.av_len))) } else { *(*C.char)(outputPtr) = C.AMF_LONG_STRING incBytePtr(outputPtr, 1) - // TODO Encode Int16 - outputPtr = unsafe.Pointer(C.AMF_EncodeInt32((*C.char)(outputPtr), - (*C.char)(outendPtr), bv.av_len)) + outputPtr = unsafe.Pointer(afmEncodeInt32((*byte)(outputPtr), + (*byte)(outendPtr), (int32)(bv.av_len))) } memmove(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), uintptr(bv.av_len)) //C.memcpy(unsafe.Pointer(outputPtr), unsafe.Pointer(bv.av_val), (C.size_t)(bv.av_len)) @@ -527,6 +525,52 @@ func afmEncodeString(output *byte, outend *byte, bv *C.AVal) *byte { return (*byte)(outputPtr) } +// afmEncodeInt16 encodes a int16 into data +func afmEncodeInt16(output *byte, outend *byte, nVal int16) *byte { + outputPtr := unsafe.Pointer(output) + outendPtr := unsafe.Pointer(outend) + if uintptr(outputPtr)+2 > uintptr(outendPtr) { + // length < 2 + return nil + } + + // Assign output[1] + second := (*byte)(incBytePtr(outputPtr, 1)) + *second = (byte)(nVal & 0xff) + + // Assign output[0] + *output = (byte)(nVal >> 8) + + return (*byte)(incBytePtr(outputPtr, 2)) +} + +// afmEncodeInt32 encodes a int32 into data +func afmEncodeInt32(output *byte, outend *byte, nVal int32) *byte { + outputPtr := unsafe.Pointer(output) + outendPtr := unsafe.Pointer(outend) + if uintptr(outputPtr)+4 > uintptr(outendPtr) { + // length < 4 + return nil + } + + // Assign output[3] + forth := (*byte)(incBytePtr(outputPtr, 3)) + *forth = (byte)(nVal & 0xff) + + // Assign output[2] + third := (*byte)(incBytePtr(outputPtr, 2)) + *third = (byte)(nVal >> 8) + + // Assign output[1] + second := (*byte)(incBytePtr(outputPtr, 1)) + *second = (byte)(nVal >> 16) + + // Assign output[0] + *output = (byte)(nVal >> 24) + + return (*byte)(incBytePtr(outputPtr, 4)) +} + // send packet version 1 - less C stuff func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { var prevPacket *C.RTMPPacket @@ -662,7 +706,7 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { } if t >= 0xffffff { - hptr = unsafe.Pointer(C.AMF_EncodeInt32((*C.char)(hptr), (*C.char)(hend), C.int(t))) + hptr = unsafe.Pointer(afmEncodeInt32((*byte)(hptr), (*byte)(hend), (int32)(t))) } nSize = int(packet.m_nBodySize) @@ -739,9 +783,8 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int { } if t >= 0xffffff { extendedTimestamp := incBytePtr(header, 1+cSize) - // TODO: port this - C.AMF_EncodeInt32((*C.char)(extendedTimestamp), - (*C.char)(incBytePtr(extendedTimestamp, 4)), C.int(t)) + afmEncodeInt32((*byte)(extendedTimestamp), + (*byte)(incBytePtr(extendedTimestamp, 4)), (int32)(t)) } } }