From 072c065224bcfed8f658c93d765f7bcb8c093831 Mon Sep 17 00:00:00 2001 From: saxon Date: Tue, 10 Jul 2018 20:06:49 +0930 Subject: [PATCH] rtmp.go builds after porting RTMP_Write to rtmpWrite. Still need to clean up and test --- rtmp/rtmp.go | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 2c436c3d..02ddfc05 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -79,7 +79,7 @@ func NewSession(url string, connectTimeout uint) Session { // See #define AVC(str) {str, sizeof(str)-1} in amf.h func AVC(str string) C.AVal { var aval C.AVal - aval.av_val = (*C.char)([]byte(str)[0]) + aval.av_val = (*C.char)(unsafe.Pointer(&([]byte(str)[0]))) aval.av_len = (C.int)(int(unsafe.Sizeof([]byte(str))-1)) return aval } @@ -161,7 +161,7 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { } // C: if (!RTMPPacket_Alloc(pkt, pkt->m_nBodySize)) - if !tools.UintToBool(uint( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize))) { + if !tools.IntToBool(int( C.RTMPPacket_Alloc(pkt, pkt.m_nBodySize))) { log.Println("Failed to allocated packet") return 0 } @@ -169,35 +169,45 @@ func (s *session) rtmpWrite(r *C.RTMP, buf []byte) int { // Note this is pointer arithmatic enc = pkt.m_body // pend = enc + pkt.m_nBodySize - pend = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(enc)) + ( unsafe.Sizeof(C.int(0)) * - (uintptr(pkt.m_nBodySize)) ) )) + pend = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(enc)) + + ( unsafe.Sizeof(C.char(0x00)) * (uintptr(pkt.m_nBodySize)) ) )) if pkt.m_packetType == C.RTMP_PACKET_TYPE_INFO { + // av_setDataFrame is a static const global in rtmp.c av_setDataFrame := AVC("@setDataFrame") - enc = C.AMF_EncodeString(enc, pend, unsafe.Pointer&av_setDataFrame ) - pkt.m_nBytesRead = enc - pkt.m_body + enc = C.AMF_EncodeString(enc, pend, &av_setDataFrame ) + + // C-code: pkt.m_nBytesRead enc - pkt.m_body + pkt.m_nBytesRead = (C.uint)((uintptr(unsafe.Pointer(enc)) - + uintptr(unsafe.Pointer(pkt.m_body)))/unsafe.Sizeof(C.char(0x00))) } } else { - enc = pkt.m_body + pkt.m_nBytesRead + //C-code: enc = pkt.m_body + pkt.m_nBytesRead + enc = (*C.char)(unsafe.Pointer( uintptr(unsafe.Pointer(pkt.m_body)) + + ( unsafe.Sizeof(C.char(0x00)) * (uintptr(pkt.m_nBytesRead)) ) )) } - num = pkt.m_nBodySize - pkt.m_nBytesRead + num = int(pkt.m_nBodySize - pkt.m_nBytesRead) if num > s2 { num = s2 } // C.memcpy(enc, buf, num) - copy(enc,buf) + // Need to create byte slice from c array enc + encSlice := C.GoBytes(unsafe.Pointer(enc),C.int(num)) + copy(encSlice,buf) - pkt.m_nBytesRead += num + pkt.m_nBytesRead += C.uint(num) s2 -= num - buf += num + + //buf += num + buf = buf[num:] if pkt.m_nBytesRead == pkt.m_nBodySize { - ret = C.RTMP_SendPacket(r, pkt, 0) - c.RTMPPacket_Free(pkt) + ret = int(C.RTMP_SendPacket(r, pkt, 0)) + C.RTMPPacket_Free(pkt) pkt.m_nBytesRead = 0 - if !uintToBool(ret) { + if !tools.IntToBool(ret) { return -1 } // buf += 4 @@ -217,7 +227,7 @@ func (s *session) writeFrame(data []byte) uint { return 1 } // This is where C.RTMP_Write would be used - if rtmpWrite(s.rtmp, data) <= 0 { + if s.rtmpWrite(s.rtmp, data) <= 0 { return 2 } return 0