2018-08-24 03:55:36 +03:00
|
|
|
/*
|
|
|
|
NAME
|
2019-01-19 05:46:46 +03:00
|
|
|
conn.go
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
DESCRIPTION
|
2019-01-19 05:46:46 +03:00
|
|
|
RTMP connection functionality.
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon Nelson-Milton <saxon@ausocean.org>
|
|
|
|
Dan Kortschak <dan@ausocean.org>
|
2019-01-07 08:50:35 +03:00
|
|
|
Alan Noble <alan@ausocean.org>
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
LICENSE
|
2019-01-19 05:46:46 +03:00
|
|
|
conn.go is Copyright (C) 2017-2019 the Australian Ocean Lab (AusOcean)
|
2018-08-24 03:55:36 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2018-08-24 03:22:51 +03:00
|
|
|
package rtmp
|
|
|
|
|
2019-01-10 15:46:20 +03:00
|
|
|
import (
|
|
|
|
"io"
|
2019-01-10 16:49:50 +03:00
|
|
|
"net"
|
2019-01-19 05:46:46 +03:00
|
|
|
"strconv"
|
2019-01-10 15:46:20 +03:00
|
|
|
"time"
|
2019-01-11 23:43:27 +03:00
|
|
|
|
|
|
|
"bitbucket.org/ausocean/av/rtmp/amf"
|
2019-01-10 15:46:20 +03:00
|
|
|
)
|
|
|
|
|
2019-01-19 05:46:46 +03:00
|
|
|
// Conn represents an RTMP connection.
|
|
|
|
type Conn struct {
|
2019-01-14 02:47:47 +03:00
|
|
|
inChunkSize uint32
|
|
|
|
outChunkSize uint32
|
|
|
|
nBytesIn uint32
|
|
|
|
nBytesInSent uint32
|
2019-01-07 08:50:35 +03:00
|
|
|
streamID int32
|
2019-01-14 02:47:47 +03:00
|
|
|
serverBW uint32
|
|
|
|
clientBW uint32
|
2019-01-14 03:01:42 +03:00
|
|
|
clientBW2 uint8
|
2019-01-07 08:50:35 +03:00
|
|
|
isPlaying bool
|
|
|
|
numInvokes int32
|
|
|
|
methodCalls []method
|
|
|
|
channelsAllocatedIn int32
|
|
|
|
channelsAllocatedOut int32
|
2019-01-09 15:21:07 +03:00
|
|
|
channelsIn []*packet
|
|
|
|
channelsOut []*packet
|
2019-01-07 08:50:35 +03:00
|
|
|
channelTimestamp []int32
|
2019-01-07 16:29:41 +03:00
|
|
|
deferred []byte
|
2019-01-07 08:50:35 +03:00
|
|
|
link link
|
2019-01-07 16:29:41 +03:00
|
|
|
log Log
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-10 16:49:50 +03:00
|
|
|
// link represents RTMP URL and connection information.
|
|
|
|
type link struct {
|
|
|
|
host string
|
|
|
|
playpath string
|
2019-01-13 01:42:41 +03:00
|
|
|
url string
|
2019-01-10 16:49:50 +03:00
|
|
|
app string
|
|
|
|
auth string
|
2019-01-11 02:22:21 +03:00
|
|
|
flags int32
|
2019-01-10 16:49:50 +03:00
|
|
|
protocol int32
|
|
|
|
timeout uint
|
|
|
|
port uint16
|
|
|
|
conn *net.TCPConn
|
|
|
|
}
|
|
|
|
|
|
|
|
// method represents an RTMP method.
|
|
|
|
type method struct {
|
|
|
|
name string
|
|
|
|
num int32
|
|
|
|
}
|
|
|
|
|
2019-01-07 16:29:41 +03:00
|
|
|
// Log defines the RTMP logging function.
|
|
|
|
type Log func(level int8, message string, params ...interface{})
|
|
|
|
|
|
|
|
// Log levels used by Log.
|
|
|
|
const (
|
|
|
|
DebugLevel int8 = -1
|
|
|
|
InfoLevel int8 = 0
|
|
|
|
WarnLevel int8 = 1
|
|
|
|
ErrorLevel int8 = 2
|
|
|
|
FatalLevel int8 = 5
|
|
|
|
)
|
|
|
|
|
2019-01-11 08:30:00 +03:00
|
|
|
// flvTagheaderSize is the FLV header size we expect.
|
|
|
|
// NB: We don't accept extended headers.
|
|
|
|
const flvTagheaderSize = 11
|
|
|
|
|
2019-01-19 05:46:46 +03:00
|
|
|
// Dial connects to RTMP server specified by the given URL and returns the connection.
|
|
|
|
func Dial(url string, timeout uint, log Log) (*Conn, error) {
|
|
|
|
log(DebugLevel, pkg+"rtmp.Dial")
|
|
|
|
c := Conn{
|
2019-01-09 14:35:04 +03:00
|
|
|
inChunkSize: 128,
|
|
|
|
outChunkSize: 128,
|
|
|
|
clientBW: 2500000,
|
|
|
|
clientBW2: 2,
|
|
|
|
serverBW: 2500000,
|
|
|
|
log: log,
|
|
|
|
link: link{
|
|
|
|
timeout: timeout,
|
|
|
|
},
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:46:46 +03:00
|
|
|
var err error
|
|
|
|
c.link.protocol, c.link.host, c.link.port, c.link.app, c.link.playpath, err = parseURL(url)
|
2019-01-07 08:50:35 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:46:46 +03:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if c.link.app == "" {
|
|
|
|
return nil, errInvalidURL
|
|
|
|
}
|
|
|
|
if c.link.port == 0 {
|
|
|
|
switch {
|
|
|
|
case (c.link.protocol & featureSSL) != 0:
|
|
|
|
c.link.port = 433
|
|
|
|
c.log(FatalLevel, pkg+"SSL not supported")
|
|
|
|
case (c.link.protocol & featureHTTP) != 0:
|
|
|
|
c.link.port = 80
|
|
|
|
default:
|
|
|
|
c.link.port = 1935
|
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
c.link.url = rtmpProtocolStrings[c.link.protocol] + "://" + c.link.host + ":" + strconv.Itoa(int(c.link.port)) + "/" + c.link.app
|
|
|
|
c.link.protocol |= featureWrite
|
2019-01-07 08:50:35 +03:00
|
|
|
|
2019-01-19 05:46:46 +03:00
|
|
|
err = connect(&c)
|
2019-01-07 08:50:35 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:46:46 +03:00
|
|
|
return nil, err
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
return &c, nil
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
|
2019-01-19 05:46:46 +03:00
|
|
|
// Close terminates the RTMP connection.
|
|
|
|
// NB: Close is idempotent and the connection value is cleared completely.
|
|
|
|
func (c *Conn) Close() error {
|
|
|
|
c.log(DebugLevel, pkg+"Conn.Close")
|
|
|
|
if !c.isConnected() {
|
2019-01-07 10:30:42 +03:00
|
|
|
return errNotConnected
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
if c.streamID > 0 {
|
|
|
|
if c.link.protocol&featureWrite != 0 {
|
|
|
|
sendFCUnpublish(c)
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
sendDeleteStream(c, float64(c.streamID))
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
c.link.conn.Close()
|
|
|
|
*c = Conn{}
|
2019-01-10 16:59:51 +03:00
|
|
|
return nil
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|
2018-08-24 03:22:51 +03:00
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// Write writes a frame (flv tag) to the rtmp connection.
|
2019-01-19 05:46:46 +03:00
|
|
|
func (c *Conn) Write(data []byte) (int, error) {
|
|
|
|
if !c.isConnected() {
|
2019-01-07 10:30:42 +03:00
|
|
|
return 0, errNotConnected
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
2019-01-11 08:30:00 +03:00
|
|
|
if len(data) < flvTagheaderSize {
|
|
|
|
return 0, errInvalidFlvTag
|
2019-01-10 15:35:24 +03:00
|
|
|
}
|
2019-01-11 02:40:27 +03:00
|
|
|
if data[0] == packetTypeInfo || (data[0] == 'F' && data[1] == 'L' && data[2] == 'V') {
|
|
|
|
return 0, errUnimplemented
|
|
|
|
}
|
2019-01-10 15:35:24 +03:00
|
|
|
|
|
|
|
pkt := packet{
|
|
|
|
packetType: data[0],
|
2019-01-11 23:43:27 +03:00
|
|
|
bodySize: amf.DecodeInt24(data[1:4]),
|
|
|
|
timestamp: amf.DecodeInt24(data[4:7]) | uint32(data[7])<<24,
|
2019-01-11 02:22:21 +03:00
|
|
|
channel: chanSource,
|
2019-01-19 05:46:46 +03:00
|
|
|
info: c.streamID,
|
2019-01-10 15:35:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 02:22:21 +03:00
|
|
|
pkt.resize(pkt.bodySize, headerSizeAuto)
|
2019-01-11 08:30:00 +03:00
|
|
|
copy(pkt.body, data[flvTagheaderSize:flvTagheaderSize+pkt.bodySize])
|
2019-01-19 05:46:46 +03:00
|
|
|
err := pkt.writeTo(c, false)
|
2019-01-06 07:12:51 +03:00
|
|
|
if err != nil {
|
2019-01-07 10:30:42 +03:00
|
|
|
return 0, err
|
2018-08-24 03:22:51 +03:00
|
|
|
}
|
|
|
|
return len(data), nil
|
|
|
|
}
|
2019-01-07 08:50:35 +03:00
|
|
|
|
2019-01-10 15:46:20 +03:00
|
|
|
// I/O functions
|
2019-01-10 16:59:51 +03:00
|
|
|
|
|
|
|
// read from an RTMP connection. Sends a bytes received message if the
|
|
|
|
// number of bytes received (nBytesIn) is greater than the number sent
|
|
|
|
// (nBytesInSent) by 10% of the bandwidth.
|
2019-01-19 05:46:46 +03:00
|
|
|
func (c *Conn) read(buf []byte) (int, error) {
|
|
|
|
err := c.link.conn.SetReadDeadline(time.Now().Add(time.Second * time.Duration(c.link.timeout)))
|
2019-01-10 15:46:20 +03:00
|
|
|
if err != nil {
|
2019-01-11 03:05:20 +03:00
|
|
|
return 0, err
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
n, err := io.ReadFull(c.link.conn, buf)
|
2019-01-10 15:46:20 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:46:46 +03:00
|
|
|
c.log(DebugLevel, pkg+"read failed", "error", err.Error())
|
2019-01-11 03:05:20 +03:00
|
|
|
return 0, err
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
c.nBytesIn += uint32(n)
|
|
|
|
if c.nBytesIn > (c.nBytesInSent + c.clientBW/10) {
|
|
|
|
err := sendBytesReceived(c)
|
2019-01-10 15:46:20 +03:00
|
|
|
if err != nil {
|
2019-01-11 03:05:20 +03:00
|
|
|
return n, err // NB: we still read n bytes, even though send bytes failed
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-11 03:05:20 +03:00
|
|
|
return n, nil
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// write to an RTMP connection.
|
2019-01-19 05:46:46 +03:00
|
|
|
func (c *Conn) write(buf []byte) (int, error) {
|
2019-01-10 15:46:20 +03:00
|
|
|
//ToDo: consider using a different timeout for writes than for reads
|
2019-01-19 05:46:46 +03:00
|
|
|
err := c.link.conn.SetWriteDeadline(time.Now().Add(time.Second * time.Duration(c.link.timeout)))
|
2019-01-10 15:46:20 +03:00
|
|
|
if err != nil {
|
2019-01-11 03:05:20 +03:00
|
|
|
return 0, err
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
2019-01-19 05:46:46 +03:00
|
|
|
n, err := c.link.conn.Write(buf)
|
2019-01-10 15:46:20 +03:00
|
|
|
if err != nil {
|
2019-01-19 05:46:46 +03:00
|
|
|
c.log(WarnLevel, pkg+"write failed", "error", err.Error())
|
2019-01-11 03:05:20 +03:00
|
|
|
return 0, err
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
2019-01-11 03:05:20 +03:00
|
|
|
return n, nil
|
2019-01-10 15:46:20 +03:00
|
|
|
}
|
|
|
|
|
2019-01-07 08:50:35 +03:00
|
|
|
// isConnected returns true if the RTMP connection is up.
|
2019-01-19 05:46:46 +03:00
|
|
|
func (c *Conn) isConnected() bool {
|
|
|
|
return c.link.conn != nil
|
2019-01-07 08:50:35 +03:00
|
|
|
}
|