rtmp: make C_AMF_EncodeNamed{Boolean,Number,String} take []byte

This commit is contained in:
Dan Kortschak 2018-09-15 09:14:27 +09:30
parent 458eb932aa
commit 52f34e4a85
2 changed files with 36 additions and 33 deletions

View File

@ -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); // char* AMF_EncodeNamedString(char* output, char* outend, const C_AVal* strName, const C_AVal* strValue);
// amf.c +273 // amf.c +273
func C_AMF_EncodeNamedString(output *byte, outend *byte, key, val string) *byte { func C_AMF_EncodeNamedString(dst []byte, key, val string) *byte {
buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) if 2+len(key) > len(dst) {
if 2+len(key) > buflen {
return nil return nil
} }
dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen]
binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
copy(dst[2:], key) dst = dst[2:]
output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) copy(dst, key)
return C_AMF_EncodeString(pp2b(output, outend), val) 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); // char* AMF_EncodeNamedNumber(char* output, char* outend, const C_AVal* strName, double dVal);
// amf.c +286 // amf.c +286
func C_AMF_EncodeNamedNumber(output *byte, outend *byte, key string, val float64) *byte { func C_AMF_EncodeNamedNumber(dst []byte, key string, val float64) *byte {
buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) if 2+len(key) > len(dst) {
if 2+len(key) > buflen {
return nil return nil
} }
dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen]
binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
copy(dst[2:], key) dst = dst[2:]
output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) copy(dst, key)
return C_AMF_EncodeNumber(pp2b(output, outend), val) 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); // char* AMF_EncodeNamedBoolean(char* output, char* outend, const C_AVal* strname, int bVal);
// amf.c +299 // amf.c +299
func C_AMF_EncodeNamedBoolean(output *byte, outend *byte, key string, val bool) *byte { func C_AMF_EncodeNamedBoolean(dst []byte, key string, val bool) *byte {
buflen := int(uintptr(unsafe.Pointer(outend)) - uintptr(unsafe.Pointer(output))) if 2+len(key) > len(dst) {
if 2+len(key) > buflen {
return nil return nil
} }
dst := (*[_Gi]byte)(unsafe.Pointer(output))[:buflen]
binary.BigEndian.PutUint16(dst[:2], uint16(len(key))) binary.BigEndian.PutUint16(dst[:2], uint16(len(key)))
copy(dst[2:], key) dst = dst[2:]
output = (*byte)(incBytePtr(unsafe.Pointer(output), 2+len(key))) copy(dst, key)
return C_AMF_EncodeBoolean(pp2b(output, outend), val) if len(key) == len(dst) {
return nil
}
return C_AMF_EncodeBoolean(dst[len(key):], val)
} }
// void AMFProp_SetName(AMFObjectProperty *prop, AVal *name); // void AMFProp_SetName(AMFObjectProperty *prop, AVal *name);

View File

@ -679,60 +679,60 @@ func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) 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 { if enc == nil {
return false return false
} }
if r.Link.protocol&RTMP_FEATURE_WRITE != 0 { 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 { if enc == nil {
return false return false
} }
} }
if r.Link.flashVer != "" { 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 { if enc == nil {
return false return false
} }
} }
if r.Link.swfUrl != "" { 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 { if enc == nil {
return false return false
} }
} }
if r.Link.tcUrl != "" { 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 { if enc == nil {
return false return false
} }
} }
if r.Link.protocol&RTMP_FEATURE_WRITE == 0 { 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 { if enc == nil {
return false return false
} }
enc = C_AMF_EncodeNamedNumber(enc, pend, av_capabilities, 15) enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_capabilities, 15)
if enc == nil { if enc == nil {
return false 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 { if enc == nil {
return false 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 { if enc == nil {
return false return false
} }
enc = C_AMF_EncodeNamedNumber(enc, pend, av_videoFunction, 1) enc = C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_videoFunction, 1)
if enc == nil { if enc == nil {
return false return false
} }
if r.Link.pageUrl != "" { 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 { if enc == nil {
return false 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 { 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 { if enc == nil {
return false return false
} }