Adds RTSP client custom keepalive timeout

This commit is contained in:
Alexey Khit
2023-04-17 16:54:02 +03:00
parent 6cef5faf27
commit 76ec70d2a0
2 changed files with 21 additions and 14 deletions
+3 -3
View File
@@ -246,10 +246,10 @@ func (c *Conn) SetupMedia(media *core.Media) (byte, error) {
if c.session == "" { if c.session == "" {
// Session: 216525287999;timeout=60 // Session: 216525287999;timeout=60
if s := res.Header.Get("Session"); s != "" { if s := res.Header.Get("Session"); s != "" {
if j := strings.IndexByte(s, ';'); j > 0 { c.session, s, _ = strings.Cut(s, ";timeout=")
s = s[:j] if s != "" {
c.keepalive, _ = strconv.Atoi(s)
} }
c.session = s
} }
} }
+18 -11
View File
@@ -30,13 +30,14 @@ type Conn struct {
// internal // internal
auth *tcp.Auth auth *tcp.Auth
conn net.Conn conn net.Conn
mode core.Mode keepalive int
reader *bufio.Reader mode core.Mode
sequence int reader *bufio.Reader
session string sequence int
uri string session string
uri string
state State state State
stateMu sync.Mutex stateMu sync.Mutex
@@ -89,11 +90,17 @@ const (
func (c *Conn) Handle() (err error) { func (c *Conn) Handle() (err error) {
var timeout time.Duration var timeout time.Duration
var keepalive time.Time var keepaliveDT time.Duration
var keepaliveTS time.Time
switch c.mode { switch c.mode {
case core.ModeActiveProducer: 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) // polling frames from remote RTSP Server (ex Camera)
if len(c.receivers) > 0 { if len(c.receivers) > 0 {
@@ -237,13 +244,13 @@ func (c *Conn) Handle() (err error) {
c.Fire(msg) c.Fire(msg)
} }
if !keepalive.IsZero() && ts.After(keepalive) { if keepaliveDT != 0 && ts.After(keepaliveTS) {
req := &tcp.Request{Method: MethodOptions, URL: c.URL} req := &tcp.Request{Method: MethodOptions, URL: c.URL}
if err = c.WriteRequest(req); err != nil { if err = c.WriteRequest(req); err != nil {
return return
} }
keepalive = ts.Add(time.Second * 25) keepaliveTS = ts.Add(keepaliveDT)
} }
} }