Split out stream tracker manager (#394)
* Random clean up * Split out StreamTrackerManager for re-use * Reset the correct tracker * use generation counter to exit coroutine * start only for video and when enabled * Add RemoveAllTrackers method
This commit is contained in:
+26
-42
@@ -4,6 +4,8 @@ import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type StreamStatus int32
|
||||
@@ -37,7 +39,6 @@ type StreamTracker struct {
|
||||
|
||||
paused atomicBool
|
||||
countSinceLast uint32 // number of packets received since last check
|
||||
running chan struct{}
|
||||
generation atomicUint32
|
||||
|
||||
initMu sync.Mutex
|
||||
@@ -51,6 +52,8 @@ type StreamTracker struct {
|
||||
|
||||
// only access by the same goroutine as Observe
|
||||
lastSN uint16
|
||||
|
||||
isStopped utils.AtomicFlag
|
||||
}
|
||||
|
||||
func NewStreamTracker(samplesRequired uint32, cyclesRequired uint64, cycleDuration time.Duration) *StreamTracker {
|
||||
@@ -74,41 +77,31 @@ func (s *StreamTracker) Status() StreamStatus {
|
||||
return s.status
|
||||
}
|
||||
|
||||
func (s *StreamTracker) maybeSetActive() {
|
||||
func (s *StreamTracker) maybeSetStatus(status StreamStatus) {
|
||||
changed := false
|
||||
s.statusMu.Lock()
|
||||
if s.status != StreamStatusActive {
|
||||
s.status = StreamStatusActive
|
||||
if s.status != status {
|
||||
s.status = status
|
||||
changed = true
|
||||
}
|
||||
s.statusMu.Unlock()
|
||||
|
||||
if changed && s.onStatusChanged != nil {
|
||||
s.onStatusChanged(StreamStatusActive)
|
||||
s.onStatusChanged(status)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamTracker) maybeSetStopped() {
|
||||
changed := false
|
||||
s.statusMu.Lock()
|
||||
if s.status != StreamStatusStopped {
|
||||
s.status = StreamStatusStopped
|
||||
changed = true
|
||||
}
|
||||
s.statusMu.Unlock()
|
||||
func (s *StreamTracker) maybeSetActive() {
|
||||
s.maybeSetStatus(StreamStatusActive)
|
||||
}
|
||||
|
||||
if changed && s.onStatusChanged != nil {
|
||||
s.onStatusChanged(StreamStatusStopped)
|
||||
}
|
||||
func (s *StreamTracker) maybeSetStopped() {
|
||||
s.maybeSetStatus(StreamStatusStopped)
|
||||
}
|
||||
|
||||
func (s *StreamTracker) init() {
|
||||
s.maybeSetActive()
|
||||
|
||||
if s.isRunning() {
|
||||
return
|
||||
}
|
||||
s.running = make(chan struct{})
|
||||
go s.detectWorker(s.generation.get())
|
||||
}
|
||||
|
||||
@@ -116,17 +109,23 @@ func (s *StreamTracker) Start() {
|
||||
}
|
||||
|
||||
func (s *StreamTracker) Stop() {
|
||||
if s.running != nil {
|
||||
close(s.running)
|
||||
s.running = nil
|
||||
if !s.isStopped.TrySet(true) {
|
||||
return
|
||||
}
|
||||
|
||||
// bump generation to trigger exit of worker
|
||||
s.generation.add(1)
|
||||
}
|
||||
|
||||
func (s *StreamTracker) Reset() {
|
||||
s.generation.add(1)
|
||||
s.Stop()
|
||||
if s.isStopped.Get() {
|
||||
return
|
||||
}
|
||||
|
||||
s.countSinceLast = 0
|
||||
// bump generation to trigger exit of current worker
|
||||
s.generation.add(1)
|
||||
|
||||
atomic.StoreUint32(&s.countSinceLast, 0)
|
||||
s.cycleCount = 0
|
||||
|
||||
s.initMu.Lock()
|
||||
@@ -142,18 +141,6 @@ func (s *StreamTracker) SetPaused(paused bool) {
|
||||
s.paused.set(paused)
|
||||
}
|
||||
|
||||
func (s *StreamTracker) isRunning() bool {
|
||||
if s.running == nil {
|
||||
return false
|
||||
}
|
||||
select {
|
||||
case <-s.running:
|
||||
return false
|
||||
default:
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// Observe a packet that's received
|
||||
func (s *StreamTracker) Observe(sn uint16) {
|
||||
if s.paused.get() {
|
||||
@@ -187,11 +174,8 @@ func (s *StreamTracker) Observe(sn uint16) {
|
||||
func (s *StreamTracker) detectWorker(generation uint32) {
|
||||
ticker := time.NewTicker(s.cycleDuration)
|
||||
|
||||
for s.isRunning() {
|
||||
for {
|
||||
<-ticker.C
|
||||
if !s.isRunning() {
|
||||
return
|
||||
}
|
||||
if generation != s.generation.get() {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user