From 458eb932aa3c61a214b81dd0d42642bad6ca0234 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 15 Sep 2018 06:36:48 +0930 Subject: [PATCH] rtmp: make C_AMF_EncodeNumber take []byte --- rtmp/amf.go | 24 ++++++++---------------- rtmp/rtmp.go | 18 +++++++++--------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 52fdfe1b..2350d699 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -172,22 +172,14 @@ func C_AMF_EncodeString(dst []byte, val string) *byte { // char* AMF_EncodeNumber(char* output, char* outend, double dVal); // amf.c +199 -func C_AMF_EncodeNumber(output *byte, outend *byte, dVal float64) *byte { - if int(uintptr(unsafe.Pointer(output)))+1+8 > int(uintptr(unsafe.Pointer(outend))) { +func C_AMF_EncodeNumber(dst []byte, val float64) *byte { + if len(dst) < 9 { return nil } - // TODO: port this - *(*byte)(unsafe.Pointer(output)) = AMF_NUMBER - output = (*byte)(incBytePtr(unsafe.Pointer(output), 1)) - // NOTE: here we are assuming little endian for both byte order and float - // word order - var ci, co *uint8 - ci = (*uint8)(unsafe.Pointer(&dVal)) - co = (*uint8)(unsafe.Pointer(output)) - for i := 0; i < 8; i++ { - (*[_Gi]byte)(unsafe.Pointer(co))[i] = (*[_Gi]byte)(unsafe.Pointer(ci))[7-i] - } - return (*byte)(incBytePtr(unsafe.Pointer(output), 8)) + dst[0] = AMF_NUMBER + dst = dst[1:] + binary.BigEndian.PutUint64(dst, math.Float64bits(val)) + return &dst[8:][0] } // char* AMF_EncodeBoolean(char* output, char* outend, int bVal); @@ -232,7 +224,7 @@ func C_AMF_EncodeNamedNumber(output *byte, outend *byte, key string, val float64 binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) copy(dst[2:], key) output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) - return C_AMF_EncodeNumber(output, outend, val) + return C_AMF_EncodeNumber(pp2b(output, outend), val) } // char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal); @@ -303,7 +295,7 @@ func C_AMF_PropEncode(p *C_AMFObjectProperty, pBuffer *byte, pBufEnd *byte) *byt switch p.p_type { case AMF_NUMBER: - pBuffer = C_AMF_EncodeNumber(pBuffer, pBufEnd, p.p_vu.p_number) + pBuffer = C_AMF_EncodeNumber(pp2b(pBuffer, pBufEnd), p.p_vu.p_number) case AMF_BOOLEAN: pBuffer = C_AMF_EncodeBoolean(pp2b(pBuffer, pBufEnd), p.p_vu.p_number != 0) case AMF_STRING: diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index ee24420e..6adf4caf 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -673,7 +673,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = C_AMF_EncodeString(pp2b(enc, pend), av_connect) r.m_numInvokes += 1 - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) (*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT @@ -808,7 +808,7 @@ func C_RTMP_SendCreateStream(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_createStream) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) @@ -838,7 +838,7 @@ func C_SendReleaseStream(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_releaseStream) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(pp2b(enc, pend), r.Link.playpath) @@ -871,7 +871,7 @@ func C_SendFCPublish(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_FCPublish) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(pp2b(enc, pend), r.Link.playpath) @@ -904,7 +904,7 @@ func C_SendFCUnpublish(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_FCUnpublish) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(pp2b(enc, pend), r.Link.playpath) @@ -939,7 +939,7 @@ func C_SendPublish(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_publish) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeString(pp2b(enc, pend), r.Link.playpath) @@ -979,10 +979,10 @@ func C_SendDeleteStream(r *C_RTMP, dStreamId float64) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av_deleteStream) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeNumber(enc, pend, dStreamId) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), dStreamId) packet.m_nBodySize = uint32(uintptr(unsafe.Pointer(enc)) - uintptr( unsafe.Pointer(packet.m_body))) @@ -1035,7 +1035,7 @@ func C_SendCheckBW(r *C_RTMP) (ok bool) { enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(pp2b(enc, pend), av__checkbw) r.m_numInvokes++ - enc = C_AMF_EncodeNumber(enc, pend, float64(r.m_numInvokes)) + enc = C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes)) *enc = AMF_NULL enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1))