client: rewrite inline functions as methods (#291)

This commit is contained in:
Alessandro Ros
2023-05-28 12:31:37 +02:00
committed by GitHub
parent 1e1e10b031
commit b50151a0d6

View File

@@ -76,6 +76,20 @@ func resetMediaControls(ms media.Medias) {
} }
} }
func supportsGetParameter(header base.Header) bool {
pub, ok := header["Public"]
if !ok || len(pub) != 1 {
return false
}
for _, m := range strings.Split(pub[0], ",") {
if base.Method(strings.Trim(m, " ")) == base.GetParameter {
return true
}
}
return false
}
type clientState int type clientState int
const ( const (
@@ -825,14 +839,7 @@ func (c *Client) do(req *base.Request, skipResponse bool, allowFrames bool) (*ba
return res, nil return res, nil
} }
func (c *Client) checkTimeout() error { func (c *Client) atLeastOneUDPPacketHasBeenReceived() bool {
if *c.effectiveTransport == TransportUDP ||
*c.effectiveTransport == TransportUDPMulticast {
if c.checkTimeoutInitial {
c.checkTimeoutInitial = false
// check that at least one packet has been received
inTimeout := func() bool {
for _, ct := range c.medias { for _, ct := range c.medias {
lft := atomic.LoadInt64(ct.udpRTPListener.lastPacketTime) lft := atomic.LoadInt64(ct.udpRTPListener.lastPacketTime)
if lft != 0 { if lft != 0 {
@@ -845,15 +852,9 @@ func (c *Client) checkTimeout() error {
} }
} }
return true return true
}()
if inTimeout {
err := c.trySwitchingProtocol()
if err != nil {
return err
} }
}
} else { func (c *Client) isInUDPTimeout() bool {
inTimeout := func() bool {
now := time.Now() now := time.Now()
for _, ct := range c.medias { for _, ct := range c.medias {
lft := time.Unix(atomic.LoadInt64(ct.udpRTPListener.lastPacketTime), 0) lft := time.Unix(atomic.LoadInt64(ct.udpRTPListener.lastPacketTime), 0)
@@ -867,20 +868,31 @@ func (c *Client) checkTimeout() error {
} }
} }
return true return true
}()
if inTimeout {
return liberrors.ErrClientUDPTimeout{}
} }
}
} else { // TCP func (c *Client) isInTCPTimeout() bool {
inTimeout := func() bool {
now := time.Now() now := time.Now()
lft := time.Unix(atomic.LoadInt64(c.tcpLastFrameTime), 0) lft := time.Unix(atomic.LoadInt64(c.tcpLastFrameTime), 0)
return now.Sub(lft) >= c.ReadTimeout return now.Sub(lft) >= c.ReadTimeout
}()
if inTimeout {
return liberrors.ErrClientTCPTimeout{}
} }
func (c *Client) checkTimeout() error {
if *c.effectiveTransport == TransportUDP ||
*c.effectiveTransport == TransportUDPMulticast {
if c.checkTimeoutInitial {
c.checkTimeoutInitial = false
if c.atLeastOneUDPPacketHasBeenReceived() {
err := c.trySwitchingProtocol()
if err != nil {
return err
}
}
} else if c.isInUDPTimeout() {
return liberrors.ErrClientUDPTimeout{}
}
} else if c.isInTCPTimeout() {
return liberrors.ErrClientTCPTimeout{}
} }
return nil return nil
@@ -921,7 +933,7 @@ func (c *Client) doOptions(u *url.URL) (*base.Response, error) {
if res.StatusCode != base.StatusOK { if res.StatusCode != base.StatusOK {
// since this method is not implemented by every RTSP server, // since this method is not implemented by every RTSP server,
// return only if status code is not 404 // return an error only if status code is not 404
if res.StatusCode == base.StatusNotFound { if res.StatusCode == base.StatusNotFound {
return res, nil return res, nil
} }
@@ -929,20 +941,7 @@ func (c *Client) doOptions(u *url.URL) (*base.Response, error) {
} }
c.optionsSent = true c.optionsSent = true
c.useGetParameter = supportsGetParameter(res.Header)
c.useGetParameter = func() bool {
pub, ok := res.Header["Public"]
if !ok || len(pub) != 1 {
return false
}
for _, m := range strings.Split(pub[0], ",") {
if base.Method(strings.Trim(m, " ")) == base.GetParameter {
return true
}
}
return false
}()
return res, nil return res, nil
} }