From 8163c7a520f2f557e58cd2b8905167112a929665 Mon Sep 17 00:00:00 2001 From: Alex X Date: Sun, 17 Sep 2023 14:11:47 +0300 Subject: [PATCH] Update tcp.Dial func --- pkg/rtmp/producer.go | 2 +- pkg/rtsp/client.go | 2 +- pkg/tcp/dial.go | 15 ++++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/rtmp/producer.go b/pkg/rtmp/producer.go index 45665aa6..bbded280 100644 --- a/pkg/rtmp/producer.go +++ b/pkg/rtmp/producer.go @@ -14,7 +14,7 @@ func Dial(rawURL string) (core.Producer, error) { return nil, err } - conn, err := tcp.Dial(u, "1935", core.ConnDialTimeout) + conn, err := tcp.Dial(u, core.ConnDialTimeout) if err != nil { return nil, err } diff --git a/pkg/rtsp/client.go b/pkg/rtsp/client.go index 186d69bb..37d30950 100644 --- a/pkg/rtsp/client.go +++ b/pkg/rtsp/client.go @@ -35,7 +35,7 @@ func (c *Conn) Dial() (err error) { if c.Timeout != 0 { timeout = time.Second * time.Duration(c.Timeout) } - conn, err = tcp.Dial(c.URL, "554", timeout) + conn, err = tcp.Dial(c.URL, timeout) } else { conn, err = websocket.Dial(c.Transport) } diff --git a/pkg/tcp/dial.go b/pkg/tcp/dial.go index 9126190c..447d4d38 100644 --- a/pkg/tcp/dial.go +++ b/pkg/tcp/dial.go @@ -10,13 +10,22 @@ import ( ) // Dial - for RTSP(S|X) and RTMP(S|X) -func Dial(u *url.URL, port string, timeout time.Duration) (net.Conn, error) { +func Dial(u *url.URL, timeout time.Duration) (net.Conn, error) { + var address string var hostname string // without port if i := strings.IndexByte(u.Host, ':'); i > 0 { + address = u.Host hostname = u.Host[:i] } else { + switch u.Scheme { + case "rtsp", "rtsps", "rtspx": + address = u.Host + ":554" + case "rtmp": + address = u.Host + ":1935" + case "rtmps", "rtmpx": + address = u.Host + ":443" + } hostname = u.Host - u.Host += ":" + port } var secure *tls.Config @@ -33,7 +42,7 @@ func Dial(u *url.URL, port string, timeout time.Duration) (net.Conn, error) { return nil, errors.New("unsupported scheme: " + u.Scheme) } - conn, err := net.DialTimeout("tcp", u.Host, timeout) + conn, err := net.DialTimeout("tcp", address, timeout) if err != nil { return nil, err }