From 26b5745f0ac95f5cc9c16b52e76640d77cf0c5b3 Mon Sep 17 00:00:00 2001 From: Alexey Khit Date: Mon, 22 Aug 2022 06:54:58 +0300 Subject: [PATCH] Adds keep-alive to RTSP connection --- pkg/rtsp/conn.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/pkg/rtsp/conn.go b/pkg/rtsp/conn.go index a86f0df8..ffd6b2e7 100644 --- a/pkg/rtsp/conn.go +++ b/pkg/rtsp/conn.go @@ -43,6 +43,8 @@ const ( ModeServerConsumer ) +const KeepAlive = time.Second * 25 + type Conn struct { streamer.Element @@ -575,6 +577,7 @@ func (c *Conn) Handle() (err error) { }() //c.Fire(streamer.StatePlaying) + ts := time.Now().Add(KeepAlive) for { // we can read: @@ -653,6 +656,17 @@ func (c *Conn) Handle() (err error) { c.Fire(msg) } + + // keep-alive + now := time.Now() + if now.After(ts) { + req := &tcp.Request{Method: MethodOptions, URL: c.URL} + // don't need to wait respose on this request + if err = c.Request(req); err != nil { + return err + } + ts = now.Add(KeepAlive) + } } }