protocol/rtcp: using defer where I can

This commit is contained in:
Saxon 2019-04-16 22:01:38 +09:30
parent f66a94543a
commit 881ddc3d38
2 changed files with 9 additions and 15 deletions

View File

@ -243,11 +243,9 @@ func (c *client) UpdateSequence(s uint32) {
// highestSequence will return the highest sequence number received through RTP. // highestSequence will return the highest sequence number received through RTP.
func (c *client) highestSequence() uint32 { func (c *client) highestSequence() uint32 {
var s uint32
c.mu.Lock() c.mu.Lock()
s = c.sequence defer c.mu.Unlock()
c.mu.Unlock() return c.sequence
return s
} }
// jitter returns the interarrival jitter as described by RTCP specifications: // jitter returns the interarrival jitter as described by RTCP specifications:
@ -259,34 +257,30 @@ func (c *client) jitter() uint32 {
// setSenderTs allows us to safely set the current sender report timestamp. // setSenderTs allows us to safely set the current sender report timestamp.
func (c *client) setSenderTs(msw, lsw uint32) { func (c *client) setSenderTs(msw, lsw uint32) {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock()
binary.BigEndian.PutUint32(c.senderTs[:], msw) binary.BigEndian.PutUint32(c.senderTs[:], msw)
binary.BigEndian.PutUint32(c.senderTs[4:], lsw) binary.BigEndian.PutUint32(c.senderTs[4:], lsw)
c.mu.Unlock()
} }
// lastSenderTs returns the timestamp of the most recent sender report. // lastSenderTs returns the timestamp of the most recent sender report.
func (c *client) lastSenderTs() uint32 { func (c *client) lastSenderTs() uint32 {
var ts uint32
c.mu.Lock() c.mu.Lock()
ts = binary.BigEndian.Uint32(c.senderTs[2:]) defer c.mu.Unlock()
c.mu.Unlock() return binary.BigEndian.Uint32(c.senderTs[2:])
return ts
} }
// delay returns the duration between the receive time of the last sender report // delay returns the duration between the receive time of the last sender report
// and now. This is called when forming a receiver report. // and now. This is called when forming a receiver report.
func (c *client) delay() uint32 { func (c *client) delay() uint32 {
var receiveTime time.Time
c.mu.Lock() c.mu.Lock()
receiveTime = c.receiveTime defer c.mu.Unlock()
c.mu.Unlock()
now := time.Now() now := time.Now()
return uint32(now.Sub(receiveTime).Seconds() / delayUnit) return uint32(now.Sub(c.receiveTime).Seconds() / delayUnit)
} }
// received is called when a sender report is received to mark the receive time. // received is called when a sender report is received to mark the receive time.
func (c *client) received() { func (c *client) received() {
c.mu.Lock() c.mu.Lock()
defer c.mu.Unlock()
c.receiveTime = time.Now() c.receiveTime = time.Now()
c.mu.Unlock()
} }

View File

@ -39,7 +39,7 @@ func Timestamp(buf []byte) (msw, lsw uint32, err error) {
if len(buf) < 4 { if len(buf) < 4 {
return 0, 0, errors.New("bad RTCP packet, not of sufficient length") return 0, 0, errors.New("bad RTCP packet, not of sufficient length")
} }
if (buf[0]&0xc0)>>6 != rtpVer { if (buf[0]&0xc0)>>6 != rtcpVer {
return 0, 0, errors.New("incompatible RTCP version") return 0, 0, errors.New("incompatible RTCP version")
} }