fix: retry with and without backchannel, some cameras might fail on this if not supported

This commit is contained in:
Cedric Verstraeten
2023-11-20 09:21:57 +01:00
parent 474f81bfc4
commit 390060fd03
3 changed files with 24 additions and 20 deletions

View File

@@ -60,7 +60,7 @@ type RegisterHandler struct {
ReaderDemuxer func(io.Reader) av.Demuxer
WriterMuxer func(io.Writer) av.Muxer
UrlMuxer func(string) (bool, av.MuxCloser, error)
UrlDemuxer func(string) (bool, av.DemuxCloser, error)
UrlDemuxer func(string, bool) (bool, av.DemuxCloser, error)
UrlReader func(string) (bool, io.ReadCloser, error)
Probe func([]byte) bool
AudioEncoder func(av.CodecType) (av.AudioEncoder, error)
@@ -126,7 +126,7 @@ func (self *Handlers) NewAudioDecoder(codec av.AudioCodecData) (dec av.AudioDeco
return
}
func (self *Handlers) Open(ctx context.Context, uri string) (demuxer av.DemuxCloser, err error) {
func (self *Handlers) Open(ctx context.Context, uri string, withBackChannel bool) (demuxer av.DemuxCloser, err error) {
listen := false
if strings.HasPrefix(uri, "listen:") {
uri = uri[len("listen:"):]
@@ -144,7 +144,7 @@ func (self *Handlers) Open(ctx context.Context, uri string) (demuxer av.DemuxClo
} else {
if handler.UrlDemuxer != nil {
var ok bool
if ok, demuxer, err = handler.UrlDemuxer(uri); ok {
if ok, demuxer, err = handler.UrlDemuxer(uri, withBackChannel); ok {
return
}
}
@@ -269,8 +269,8 @@ func (self *Handlers) FindCreate(uri string) (handler RegisterHandler, muxer av.
var DefaultHandlers = &Handlers{}
func Open(ctx context.Context, url string) (demuxer av.DemuxCloser, err error) {
return DefaultHandlers.Open(ctx, url)
func Open(ctx context.Context, url string, withBackChannel bool) (demuxer av.DemuxCloser, err error) {
return DefaultHandlers.Open(ctx, url, withBackChannel)
}
func Create(url string) (muxer av.MuxCloser, err error) {

View File

@@ -1652,7 +1652,7 @@ func (self closeConn) Close() error {
}
func Handler(h *avutil.RegisterHandler) {
h.UrlDemuxer = func(uri string) (ok bool, demuxer av.DemuxCloser, err error) {
h.UrlDemuxer = func(uri string, withBackChannel bool) (ok bool, demuxer av.DemuxCloser, err error) {
if !strings.HasPrefix(uri, "rtmp://") {
return
}

View File

@@ -43,9 +43,10 @@ const (
)
type Client struct {
DebugRtsp bool
DebugRtp bool
Headers []string
DebugRtsp bool
DebugRtp bool
Headers []string
WithBackChannel bool
SkipErrRtpBlock bool
@@ -89,7 +90,7 @@ type Response struct {
Block []byte
}
func DialTimeout(uri string, timeout time.Duration) (self *Client, err error) {
func DialTimeout(uri string, timeout time.Duration, withBackChannel bool) (self *Client, err error) {
var URL *url.URL
if URL, err = url.Parse(uri); err != nil {
return
@@ -121,12 +122,13 @@ func DialTimeout(uri string, timeout time.Duration) (self *Client, err error) {
RtpKeepAliveTimeout: 10 * time.Second,
RtspTimeout: 10 * time.Second,
RtpTimeout: 10 * time.Second,
WithBackChannel: withBackChannel,
}
return
}
func Dial(uri string) (self *Client, err error) {
return DialTimeout(uri, time.Second*5)
return DialTimeout(uri, time.Second*5, false)
}
func (self *Client) allCodecDataReady() bool {
@@ -219,8 +221,6 @@ func (self *Client) SendRtpKeepalive() (err error) {
Uri: self.requestUri,
Header: []string{
"User-Agent: Kerberos.io",
//"Accept: application/sdp",
//"Require: www.onvif.org/ver20/backchannel",
},
}
if self.session != "" {
@@ -686,15 +686,19 @@ func md5hash(s string) string {
func (self *Client) Describe() (streams []sdp.Media, err error) {
var res Response
headers := []string{
"User-Agent: Kerberos.io",
"Accept: application/sdp",
}
if self.WithBackChannel {
headers = append(headers, "Require: www.onvif.org/ver20/backchannel")
}
for i := 0; i < 2; i++ {
req := Request{
Method: "DESCRIBE",
Uri: self.requestUri,
Header: []string{
"User-Agent: Kerberos.io",
"Accept: application/sdp",
"Require: www.onvif.org/ver20/backchannel",
},
Header: headers,
}
if err = self.WriteRequest(req); err != nil {
return
@@ -1374,12 +1378,12 @@ func (self *Client) WriteRaw(bytes []byte) (err error) {
}
func Handler(h *avutil.RegisterHandler) {
h.UrlDemuxer = func(uri string) (ok bool, demuxer av.DemuxCloser, err error) {
h.UrlDemuxer = func(uri string, withBackChannel bool) (ok bool, demuxer av.DemuxCloser, err error) {
if !strings.HasPrefix(uri, "rtsp://") {
return
}
// demuxer, err = Dial(uri)
demuxer, err = DialTimeout(uri, 15*time.Second)
demuxer, err = DialTimeout(uri, 15*time.Second, withBackChannel)
ok = err == nil
return