From 04d7540b348496f49ab350ea978bb9b3cff71cfc Mon Sep 17 00:00:00 2001 From: scruzin Date: Wed, 2 Jan 2019 18:20:30 +1030 Subject: [PATCH 1/4] Replaced Unix sockets with IPConn. Sock_XXX funtions have been left as is for PR readability. --- rtmp/rtmp_headers.go | 3 ++- rtmp/socket.go | 31 +++++++++++++------------------ rtmp/timeval_amd64.go | 7 ------- 3 files changed, 15 insertions(+), 26 deletions(-) delete mode 100644 rtmp/timeval_amd64.go diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index 62e667e1..90ae39db 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -139,7 +139,8 @@ type C_RTMPPacket struct { // typedef struct RTMPSockBuf // rtmp.h +127 type C_RTMPSockBuf struct { - conn *net.TCPConn + conn *net.IPConn + timeout int32 sb_size int sb_start int sb_buf [RTMP_BUFFER_CACHE_SIZE]byte // port const diff --git a/rtmp/socket.go b/rtmp/socket.go index f871cfbc..627db8b0 100644 --- a/rtmp/socket.go +++ b/rtmp/socket.go @@ -37,8 +37,7 @@ import ( "fmt" "log" "net" - - "golang.org/x/sys/unix" + "time" ) // int RTMP_Connect(RTMP *r, RTMPPacket* cp); @@ -54,11 +53,11 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } else { hostname = fmt.Sprintf("%s:%d", r.Link.hostname, r.Link.port) } - addr, err := net.ResolveTCPAddr("tcp4", hostname) + addr, err := net.ResolveIPAddr("tcp4", hostname) if err != nil { return false } - r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr) + r.m_sb.conn, err = net.DialIP("tcp4", nil, addr) if err != nil { return false } @@ -68,25 +67,14 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } } - f, err := r.m_sb.conn.File() - if err != nil { - log.Printf("failed to get fd to set timeout: %v", err) - return false - } - tv := setTimeval(int(r.Link.timeout)) - err = unix.SetsockoptTimeval(int(f.Fd()), unix.SOL_SOCKET, unix.SO_RCVTIMEO, &tv) - if err != nil { - log.Printf("failed to set timeout: %v", err) - } - + r.m_sb.timeout= r.Link.timeout r.m_bSendCounter = true - return C_RTMP_Connect1(r, cp) } // int SocksNegotiate(RTMP* r); // rtmp.c +1062 -func C_SocksNegotiate(r *C_RTMP, addr *net.TCPAddr) (ok bool) { +func C_SocksNegotiate(r *C_RTMP, addr *net.IPAddr) (ok bool) { ip := addr.IP.To4() packet := []byte{ 0x4, // SOCKS version @@ -116,7 +104,10 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { if sb.sb_size == 0 { sb.sb_start = 0 } - + err := sb.conn.SetReadDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) + if err != nil { + return -1 + } n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:]) if err != nil { return 0 @@ -129,6 +120,10 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { // rtmp.c +4297 // TODO replace send with golang net connection send func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) int32 { + err := sb.conn.SetWriteDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) + if err != nil { + return -1 + } n, err := sb.conn.Write(buf) if err != nil { return -1 diff --git a/rtmp/timeval_amd64.go b/rtmp/timeval_amd64.go deleted file mode 100644 index df7f4c4e..00000000 --- a/rtmp/timeval_amd64.go +++ /dev/null @@ -1,7 +0,0 @@ -package rtmp - -import "golang.org/x/sys/unix" - -func setTimeval(sec int) unix.Timeval { - return unix.Timeval{Sec: int64(sec)} -} From 0b2c38117ff2d0591af032adc288769bd9b07f46 Mon Sep 17 00:00:00 2001 From: saxon Date: Wed, 2 Jan 2019 21:28:20 +1030 Subject: [PATCH 2/4] revid: trying to fix rtmp --- rtmp/rtmp_headers.go | 2 +- rtmp/socket.go | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/rtmp/rtmp_headers.go b/rtmp/rtmp_headers.go index 90ae39db..eb33814f 100644 --- a/rtmp/rtmp_headers.go +++ b/rtmp/rtmp_headers.go @@ -139,7 +139,7 @@ type C_RTMPPacket struct { // typedef struct RTMPSockBuf // rtmp.h +127 type C_RTMPSockBuf struct { - conn *net.IPConn + conn *net.TCPConn timeout int32 sb_size int sb_start int diff --git a/rtmp/socket.go b/rtmp/socket.go index 627db8b0..35533471 100644 --- a/rtmp/socket.go +++ b/rtmp/socket.go @@ -53,12 +53,15 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } else { hostname = fmt.Sprintf("%s:%d", r.Link.hostname, r.Link.port) } - addr, err := net.ResolveIPAddr("tcp4", hostname) + addr, err := net.ResolveTCPAddr("tcp4", hostname) if err != nil { + fmt.Println(err) + fmt.Println("socket.go:58") return false } - r.m_sb.conn, err = net.DialIP("tcp4", nil, addr) + r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr) if err != nil { + fmt.Println("socket.go:63") return false } if r.Link.socksport != 0 { @@ -67,14 +70,14 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } } - r.m_sb.timeout= r.Link.timeout + r.m_sb.timeout = r.Link.timeout r.m_bSendCounter = true return C_RTMP_Connect1(r, cp) } // int SocksNegotiate(RTMP* r); // rtmp.c +1062 -func C_SocksNegotiate(r *C_RTMP, addr *net.IPAddr) (ok bool) { +func C_SocksNegotiate(r *C_RTMP, addr *net.TCPAddr) (ok bool) { ip := addr.IP.To4() packet := []byte{ 0x4, // SOCKS version @@ -106,6 +109,7 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { } err := sb.conn.SetReadDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) if err != nil { + fmt.Println("socket.go:111") return -1 } n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:]) @@ -122,6 +126,7 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) int32 { err := sb.conn.SetWriteDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) if err != nil { + fmt.Println("socket.go:128") return -1 } n, err := sb.conn.Write(buf) From cec95cd65253d43fba00f61a1165f7cf8280a203 Mon Sep 17 00:00:00 2001 From: saxon Date: Wed, 2 Jan 2019 21:54:01 +1030 Subject: [PATCH 3/4] rtmp: using TCP rather IP --- revid/revid.go | 1 - 1 file changed, 1 deletion(-) diff --git a/revid/revid.go b/revid/revid.go index d509c6bb..62e22d64 100644 --- a/revid/revid.go +++ b/revid/revid.go @@ -490,7 +490,6 @@ func (r *Revid) startRaspivid() error { // setupInputForFile sets things up for getting input from a file func (r *Revid) setupInputForFile() error { delay := time.Second / time.Duration(r.config.FrameRate) - f, err := os.Open(r.config.InputFileName) if err != nil { r.config.Logger.Log(logger.Error, err.Error()) From e340c9b9be5736382dfbf256d3d4427c7bda0825 Mon Sep 17 00:00:00 2001 From: saxon Date: Wed, 2 Jan 2019 21:55:43 +1030 Subject: [PATCH 4/4] rtmp: removed debug prints --- rtmp/socket.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/rtmp/socket.go b/rtmp/socket.go index 35533471..beef8e5c 100644 --- a/rtmp/socket.go +++ b/rtmp/socket.go @@ -55,13 +55,10 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) (ok bool) { } addr, err := net.ResolveTCPAddr("tcp4", hostname) if err != nil { - fmt.Println(err) - fmt.Println("socket.go:58") return false } r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr) if err != nil { - fmt.Println("socket.go:63") return false } if r.Link.socksport != 0 { @@ -109,7 +106,6 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { } err := sb.conn.SetReadDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) if err != nil { - fmt.Println("socket.go:111") return -1 } n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:]) @@ -126,7 +122,6 @@ func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) int { func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) int32 { err := sb.conn.SetWriteDeadline(time.Now().Local().Add(time.Second * time.Duration(sb.timeout))) if err != nil { - fmt.Println("socket.go:128") return -1 } n, err := sb.conn.Write(buf)