Use Go errors for C_RTMP_ParseURL, C_RTMP_SetupURL, C_RTMP_Connect1, and C_RTMP_ConnectStream.

This commit is contained in:
scruzin 2019-01-06 09:20:32 +10:30
parent 8381d1b1e8
commit e173153ca6
3 changed files with 39 additions and 28 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
}

View File

@ -128,31 +128,39 @@ var (
}
)
var (
errUnknownScheme = errors.New("Unknown scheme")
errHandshake = errors.New("Handshake failed")
errConnSend = errors.New("Connection send error")
errConnStream = errors.New("Connection stream error")
)
func startSession(rtmp *C_RTMP, u string, timeout uint32) (*C_RTMP, error) {
connect_timeout := int32(timeout)
rtmp = C_RTMP_Alloc()
C_RTMP_Init(rtmp)
rtmp.Link.timeout = connect_timeout
if !C_RTMP_SetupURL(rtmp, u) {
err := C_RTMP_SetupURL(rtmp, u)
if err != nil {
C_RTMP_Close(rtmp)
//C.RTMP_Free(rtmp)
return nil, errors.New("rtmp startSession: Failed to setup URL!")
return nil, err
}
C_RTMP_EnableWrite(rtmp)
C_RTMP_SetBufferMS(rtmp, 3600*1000)
err := C_RTMP_Connect(rtmp, nil)
err = C_RTMP_Connect(rtmp, nil)
if err != nil {
C_RTMP_Close(rtmp)
//C.RTMP_Free(rtmp)
return nil, errors.New("rtmp startSession: Failed to connect with error: " + err.Error())
return nil, err
}
// TODO: port this
if !C_RTMP_ConnectStream(rtmp, 0) {
err = C_RTMP_ConnectStream(rtmp, 0)
if err != nil {
C_RTMP_Close(rtmp)
//C.RTMP_Free(rtmp)
return nil, errors.New("rtmp startSession: Failed to connect stream!")
return nil, err
}
return rtmp, nil
@ -242,10 +250,10 @@ func C_RTMP_SetBufferMS(r *C_RTMP, size int32) {
// int RTMP_SetupURL(RTMP *r, char* url);
// rtmp.c +757
// NOTE: code dealing with rtmp over http has been disregarded
func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) {
r.Link.protocol, r.Link.host, r.Link.port, r.Link.app, r.Link.playpath0, ok = C_RTMP_ParseURL(addr)
if !ok {
return false
func C_RTMP_SetupURL(r *C_RTMP, addr string) (err error) {
r.Link.protocol, r.Link.host, r.Link.port, r.Link.app, r.Link.playpath0, err = C_RTMP_ParseURL(addr)
if err != nil {
return nil
}
r.Link.playpath = r.Link.playpath0
@ -269,32 +277,33 @@ func C_RTMP_SetupURL(r *C_RTMP, addr string) (ok bool) {
r.Link.port = 1935
}
}
return true
return nil
}
// int RTMP_Connect1(RTMP* r, RTMPPacket* cp);
// rtmp.c +978
func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) (ok bool) {
func C_RTMP_Connect1(r *C_RTMP, cp *C_RTMPPacket) error {
if debugMode {
log.Println("... connected, handshaking...")
}
if !C_HandShake(r, 1) {
log.Println("C_RTMP_Connect1: handshake failed!")
return false
return errHandshake
}
if debugMode {
log.Println("... handshaked...")
}
if !C_SendConnectPacket(r, cp) {
log.Println("RTMP connect failed!")
return false
return errConnSend
}
return true
return nil
}
// int RTMP_ConnectStream(RTMP* r, int seekTime);
// rtmp.c +1099
func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) (playing bool) {
// Side effects: r.m_bPlaying is set true upon successful connection
func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) error {
var packet C_RTMPPacket
if seekTime > 0 {
@ -323,7 +332,11 @@ func C_RTMP_ConnectStream(r *C_RTMP, seekTime int32) (playing bool) {
C_RTMPPacket_Free(&packet)
}
}
return r.m_bPlaying
if !r.m_bPlaying {
return errConnStream
}
return nil
}
// int RTMP_ClientPacket()
@ -836,6 +849,7 @@ func C_AV_erase(m []C_RTMP_METHOD, i int) []C_RTMP_METHOD {
// int HandleInvoke(RTMP* r, const char* body, unsigned int nBodySize);
// rtmp.c +2912
// Side effects: r.m_bPlaying set to true upon av_NetStream_Publish_Start
func C_HandleInvoke(r *C_RTMP, body []byte) (ok bool) {
if body[0] != 0x02 {
// TODO use new logger here

View File

@ -55,10 +55,7 @@ func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error {
return err
}
r.m_sb.timeout = r.Link.timeout
if !C_RTMP_Connect1(r, cp) {
return errors.New("RTMP connection failed")
}
return nil
return C_RTMP_Connect1(r, cp)
}
// int SocksNegotiate(RTMP* r);