Merged in use-go-errors (pull request #97)

Use Go errors for all rtmp functions.
Approved-by: Saxon Milton <saxon.milton@gmail.com>
Approved-by: Alan Noble <anoble@gmail.com>
This commit is contained in:
Alan Noble 2019-01-06 23:18:35 +00:00
commit 705fedc532
5 changed files with 286 additions and 350 deletions

View File

@ -42,11 +42,11 @@ import (
// int RTMP_ParseURL(const char *url, int *protocol, AVal *host, unsigned int *port, AVal *playpath, AVal *app);
// parseurl.c +33
func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, ok bool) {
func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app, playpath string, err error) {
u, err := url.Parse(addr)
if err != nil {
log.Printf("failed to parse addr: %v", err)
return protocol, host, port, app, playpath, false
return protocol, host, port, app, playpath, err
}
switch u.Scheme {
@ -66,20 +66,20 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app
protocol = RTMP_PROTOCOL_RTMPTS
default:
log.Printf("unknown scheme: %q", u.Scheme)
return protocol, host, port, app, playpath, false
return protocol, host, port, app, playpath, errUnknownScheme
}
host = u.Host
if p := u.Port(); p != "" {
pi, err := strconv.Atoi(p)
if err != nil {
return protocol, host, port, app, playpath, false
return protocol, host, port, app, playpath, err
}
port = uint16(pi)
}
if !path.IsAbs(u.Path) {
return protocol, host, port, app, playpath, true
return protocol, host, port, app, playpath, nil
}
elems := strings.SplitN(u.Path[1:], "/", 3)
app = elems[0]
@ -99,5 +99,5 @@ func C_RTMP_ParseURL(addr string) (protocol int32, host string, port uint16, app
}
}
return protocol, host, port, app, playpath, true
return protocol, host, port, app, playpath, nil
}

File diff suppressed because it is too large Load Diff

View File

@ -138,13 +138,7 @@ type C_RTMPPacket struct {
// typedef struct RTMPSockBuf
// rtmp.h +127
type C_RTMPSockBuf struct {
conn *net.TCPConn
timeout int32
sb_size int
sb_start int
sb_buf [RTMP_BUFFER_CACHE_SIZE]byte // port const
}
// DELETED: subsumed by C_RTMP_LNK
// RTMPPacket_IsReady(a)
// rtmp.h +142
@ -170,8 +164,9 @@ type C_RTMP_LNK struct {
lFlags int32
swfAge int32
protocol int32
timeout int32
timeout uint
port uint16
conn *net.TCPConn
}
// typedef struct RTMPMethod
@ -213,6 +208,5 @@ type C_RTMP struct {
m_resplen int32
m_unackd int32
m_write C_RTMPPacket
m_sb C_RTMPSockBuf
Link C_RTMP_LNK
}

View File

@ -58,7 +58,7 @@ func (s *Session) Open() error {
return errors.New("rtmp: attempt to start already running session")
}
var err error
s.rtmp, err = startSession(s.rtmp, s.url, uint32(s.timeout))
s.rtmp, err = startSession(s.rtmp, s.url, s.timeout)
if s.rtmp == nil {
return err
}
@ -84,13 +84,14 @@ func (s *Session) Write(data []byte) (int, error) {
return 0, Err(3)
}
if C_RTMP_IsConnected(s.rtmp) == 0 {
//if C.RTMP_IsConnected(s.rtmp) == 0 {
if !C_RTMP_IsConnected(s.rtmp) {
return 0, Err(1)
}
if C_RTMP_Write(s.rtmp, data) == 0 {
err := C_RTMP_Write(s.rtmp, data)
if err != nil {
//if C.RTMP_Write(s.rtmp, (*byte)(unsafe.Pointer(&data[0])), int32(len(data))) == 0 {
// TODO: propagate err
return 0, Err(2)
}
return len(data), nil

View File

@ -1,102 +0,0 @@
/*
NAME
rtmp.go
DESCRIPTION
See Readme.md
AUTHORS
Saxon Nelson-Milton <saxon@ausocean.org>
Dan Kortschak <dan@ausocean.org>
LICENSE
rtmp.go is Copyright (C) 2017 the Australian Ocean Lab (AusOcean)
It is free software: you can redistribute it and/or modify them
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with revid in gpl.txt. If not, see http://www.gnu.org/licenses.
Derived from librtmp under the GNU Lesser General Public License 2.1
Copyright (C) 2005-2008 Team XBMC http://www.xbmc.org
Copyright (C) 2008-2009 Andrej Stepanchuk
Copyright (C) 2009-2010 Howard Chu
*/
package rtmp
import (
"errors"
"net"
"strconv"
"time"
)
// int RTMP_Connect(RTMP *r, RTMPPacket* cp);
// rtmp.c +1032
func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error {
if r.Link.host == "" {
return errors.New("Empty host")
}
addr, err := net.ResolveTCPAddr("tcp4", r.Link.host+":"+strconv.Itoa(int(r.Link.port)))
if err != nil {
return err
}
r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr)
if err != nil {
return err
}
r.m_sb.timeout = r.Link.timeout
if !C_RTMP_Connect1(r, cp) {
return errors.New("RTMP connection failed")
}
return nil
}
// int SocksNegotiate(RTMP* r);
// rtmp.c +1062
// DELETED
// int RTMPSockBuf_Fill(RTMPSockBuf* sb);
// rtmp.c +4253
func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) (int, error) {
if sb.sb_size == 0 {
sb.sb_start = 0
}
err := sb.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout)))
if err != nil {
return 0, err
}
n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:])
sb.sb_size += n
return n, err
}
// int RTMPSockBuf_Send(RTMPSockBuf* sb, const char* buf, int len);
// rtmp.c +4297
// TODO replace send with golang net connection send
func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) (int, error) {
err := sb.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout)))
if err != nil {
return 0, err
}
return sb.conn.Write(buf)
}
// int
// RTMPSockBuf_Close(RTMPSockBuf *sb)
// rtmp.c +4369
func C_RTMPSockBuf_Close(sb *C_RTMPSockBuf) error {
if sb.conn == nil {
return nil
}
return sb.conn.Close()
}