From 3b938228138ed630d1b763f922551b88975b173f Mon Sep 17 00:00:00 2001 From: et Date: Thu, 29 Dec 2022 16:39:58 +0800 Subject: [PATCH] rtmps --- client.go | 19 ++++++++++++++++--- netConnection.go | 2 +- server.go | 9 ++++++--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/client.go b/client.go index 1261854..397e5b3 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package rtmp import ( "bufio" + "crypto/tls" "errors" "net" "net/url" @@ -18,16 +19,28 @@ func NewRTMPClient(addr string) (client *NetConnection, err error) { RTMPPlugin.Error("connect url parse", zap.Error(err)) return nil, err } + isRtmps := u.Scheme == "rtmps" 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 { RTMPPlugin.Error("dial tcp", zap.String("host", u.Host), zap.Error(err)) return nil, err } client = &NetConnection{ - TCPConn: conn.(*net.TCPConn), + Conn: conn, Reader: bufio.NewReader(conn), writeChunkSize: conf.ChunkSize, readChunkSize: RTMP_DEFAULT_CHUNK_SIZE, diff --git a/netConnection.go b/netConnection.go index e0a7e77..5113eac 100644 --- a/netConnection.go +++ b/netConnection.go @@ -76,7 +76,7 @@ func newPlayResponseMessageData(streamid uint32, code, level string) (amfobj AMF type NetConnection struct { *bufio.Reader `json:"-"` - *net.TCPConn `json:"-"` + net.Conn `json:"-"` bandwidth uint32 readSeqNum uint32 // 当前读的字节 writeSeqNum uint32 // 当前写的字节 diff --git a/server.go b/server.go index 9a88bfe..fd5d471 100644 --- a/server.go +++ b/server.go @@ -39,7 +39,7 @@ func (config *RTMPConfig) ServeTCP(conn *net.TCPConn) { senders := make(map[uint32]*RTMPSubscriber) receivers := make(map[uint32]*RTMPReceiver) nc := &NetConnection{ - TCPConn: conn, + Conn: conn, Reader: bufio.NewReader(conn), writeChunkSize: RTMP_DEFAULT_CHUNK_SIZE, readChunkSize: RTMP_DEFAULT_CHUNK_SIZE, @@ -71,8 +71,11 @@ func (config *RTMPConfig) ServeTCP(conn *net.TCPConn) { case *CallMessage: //connect app := cmd.Object["app"] // 客户端要连接到的服务应用名 objectEncoding := cmd.Object["objectEncoding"] // AMF编码方法 - if objectEncoding != nil { - nc.objectEncoding = objectEncoding.(float64) + switch v := objectEncoding.(type) { + case float64: + nc.objectEncoding = v + default: + nc.objectEncoding = 0 } nc.appName = app.(string) RTMPPlugin.Info("connect", zap.String("appName", nc.appName), zap.Float64("objectEncoding", nc.objectEncoding))