From 52f34e4a8511d05bf993dad804f395937289f838 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Sat, 15 Sep 2018 09:14:27 +0930 Subject: [PATCH] rtmp: make C_AMF_EncodeNamed{Boolean,Number,String} take []byte --- rtmp/amf.go | 45 ++++++++++++++++++++++++--------------------- rtmp/rtmp.go | 24 ++++++++++++------------ 2 files changed, 36 insertions(+), 33 deletions(-) diff --git a/rtmp/amf.go b/rtmp/amf.go index 2350d699..19c9dbd5 100644 --- a/rtmp/amf.go +++ b/rtmp/amf.go @@ -201,44 +201,47 @@ func C_AMF_EncodeBoolean(dst []byte, val bool) *byte { // char* AMF_EncodeNamedString(char* output, char* outend, const C_AVal* strName, const C_AVal* strValue); // amf.c +273 -func C_AMF_EncodeNamedString(output *byte, outend *byte, key, val string) *byte { - buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) - if 2+len(key) > buflen { +func C_AMF_EncodeNamedString(dst []byte, key, val string) *byte { + if 2+len(key) > len(dst) { return nil } - dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) - copy(dst[2:], key) - output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) - return C_AMF_EncodeString(pp2b(output, outend), val) + dst = dst[2:] + copy(dst, key) + if len(key) == len(dst) { + return nil + } + return C_AMF_EncodeString(dst[len(key):], val) } // char* AMF_EncodeNamedNumber(char* output, char* outend, const C_AVal* strName, double dVal); // amf.c +286 -func C_AMF_EncodeNamedNumber(output *byte, outend *byte, key string, val float64) *byte { - buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) - if 2+len(key) > buflen { +func C_AMF_EncodeNamedNumber(dst []byte, key string, val float64) *byte { + if 2+len(key) > len(dst) { return nil } - dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] 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(pp2b(output, outend), val) + dst = dst[2:] + copy(dst, key) + if len(key) == len(dst) { + return nil + } + return C_AMF_EncodeNumber(dst[len(key):], val) } // char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal); // amf.c +299 -func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val bool) *byte { - buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) - if 2+len(key) > buflen { +func C_AMF_EncodeNamedBoolean(dst []byte, key string, val bool) *byte { + if 2+len(key) > len(dst) { return nil } - dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen] binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) - copy(dst[2:], key) - output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) - return C_AMF_EncodeBoolean(pp2b(output, outend), val) + dst = dst[2:] + copy(dst, key) + if len(key) == len(dst) { + return nil + } + return C_AMF_EncodeBoolean(dst[len(key):], val) } // void AMFProp_SetName(AMFObjectProperty *prop, AVal *name); diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 6adf4caf..3a8a2def 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -679,60 +679,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) - enc = C_AMF_EncodeNamedString(enc, pend, av_app, r.Link.app) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_app, r.Link.app) if enc == nil { return false } if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { - enc = C_AMF_EncodeNamedString(enc, pend, av_type, av_nonprivate) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_type, av_nonprivate) if enc == nil { return false } } if r.Link.flashVer != "" { - enc = C_AMF_EncodeNamedString(enc, pend, av_flashVer, r.Link.flashVer) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_flashVer, r.Link.flashVer) if enc == nil { return false } } if r.Link.swfUrl != "" { - enc = C_AMF_EncodeNamedString(enc, pend, av_swfUrl, r.Link.swfUrl) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_swfUrl, r.Link.swfUrl) if enc == nil { return false } } if r.Link.tcUrl != "" { - enc = C_AMF_EncodeNamedString(enc, pend, av_tcUrl, r.Link.tcUrl) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_tcUrl, r.Link.tcUrl) if enc == nil { return false } } if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { - enc = C_AMF_EncodeNamedBoolean(enc, pend, av_fpad, false) + enc = C_AMF_EncodeNamedBoolean(pp2b(enc, pend), av_fpad, false) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, av_capabilities, 15) + enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_capabilities, 15) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, av_audioCodecs, r.m_fAudioCodecs) + enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_audioCodecs, r.m_fAudioCodecs) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoCodecs, r.m_fVideoCodecs) + enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_videoCodecs, r.m_fVideoCodecs) if enc == nil { return false } - enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoFunction, 1) + enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_videoFunction, 1) if enc == nil { return false } if r.Link.pageUrl != "" { - enc = C_AMF_EncodeNamedString(enc, pend, av_pageUrl, r.Link.pageUrl) + enc = C_AMF_EncodeNamedString(pp2b(enc, pend), av_pageUrl, r.Link.pageUrl) if enc == nil { return false } @@ -740,7 +740,7 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } if r.m_fEncoding != 0.0 || r.m_bSendEncoding { - enc = C_AMF_EncodeNamedNumber(enc, pend, av_objectEncoding, r.m_fEncoding) + enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_objectEncoding, r.m_fEncoding) if enc == nil { return false }