mirror of
https://github.com/aler9/gortsplib
synced 2025-10-05 15:16:51 +08:00
client: rewrite inline functions as methods (#291)
This commit is contained in:
117
client.go
117
client.go
@@ -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,62 +839,60 @@ func (c *Client) do(req *base.Request, skipResponse bool, allowFrames bool) (*ba
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Client) atLeastOneUDPPacketHasBeenReceived() bool {
|
||||||
|
for _, ct := range c.medias {
|
||||||
|
lft := atomic.LoadInt64(ct.udpRTPListener.lastPacketTime)
|
||||||
|
if lft != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
lft = atomic.LoadInt64(ct.udpRTCPListener.lastPacketTime)
|
||||||
|
if lft != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) isInUDPTimeout() bool {
|
||||||
|
now := time.Now()
|
||||||
|
for _, ct := range c.medias {
|
||||||
|
lft := time.Unix(atomic.LoadInt64(ct.udpRTPListener.lastPacketTime), 0)
|
||||||
|
if now.Sub(lft) < c.ReadTimeout {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
lft = time.Unix(atomic.LoadInt64(ct.udpRTCPListener.lastPacketTime), 0)
|
||||||
|
if now.Sub(lft) < c.ReadTimeout {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Client) isInTCPTimeout() bool {
|
||||||
|
now := time.Now()
|
||||||
|
lft := time.Unix(atomic.LoadInt64(c.tcpLastFrameTime), 0)
|
||||||
|
return now.Sub(lft) >= c.ReadTimeout
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) checkTimeout() error {
|
func (c *Client) checkTimeout() error {
|
||||||
if *c.effectiveTransport == TransportUDP ||
|
if *c.effectiveTransport == TransportUDP ||
|
||||||
*c.effectiveTransport == TransportUDPMulticast {
|
*c.effectiveTransport == TransportUDPMulticast {
|
||||||
if c.checkTimeoutInitial {
|
if c.checkTimeoutInitial {
|
||||||
c.checkTimeoutInitial = false
|
c.checkTimeoutInitial = false
|
||||||
|
|
||||||
// check that at least one packet has been received
|
if c.atLeastOneUDPPacketHasBeenReceived() {
|
||||||
inTimeout := func() bool {
|
|
||||||
for _, ct := range c.medias {
|
|
||||||
lft := atomic.LoadInt64(ct.udpRTPListener.lastPacketTime)
|
|
||||||
if lft != 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
lft = atomic.LoadInt64(ct.udpRTCPListener.lastPacketTime)
|
|
||||||
if lft != 0 {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}()
|
|
||||||
if inTimeout {
|
|
||||||
err := c.trySwitchingProtocol()
|
err := c.trySwitchingProtocol()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else if c.isInUDPTimeout() {
|
||||||
inTimeout := func() bool {
|
return liberrors.ErrClientUDPTimeout{}
|
||||||
now := time.Now()
|
|
||||||
for _, ct := range c.medias {
|
|
||||||
lft := time.Unix(atomic.LoadInt64(ct.udpRTPListener.lastPacketTime), 0)
|
|
||||||
if now.Sub(lft) < c.ReadTimeout {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
lft = time.Unix(atomic.LoadInt64(ct.udpRTCPListener.lastPacketTime), 0)
|
|
||||||
if now.Sub(lft) < c.ReadTimeout {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}()
|
|
||||||
if inTimeout {
|
|
||||||
return liberrors.ErrClientUDPTimeout{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else { // TCP
|
|
||||||
inTimeout := func() bool {
|
|
||||||
now := time.Now()
|
|
||||||
lft := time.Unix(atomic.LoadInt64(c.tcpLastFrameTime), 0)
|
|
||||||
return now.Sub(lft) >= c.ReadTimeout
|
|
||||||
}()
|
|
||||||
if inTimeout {
|
|
||||||
return liberrors.ErrClientTCPTimeout{}
|
|
||||||
}
|
}
|
||||||
|
} 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
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user