Add support custom timeout for RTSP source

This commit is contained in:
Alexey Khit
2023-08-23 14:08:15 +03:00
parent b1c0a28366
commit 9f9dc7e844
6 changed files with 24 additions and 13 deletions
+1
View File
@@ -102,6 +102,7 @@ func rtspHandler(rawURL string) (core.Producer, error) {
query := streams.ParseQuery(rawQuery) query := streams.ParseQuery(rawQuery)
conn.Backchannel = query.Get("backchannel") == "1" conn.Backchannel = query.Get("backchannel") == "1"
conn.Media = query.Get("media") conn.Media = query.Get("media")
conn.Timeout = core.Atoi(query.Get("timeout"))
conn.Transport = query.Get("transport") conn.Transport = query.Get("transport")
} }
+3 -1
View File
@@ -63,7 +63,9 @@ func Between(s, sub1, sub2 string) string {
} }
func Atoi(s string) (i int) { func Atoi(s string) (i int) {
i, _ = strconv.Atoi(s) if s != "" {
i, _ = strconv.Atoi(s)
}
return return
} }
+1 -1
View File
@@ -14,7 +14,7 @@ func Dial(rawURL string) (core.Producer, error) {
return nil, err return nil, err
} }
conn, err := tcp.Dial(u, "1935") conn, err := tcp.Dial(u, "1935", core.ConnDialTimeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }
+5 -1
View File
@@ -31,7 +31,11 @@ func (c *Conn) Dial() (err error) {
var conn net.Conn var conn net.Conn
if c.Transport == "" { if c.Transport == "" {
conn, err = tcp.Dial(c.URL, "554") timeout := core.ConnDialTimeout
if c.Timeout != 0 {
timeout = time.Second * time.Duration(c.Timeout)
}
conn, err = tcp.Dial(c.URL, "554", timeout)
} else { } else {
conn, err = websocket.Dial(c.Transport) conn, err = websocket.Dial(c.Transport)
} }
+11 -6
View File
@@ -26,6 +26,7 @@ type Conn struct {
Media string Media string
PacketSize uint16 PacketSize uint16
SessionName string SessionName string
Timeout int
Transport string // custom transport support, ex. RTSP over WebSocket Transport string // custom transport support, ex. RTSP over WebSocket
Medias []*core.Media Medias []*core.Media
@@ -108,13 +109,17 @@ func (c *Conn) Handle() (err error) {
} }
keepaliveTS = time.Now().Add(keepaliveDT) keepaliveTS = time.Now().Add(keepaliveDT)
// polling frames from remote RTSP Server (ex Camera) if c.Timeout == 0 {
if len(c.receivers) > 0 { // polling frames from remote RTSP Server (ex Camera)
// if we receiving video/audio from camera if len(c.receivers) > 0 {
timeout = time.Second * 5 // if we receiving video/audio from camera
timeout = time.Second * 5
} else {
// if we only send audio to camera
timeout = time.Second * 30
}
} else { } else {
// if we only send audio to camera timeout = time.Second * time.Duration(c.Timeout)
timeout = time.Second * 30
} }
case core.ModePassiveProducer: case core.ModePassiveProducer:
+3 -4
View File
@@ -6,12 +6,11 @@ import (
"net" "net"
"net/url" "net/url"
"strings" "strings"
"time"
"github.com/AlexxIT/go2rtc/pkg/core"
) )
// Dial - for RTSP(S|X) and RTMP(S|X) // Dial - for RTSP(S|X) and RTMP(S|X)
func Dial(u *url.URL, port string) (net.Conn, error) { func Dial(u *url.URL, port string, timeout time.Duration) (net.Conn, error) {
var hostname string // without port var hostname string // without port
if i := strings.IndexByte(u.Host, ':'); i > 0 { if i := strings.IndexByte(u.Host, ':'); i > 0 {
hostname = u.Host[:i] hostname = u.Host[:i]
@@ -34,7 +33,7 @@ func Dial(u *url.URL, port string) (net.Conn, error) {
return nil, errors.New("unsupported scheme: " + u.Scheme) return nil, errors.New("unsupported scheme: " + u.Scheme)
} }
conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout) conn, err := net.DialTimeout("tcp", u.Host, timeout)
if err != nil { if err != nil {
return nil, err return nil, err
} }