From 965641be1649f16882ca4757243aa43d7b6f698d Mon Sep 17 00:00:00 2001 From: saxon Date: Sat, 14 Jul 2018 14:49:21 +0930 Subject: [PATCH] Got rid of C.memcpy --- rtmp/rtmp.go | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go index 77042fa4..0b06139b 100644 --- a/rtmp/rtmp.go +++ b/rtmp/rtmp.go @@ -49,6 +49,7 @@ import ( "unsafe" _"fmt" "math" + "reflect" ) const ( @@ -130,7 +131,7 @@ func (s *session) Write(data []byte) (int, error) { } func rtmpWrite(r *C.RTMP, data []byte) int { - buf := unsafe.Pointer(&data[0]) + buf := sliceToPtr(data) var pkt = &r.m_write var pend, enc unsafe.Pointer size := len(data) @@ -198,9 +199,7 @@ func rtmpWrite(r *C.RTMP, data []byte) int { if num > s2 { num = s2 } - // TODO: Can only remove this mem copy onc I port the RTMPPacket struct and - // AllocatPacket - C.memcpy(enc,buf,C.ulong(num)) + copy(ptrToSlice(enc,num),ptrToSlice(buf,num)) pkt.m_nBytesRead += C.uint(num) s2 -= num buf = incPtr(buf,num) @@ -221,12 +220,23 @@ func rtmpWrite(r *C.RTMP, data []byte) int { return size + s2 } +func sliceToPtr(data []byte) unsafe.Pointer { + return unsafe.Pointer(&data[0]) +} + +func ptrToSlice(data unsafe.Pointer, size int) []byte { + var ret []byte + shDest := (*reflect.SliceHeader)(unsafe.Pointer(&ret)) + shDest.Data = uintptr(data) + shDest.Len = size + shDest.Cap = size + return ret +} func incPtr(ptr unsafe.Pointer, inc int) unsafe.Pointer { return unsafe.Pointer(uintptr(ptr) + uintptr(inc)) } - /* func sendPacket(r *C.RTMP, pkt *C.RTMPPacket, queue int) int { const prevPacket *C.RTMPPacket