diff --git a/pkg/rtsp/client.go b/pkg/rtsp/client.go index 4ed228f0..68e06d10 100644 --- a/pkg/rtsp/client.go +++ b/pkg/rtsp/client.go @@ -246,10 +246,10 @@ func (c *Conn) SetupMedia(media *core.Media) (byte, error) { if c.session == "" { // Session: 216525287999;timeout=60 if s := res.Header.Get("Session"); s != "" { - if j := strings.IndexByte(s, ';'); j > 0 { - s = s[:j] + c.session, s, _ = strings.Cut(s, ";timeout=") + if s != "" { + c.keepalive, _ = strconv.Atoi(s) } - c.session = s } } diff --git a/pkg/rtsp/conn.go b/pkg/rtsp/conn.go index 69e67faa..1a00ac8b 100644 --- a/pkg/rtsp/conn.go +++ b/pkg/rtsp/conn.go @@ -30,13 +30,14 @@ type Conn struct { // internal - auth *tcp.Auth - conn net.Conn - mode core.Mode - reader *bufio.Reader - sequence int - session string - uri string + auth *tcp.Auth + conn net.Conn + keepalive int + mode core.Mode + reader *bufio.Reader + sequence int + session string + uri string state State stateMu sync.Mutex @@ -89,11 +90,17 @@ const ( func (c *Conn) Handle() (err error) { var timeout time.Duration - var keepalive time.Time + var keepaliveDT time.Duration + var keepaliveTS time.Time switch c.mode { case core.ModeActiveProducer: - keepalive = time.Now().Add(time.Second * 25) + if c.keepalive > 5 { + keepaliveDT = time.Duration(c.keepalive-5) * time.Second + } else { + keepaliveDT = 25 * time.Second + } + keepaliveTS = time.Now().Add(keepaliveDT) // polling frames from remote RTSP Server (ex Camera) if len(c.receivers) > 0 { @@ -237,13 +244,13 @@ func (c *Conn) Handle() (err error) { c.Fire(msg) } - if !keepalive.IsZero() && ts.After(keepalive) { + if keepaliveDT != 0 && ts.After(keepaliveTS) { req := &tcp.Request{Method: MethodOptions, URL: c.URL} if err = c.WriteRequest(req); err != nil { return } - keepalive = ts.Add(time.Second * 25) + keepaliveTS = ts.Add(keepaliveDT) } }