Fix busy RTSP backchannel

This commit is contained in:
Alexey Khit
2022-08-22 15:41:19 +03:00
parent a9af245ef8
commit 12b712426d
3 changed files with 27 additions and 11 deletions

View File

@@ -65,8 +65,17 @@ func rtspHandler(url string) (streamer.Producer, error) {
if err = conn.Dial(); err != nil { if err = conn.Dial(); err != nil {
return nil, err return nil, err
} }
conn.Backchannel = true
if err = conn.Describe(); err != nil { if err = conn.Describe(); err != nil {
return nil, err // second try without backchannel, we need to reconnect
if err = conn.Dial(); err != nil {
return nil, err
}
conn.Backchannel = false
if err = conn.Describe(); err != nil {
return nil, err
}
} }
return conn, nil return conn, nil

View File

@@ -50,6 +50,8 @@ type Conn struct {
// public // public
Backchannel bool
Medias []*streamer.Media Medias []*streamer.Media
Session string Session string
UserAgent string UserAgent string
@@ -106,6 +108,9 @@ func (c *Conn) Dial() (err error) {
//if c.state != StateClientInit { //if c.state != StateClientInit {
// panic("wrong state") // panic("wrong state")
//} //}
if c.conn != nil && c.auth != nil {
c.auth.Reset()
}
c.conn, err = net.DialTimeout( c.conn, err = net.DialTimeout(
"tcp", c.URL.Host, 10*time.Second, "tcp", c.URL.Host, 10*time.Second,
@@ -258,21 +263,17 @@ func (c *Conn) Describe() error {
Method: MethodDescribe, Method: MethodDescribe,
URL: c.URL, URL: c.URL,
Header: map[string][]string{ Header: map[string][]string{
"Accept": {"application/sdp"}, "Accept": {"application/sdp"},
"Require": {"www.onvif.org/ver20/backchannel"},
}, },
} }
if c.Backchannel {
req.Header.Set("Require", "www.onvif.org/ver20/backchannel")
}
res, err := c.Do(req) res, err := c.Do(req)
if err != nil { if err != nil {
if res != nil { return err
// if we have answer - give second chanse without onvif header
req.Header.Del("Require")
res, err = c.Do(req)
}
if err != nil {
return err
}
} }
if val := res.Header.Get("Content-Base"); val != "" { if val := res.Header.Get("Content-Base"); val != "" {

View File

@@ -80,6 +80,12 @@ func (a *Auth) Write(req *Request) {
} }
} }
func (a *Auth) Reset() {
if a.Method == AuthDigest {
a.Method = AuthUnknown
}
}
func Between(s, sub1, sub2 string) string { func Between(s, sub1, sub2 string) string {
i := strings.Index(s, sub1) i := strings.Index(s, sub1)
if i < 0 { if i < 0 {