From 5cca4d45a599b3f12995f27df98eb351eb2b10cd Mon Sep 17 00:00:00 2001
From: saxon <saxon.milton@gmail.com>
Date: Sat, 28 Jul 2018 10:15:12 +0930
Subject: [PATCH] Getting seg fault

---
 rtmp/rtmp.go | 35 +++++++++++++++++++++++------------
 1 file changed, 23 insertions(+), 12 deletions(-)

diff --git a/rtmp/rtmp.go b/rtmp/rtmp.go
index 9f0b33e0..fdf39148 100644
--- a/rtmp/rtmp.go
+++ b/rtmp/rtmp.go
@@ -379,7 +379,6 @@ func (s *session) Write(data []byte) (int, error) {
 	if rtmpIsConnected(s.rtmp) == 0 {
 		return 0, Err(1)
 	}
-	//if C.RTMP_Write(s.rtmp,(*C.char)(unsafe.Pointer(&data[0])),C.int(len(data))) == 0 {
 	if rtmpWrite(s.rtmp, data) == 0 {
 		return 0, Err(2)
 	}
@@ -449,6 +448,7 @@ func rtmpSetupUrl(r *C.RTMP, u string) int32 {
 	port = 0
 
 	len = strlen(url)
+	// TODO: port this
 	ret = int32(C.RTMP_ParseURL((*C.char)(unsafe.Pointer(url)), &r.Link.protocol, &r.Link.hostname,
 		(*C.uint)(&port), &r.Link.playpath0, &r.Link.app))
 
@@ -944,6 +944,7 @@ func sendConnectPacket(r *C.RTMP, cp *C.RTMPPacket) int {
 
 func rtmpConnectStream(r *C.RTMP, seekTime int32) int {
 	var packet C.RTMPPacket
+	memset((*byte)(unsafe.Pointer(&packet)), 0, int(unsafe.Sizeof(packet)))
 
 	if seekTime > 0 {
 		r.Link.seekTime = C.int(seekTime)
@@ -1306,7 +1307,6 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
 	}
 
 	if tbuf != nil {
-		// TODO: port C.writeN
 		wrote := int(writeN(r, tbuf, int(uintptr(decBytePtr(toff,
 			int(uintptr(unsafe.Pointer(tbuf))))))))
 		C.free(tbuf)
@@ -1333,10 +1333,10 @@ func sendPacket(r *C.RTMP, packet *C.RTMPPacket, queue int) int {
 		if queue != 0 {
 			var txn int
 			ptr = incBytePtr(ptr, 3+int(method.av_len))
+			txn = int(amfDecodeNumber((*byte)(ptr)))
 			// TODO: port this
-			txn = int(C.AMF_DecodeNumber((*C.char)(ptr)))
-			// TODO: port this
-			C.AV_queue(&r.m_methodCalls, &r.m_numCalls, &method, C.int(txn))
+			avQueue(&r.m_methodCalls, (*int)(unsafe.Pointer(&r.m_numCalls)), &method,
+				int(txn))
 		}
 	}
 
@@ -1402,21 +1402,21 @@ func sockBufSend(sb *C.RTMPSockBuf, buf *byte, l int32) int32 {
 
 // TODO: port RTMP_METHOD
 func avQueue(vals **C.RTMP_METHOD, num *int, av *C.AVal, txn int) {
-	var rtmpMethodPtr *C.RTMP_METHOD
 	if (*num & 0x0f) == 0 {
 		// TODO: work out what to do with the realloc
-		*vals = (*C.RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*int(unsafe.Sizeof(*(*vals))))))
+		*vals = (*C.RTMP_METHOD)(C.realloc(unsafe.Pointer(*vals), C.size_t((*num+16)*
+			int(unsafe.Sizeof(*(*vals))))))
 	}
 	tmp := unsafe.Pointer(C.malloc(C.size_t(av.av_len + 1)))
 	memmove(tmp, unsafe.Pointer(av.av_val), uintptr(av.av_len))
-	//C.memcpy(tmp, unsafe.Pointer(av.av_val), C.size_t(av.av_len))
-	*indxBytePtr(tmp, int(av.av_len)) = *(*byte)(unsafe.Pointer(C.CString("")))
+	*indxBytePtr(tmp, int(av.av_len)) = '\000'
+
 	(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
-		int(unsafe.Sizeof(rtmpMethodPtr))))).num = C.int(txn)
+		int(unsafe.Sizeof(*(*vals)))))).num = C.int(txn)
 	(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
-		int(unsafe.Sizeof(rtmpMethodPtr))))).name.av_len = av.av_len
+		int(unsafe.Sizeof(*(*vals)))))).name.av_len = av.av_len
 	(*(*C.RTMP_METHOD)(incPtr(unsafe.Pointer(*vals), *num,
-		int(unsafe.Sizeof(rtmpMethodPtr))))).name.av_val = (*C.char)(tmp)
+		int(unsafe.Sizeof(*(*vals)))))).name.av_val = (*C.char)(tmp)
 }
 
 func amfEncodeNamedNumber(output *byte, outend *byte, strName *C.AVal, dVal float64) *byte {
@@ -1471,6 +1471,17 @@ func amfEncodeNumber(output *byte, outend *byte, dVal float64) *byte {
 	return (*byte)(incBytePtr(unsafe.Pointer(output), 8))
 }
 
+func amfDecodeNumber(data *byte) float64 {
+	var dVal float64
+	var ci, co *uint8
+	ci = (*uint8)(unsafe.Pointer(data))
+	co = (*uint8)(unsafe.Pointer(&dVal))
+	for i := 0; i < 8; i++ {
+		*indxBytePtr(unsafe.Pointer(co), i) = *indxBytePtr(unsafe.Pointer(ci), 7-i)
+	}
+	return dVal
+}
+
 func amfEncodeNamedString(output *byte, outend *byte, strName *C.AVal, strValue *C.AVal) *byte {
 	if int(uintptr(unsafe.Pointer(output)))+2+int(strName.av_len) > int(uintptr(unsafe.Pointer(outend))) {
 		return nil