rtmp: make C_AMF_EncodeNumber take []byte

This commit is contained in:
Dan Kortschak 2018-09-15 06:36:48 +09:30
parent 00b4e167f4
commit 458eb932aa
2 changed files with 17 additions and 25 deletions

View File

@ -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:

View File

@ -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))