Skip to content

Commit

Permalink
queue: remove defer
Browse files Browse the repository at this point in the history
  • Loading branch information
nareix committed Aug 25, 2016
1 parent 9697988 commit 1832092
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions av/pubsub/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,41 +49,43 @@ func NewQueue(streams []av.CodecData) *Queue {
// Set max buffered total packets duration.
func (self *Queue) SetMaxDuration(dur time.Duration) {
self.lock.Lock()
defer self.lock.Unlock()

self.maxdur = dur
for self.maxdur > 0 && len(self.pkts) >= 2 && self.pkts[len(self.pkts)-1].Time - self.pkts[0].Time > self.maxdur {
self.pkts = self.pkts[1:]
self.head++
}

self.lock.Unlock()
return
}

// Currently buffered packets total duration.
func (self *Queue) Duration() (dur time.Duration) {
self.lock.RLock()
defer self.lock.RUnlock()

if len(self.pkts) >= 2 {
dur = self.pkts[len(self.pkts)-1].Time - self.pkts[0].Time
}

self.lock.RUnlock()
return
}

// After Close() called, all QueueCursor's ReadPacket will return io.EOF.
func (self *Queue) Close() (err error) {
self.lock.Lock()
defer self.lock.Unlock()

self.closed = true
self.cond.Broadcast()

self.lock.Unlock()
return
}

// Put packet into buffer, old packets will be discared.
func (self *Queue) WritePacket(pkt av.Packet) (err error) {
self.lock.Lock()
defer self.lock.Unlock()

if self.maxdur > 0 && len(self.pkts) >= 2 && self.pkts[len(self.pkts)-1].Time - self.pkts[0].Time > self.maxdur {
self.pkts = self.pkts[1:]
Expand All @@ -92,6 +94,8 @@ func (self *Queue) WritePacket(pkt av.Packet) (err error) {
self.pkts = append(self.pkts, pkt)
self.tail++
self.cond.Broadcast()

self.lock.Unlock()
return
}

Expand Down Expand Up @@ -164,9 +168,8 @@ func (self *Queue) DelayedGopCount(n int) *QueueCursor {

func (self *QueueCursor) Streams() (streams []av.CodecData, err error) {
self.que.lock.RLock()
defer self.que.lock.RUnlock()

streams = self.que.streams
self.que.lock.RUnlock()
return
}

Expand Down

0 comments on commit 1832092

Please sign in to comment.