rtmp: use []byte for encoding in C_SendConnectPacket

This commit is contained in:
Dan Kortschak 2018-09-19 12:36:39 +09:30
parent 55e454453b
commit eccc9377ab
1 changed files with 33 additions and 45 deletions

View File

@ -655,88 +655,84 @@ func C_WriteN(r *C_RTMP, buf []byte) (ok bool) {
// int SendConnectPacket(RTMP* r, RTMPPacket* cp); // int SendConnectPacket(RTMP* r, RTMPPacket* cp);
// rtmp.c +1579 // rtmp.c +1579
func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { func C_SendConnectPacket(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
var packet C_RTMPPacket
var pbuf [4096]byte
pend := (*byte)(incBytePtr(unsafe.Pointer(&pbuf[0]), int(unsafe.Sizeof(pbuf))))
var enc *byte
if cp != nil { if cp != nil {
return C_RTMP_SendPacket(r, cp, 1) return C_RTMP_SendPacket(r, cp, 1)
} }
packet.m_nChannel = 0x03 var pbuf [4096]byte
packet.m_headerType = RTMP_PACKET_SIZE_LARGE packet := C_RTMPPacket{
packet.m_packetType = RTMP_PACKET_TYPE_INVOKE m_nChannel: 0x03,
packet.m_nTimeStamp = 0 m_headerType: RTMP_PACKET_SIZE_LARGE,
packet.m_nInfoField2 = 0 m_packetType: RTMP_PACKET_TYPE_INVOKE,
packet.m_hasAbsTimestamp = false m_nTimeStamp: 0,
packet.m_body = &pbuf[RTMP_MAX_HEADER_SIZE] m_nInfoField2: 0,
m_hasAbsTimestamp: false,
m_body: &pbuf[RTMP_MAX_HEADER_SIZE],
}
enc := pbuf[RTMP_MAX_HEADER_SIZE:]
enc = (*byte)(unsafe.Pointer(packet.m_body)) enc = C_AMF_EncodeString(enc, av_connect)
enc = bAddr(C_AMF_EncodeString(pp2b(enc, pend), av_connect))
r.m_numInvokes += 1 r.m_numInvokes += 1
enc = bAddr(C_AMF_EncodeNumber(pp2b(enc, pend), float64(r.m_numInvokes))) enc = C_AMF_EncodeNumber(enc, float64(r.m_numInvokes))
(*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT enc[0] = AMF_OBJECT
enc = enc[1:]
enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1)) enc = C_AMF_EncodeNamedString(enc, av_app, r.Link.app)
enc = bAddr(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 = bAddr(C_AMF_EncodeNamedString(pp2b(enc, pend), av_type, av_nonprivate)) enc = C_AMF_EncodeNamedString(enc, av_type, av_nonprivate)
if enc == nil { if enc == nil {
return false return false
} }
} }
if r.Link.flashVer != "" { if r.Link.flashVer != "" {
enc = bAddr(C_AMF_EncodeNamedString(pp2b(enc, pend), av_flashVer, r.Link.flashVer)) enc = C_AMF_EncodeNamedString(enc, av_flashVer, r.Link.flashVer)
if enc == nil { if enc == nil {
return false return false
} }
} }
if r.Link.swfUrl != "" { if r.Link.swfUrl != "" {
enc = bAddr(C_AMF_EncodeNamedString(pp2b(enc, pend), av_swfUrl, r.Link.swfUrl)) enc = C_AMF_EncodeNamedString(enc, av_swfUrl, r.Link.swfUrl)
if enc == nil { if enc == nil {
return false return false
} }
} }
if r.Link.tcUrl != "" { if r.Link.tcUrl != "" {
enc = bAddr(C_AMF_EncodeNamedString(pp2b(enc, pend), av_tcUrl, r.Link.tcUrl)) enc = C_AMF_EncodeNamedString(enc, 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 = bAddr(C_AMF_EncodeNamedBoolean(pp2b(enc, pend), av_fpad, false)) enc = C_AMF_EncodeNamedBoolean(enc, av_fpad, false)
if enc == nil { if enc == nil {
return false return false
} }
enc = bAddr(C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_capabilities, 15)) enc = C_AMF_EncodeNamedNumber(enc, av_capabilities, 15)
if enc == nil { if enc == nil {
return false return false
} }
enc = bAddr(C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_audioCodecs, r.m_fAudioCodecs)) enc = C_AMF_EncodeNamedNumber(enc, av_audioCodecs, r.m_fAudioCodecs)
if enc == nil { if enc == nil {
return false return false
} }
enc = bAddr(C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_videoCodecs, r.m_fVideoCodecs)) enc = C_AMF_EncodeNamedNumber(enc, av_videoCodecs, r.m_fVideoCodecs)
if enc == nil { if enc == nil {
return false return false
} }
enc = bAddr(C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_videoFunction, 1)) enc = C_AMF_EncodeNamedNumber(enc, av_videoFunction, 1)
if enc == nil { if enc == nil {
return false return false
} }
if r.Link.pageUrl != "" { if r.Link.pageUrl != "" {
enc = bAddr(C_AMF_EncodeNamedString(pp2b(enc, pend), av_pageUrl, r.Link.pageUrl)) enc = C_AMF_EncodeNamedString(enc, av_pageUrl, r.Link.pageUrl)
if enc == nil { if enc == nil {
return false return false
} }
@ -744,45 +740,37 @@ 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 = bAddr(C_AMF_EncodeNamedNumber(pp2b(enc, pend), av_objectEncoding, r.m_fEncoding)) enc = C_AMF_EncodeNamedNumber(enc, av_objectEncoding, r.m_fEncoding)
if enc == nil { if enc == nil {
return false return false
} }
} }
if int(uintptr(incBytePtr(unsafe.Pointer(enc), 3))) >= int(uintptr( if copy(enc, []byte{0, 0, AMF_OBJECT_END}) != 3 {
unsafe.Pointer(pend))) {
return false return false
} }
enc = enc[3:]
(*[_Gi]byte)(unsafe.Pointer(enc))[0] = 0
enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1))
(*[_Gi]byte)(unsafe.Pointer(enc))[0] = 0
enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1))
(*[_Gi]byte)(unsafe.Pointer(enc))[0] = AMF_OBJECT_END
enc = (*byte)(incBytePtr(unsafe.Pointer(enc), 1))
/* add auth string */ /* add auth string */
if r.Link.auth != "" { if r.Link.auth != "" {
enc = bAddr(C_AMF_EncodeBoolean(pp2b(enc, pend), r.Link.lFlags&RTMP_LF_AUTH != 0)) enc = C_AMF_EncodeBoolean(enc, r.Link.lFlags&RTMP_LF_AUTH != 0)
if enc == nil { if enc == nil {
return false return false
} }
enc = bAddr(C_AMF_EncodeString(pp2b(enc, pend), r.Link.auth)) enc = C_AMF_EncodeString(enc, r.Link.auth)
if enc == nil { if enc == nil {
return false return false
} }
} }
for i := range r.Link.extras.o_props { for i := range r.Link.extras.o_props {
enc = bAddr(C_AMF_PropEncode(&r.Link.extras.o_props[i], pp2b(enc, pend))) enc = C_AMF_PropEncode(&r.Link.extras.o_props[i], enc)
if enc == nil { if enc == nil {
return false return false
} }
} }
packet.m_nBodySize = uint32(uintptr(decBytePtr(unsafe.Pointer(enc), packet.m_nBodySize = uint32((len(pbuf) - RTMP_MAX_HEADER_SIZE) - len(enc))
int(uintptr(unsafe.Pointer(packet.m_body))))))
return C_RTMP_SendPacket(r, &packet, 1) return C_RTMP_SendPacket(r, &packet, 1)
} }