mirror of
https://github.com/Monibuca/plugin-rtmp.git
synced 2025-09-27 03:58:28 +08:00
19
client.go
19
client.go
@@ -2,6 +2,7 @@ package rtmp
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
"net"
|
"net"
|
||||||
"net/url"
|
"net/url"
|
||||||
@@ -18,16 +19,28 @@ func NewRTMPClient(addr string) (client *NetConnection, err error) {
|
|||||||
RTMPPlugin.Error("connect url parse", zap.Error(err))
|
RTMPPlugin.Error("connect url parse", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
isRtmps := u.Scheme == "rtmps"
|
||||||
if strings.Count(u.Host, ":") == 0 {
|
if strings.Count(u.Host, ":") == 0 {
|
||||||
u.Host += ":1935"
|
if isRtmps {
|
||||||
|
u.Host += ":443"
|
||||||
|
} else {
|
||||||
|
u.Host += ":1935"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
var conn net.Conn
|
||||||
|
if isRtmps {
|
||||||
|
var tlsconn *tls.Conn
|
||||||
|
tlsconn, err = tls.Dial("tcp", u.Host, &tls.Config{})
|
||||||
|
conn = tlsconn
|
||||||
|
} else {
|
||||||
|
conn, err = net.Dial("tcp", u.Host)
|
||||||
}
|
}
|
||||||
conn, err := net.Dial("tcp", u.Host)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
RTMPPlugin.Error("dial tcp", zap.String("host", u.Host), zap.Error(err))
|
RTMPPlugin.Error("dial tcp", zap.String("host", u.Host), zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
client = &NetConnection{
|
client = &NetConnection{
|
||||||
TCPConn: conn.(*net.TCPConn),
|
Conn: conn,
|
||||||
Reader: bufio.NewReader(conn),
|
Reader: bufio.NewReader(conn),
|
||||||
writeChunkSize: conf.ChunkSize,
|
writeChunkSize: conf.ChunkSize,
|
||||||
readChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
readChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
||||||
|
@@ -76,7 +76,7 @@ func newPlayResponseMessageData(streamid uint32, code, level string) (amfobj AMF
|
|||||||
|
|
||||||
type NetConnection struct {
|
type NetConnection struct {
|
||||||
*bufio.Reader `json:"-"`
|
*bufio.Reader `json:"-"`
|
||||||
*net.TCPConn `json:"-"`
|
net.Conn `json:"-"`
|
||||||
bandwidth uint32
|
bandwidth uint32
|
||||||
readSeqNum uint32 // 当前读的字节
|
readSeqNum uint32 // 当前读的字节
|
||||||
writeSeqNum uint32 // 当前写的字节
|
writeSeqNum uint32 // 当前写的字节
|
||||||
|
@@ -39,7 +39,7 @@ func (config *RTMPConfig) ServeTCP(conn *net.TCPConn) {
|
|||||||
senders := make(map[uint32]*RTMPSubscriber)
|
senders := make(map[uint32]*RTMPSubscriber)
|
||||||
receivers := make(map[uint32]*RTMPReceiver)
|
receivers := make(map[uint32]*RTMPReceiver)
|
||||||
nc := &NetConnection{
|
nc := &NetConnection{
|
||||||
TCPConn: conn,
|
Conn: conn,
|
||||||
Reader: bufio.NewReader(conn),
|
Reader: bufio.NewReader(conn),
|
||||||
writeChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
writeChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
||||||
readChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
readChunkSize: RTMP_DEFAULT_CHUNK_SIZE,
|
||||||
@@ -71,8 +71,11 @@ func (config *RTMPConfig) ServeTCP(conn *net.TCPConn) {
|
|||||||
case *CallMessage: //connect
|
case *CallMessage: //connect
|
||||||
app := cmd.Object["app"] // 客户端要连接到的服务应用名
|
app := cmd.Object["app"] // 客户端要连接到的服务应用名
|
||||||
objectEncoding := cmd.Object["objectEncoding"] // AMF编码方法
|
objectEncoding := cmd.Object["objectEncoding"] // AMF编码方法
|
||||||
if objectEncoding != nil {
|
switch v := objectEncoding.(type) {
|
||||||
nc.objectEncoding = objectEncoding.(float64)
|
case float64:
|
||||||
|
nc.objectEncoding = v
|
||||||
|
default:
|
||||||
|
nc.objectEncoding = 0
|
||||||
}
|
}
|
||||||
nc.appName = app.(string)
|
nc.appName = app.(string)
|
||||||
RTMPPlugin.Info("connect", zap.String("appName", nc.appName), zap.Float64("objectEncoding", nc.objectEncoding))
|
RTMPPlugin.Info("connect", zap.String("appName", nc.appName), zap.Float64("objectEncoding", nc.objectEncoding))
|
||||||
|
Reference in New Issue
Block a user