2018-09-19 09:51:32 +03:00
|
|
|
/*
|
|
|
|
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 (
|
2019-01-05 15:43:19 +03:00
|
|
|
"errors"
|
2018-09-30 12:29:01 +03:00
|
|
|
"fmt"
|
|
|
|
"net"
|
2019-01-02 10:50:30 +03:00
|
|
|
"time"
|
2018-09-19 09:51:32 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
// int RTMP_Connect(RTMP *r, RTMPPacket* cp);
|
|
|
|
// rtmp.c +1032
|
2019-01-05 15:43:19 +03:00
|
|
|
func C_RTMP_Connect(r *C_RTMP, cp *C_RTMPPacket) error {
|
|
|
|
if r.Link.host == "" {
|
|
|
|
return errors.New("Empty host")
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
2019-01-05 15:43:19 +03:00
|
|
|
hostPort := fmt.Sprintf("%s:%d", r.Link.host, r.Link.port)
|
|
|
|
addr, err := net.ResolveTCPAddr("tcp4", hostPort)
|
2018-09-30 12:29:01 +03:00
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return err
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
2019-01-02 13:58:20 +03:00
|
|
|
r.m_sb.conn, err = net.DialTCP("tcp4", nil, addr)
|
2018-09-30 12:29:01 +03:00
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return err
|
2018-10-01 02:36:41 +03:00
|
|
|
}
|
2018-09-19 09:51:32 +03:00
|
|
|
|
2019-01-02 13:58:20 +03:00
|
|
|
r.m_sb.timeout = r.Link.timeout
|
2019-01-05 15:43:19 +03:00
|
|
|
if C_RTMP_Connect1(r, cp) {
|
|
|
|
return nil
|
|
|
|
} else {
|
|
|
|
return errors.New("RTMP connection failed")
|
|
|
|
}
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
2018-10-01 02:36:41 +03:00
|
|
|
// int SocksNegotiate(RTMP* r);
|
|
|
|
// rtmp.c +1062
|
2019-01-05 15:43:19 +03:00
|
|
|
// DELETED
|
2018-10-01 02:36:41 +03:00
|
|
|
|
2018-09-19 09:51:32 +03:00
|
|
|
// int RTMPSockBuf_Fill(RTMPSockBuf* sb);
|
|
|
|
// rtmp.c +4253
|
2019-01-05 15:43:19 +03:00
|
|
|
func C_RTMPSockBuf_Fill(sb *C_RTMPSockBuf) (int, error) {
|
2018-09-19 09:51:32 +03:00
|
|
|
if sb.sb_size == 0 {
|
|
|
|
sb.sb_start = 0
|
|
|
|
}
|
2019-01-05 15:43:19 +03:00
|
|
|
err := sb.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout)))
|
2019-01-02 10:50:30 +03:00
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return -1, err
|
2019-01-02 10:50:30 +03:00
|
|
|
}
|
2018-09-30 12:29:01 +03:00
|
|
|
n, err := sb.conn.Read(sb.sb_buf[sb.sb_start+sb.sb_size:])
|
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return 0, err
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
2018-09-30 12:29:01 +03:00
|
|
|
sb.sb_size += n
|
2019-01-05 15:43:19 +03:00
|
|
|
return n, nil
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int RTMPSockBuf_Send(RTMPSockBuf* sb, const char* buf, int len);
|
|
|
|
// rtmp.c +4297
|
|
|
|
// TODO replace send with golang net connection send
|
2019-01-05 15:43:19 +03:00
|
|
|
func C_RTMPSockBuf_Send(sb *C_RTMPSockBuf, buf []byte) (int, error) {
|
|
|
|
err := sb.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(sb.timeout)))
|
2019-01-02 10:50:30 +03:00
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return -1, err
|
2019-01-02 10:50:30 +03:00
|
|
|
}
|
2018-09-30 12:29:01 +03:00
|
|
|
n, err := sb.conn.Write(buf)
|
|
|
|
if err != nil {
|
2019-01-05 15:43:19 +03:00
|
|
|
return -1, err
|
2018-09-30 12:29:01 +03:00
|
|
|
}
|
2019-01-05 15:43:19 +03:00
|
|
|
return n, nil
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// int
|
|
|
|
// RTMPSockBuf_Close(RTMPSockBuf *sb)
|
|
|
|
// rtmp.c +4369
|
2019-01-05 15:43:19 +03:00
|
|
|
func C_RTMPSockBuf_Close(sb *C_RTMPSockBuf) error {
|
2018-09-30 12:29:01 +03:00
|
|
|
if sb.conn != nil {
|
|
|
|
err := sb.conn.Close()
|
|
|
|
sb.conn = nil
|
2019-01-05 15:43:19 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2018-09-30 12:29:01 +03:00
|
|
|
}
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|
2019-01-05 15:43:19 +03:00
|
|
|
return nil
|
2018-09-19 09:51:32 +03:00
|
|
|
}
|