Skip to content

Commit

Permalink
Fix busy RTSP backchannel
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexxIT committed Aug 22, 2022
1 parent a9af245 commit 12b7124
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
11 changes: 10 additions & 1 deletion cmd/rtsp/rtsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,17 @@ func rtspHandler(url string) (streamer.Producer, error) {
if err = conn.Dial(); err != nil {
return nil, err
}

conn.Backchannel = true
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
Expand Down
21 changes: 11 additions & 10 deletions pkg/rtsp/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ type Conn struct {

// public

Backchannel bool

Medias []*streamer.Media
Session string
UserAgent string
Expand Down Expand Up @@ -106,6 +108,9 @@ func (c *Conn) Dial() (err error) {
//if c.state != StateClientInit {
// panic("wrong state")
//}
if c.conn != nil && c.auth != nil {
c.auth.Reset()
}

c.conn, err = net.DialTimeout(
"tcp", c.URL.Host, 10*time.Second,
Expand Down Expand Up @@ -258,21 +263,17 @@ func (c *Conn) Describe() error {
Method: MethodDescribe,
URL: c.URL,
Header: map[string][]string{
"Accept": {"application/sdp"},
"Require": {"www.onvif.org/ver20/backchannel"},
"Accept": {"application/sdp"},
},
}

if c.Backchannel {
req.Header.Set("Require", "www.onvif.org/ver20/backchannel")
}

res, err := c.Do(req)
if err != nil {
if res != nil {
// if we have answer - give second chanse without onvif header
req.Header.Del("Require")
res, err = c.Do(req)
}
if err != nil {
return err
}
return err
}

if val := res.Header.Get("Content-Base"); val != "" {
Expand Down
6 changes: 6 additions & 0 deletions pkg/tcp/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
i := strings.Index(s, sub1)
if i < 0 {
Expand Down

0 comments on commit 12b7124

Please sign in to comment.