diff --git a/pkg/sfu/downtrack.go b/pkg/sfu/downtrack.go index eae06e6d8..0a7baf472 100644 --- a/pkg/sfu/downtrack.go +++ b/pkg/sfu/downtrack.go @@ -595,17 +595,25 @@ func (d *DownTrack) CloseWithFlush(flush bool) { d.rtpStats.Stop() d.logger.Debugw("rtp stats", "stats", d.rtpStats.ToString()) - if d.onMaxLayerChanged != nil && d.kind == webrtc.RTPCodecTypeVideo { - d.callbacksQueue.Enqueue(func() { - d.onMaxLayerChanged(d, InvalidLayerSpatial) - }) - } + if d.callbacksQueue.IsStarted() { + if d.kind == webrtc.RTPCodecTypeVideo { + d.callbacksQueue.Enqueue(func() { + if d.onMaxLayerChanged != nil { + d.onMaxLayerChanged(d, InvalidLayerSpatial) + } + }) + } - if d.onCloseHandler != nil { - d.callbacksQueue.Enqueue(d.onCloseHandler) - } + if d.onCloseHandler != nil { + d.callbacksQueue.Enqueue(d.onCloseHandler) + } - d.callbacksQueue.Stop() + d.callbacksQueue.Stop() + } else { + if d.onCloseHandler != nil { + d.onCloseHandler() + } + } d.stopKeyFrameRequester() }) } diff --git a/pkg/utils/opsqueue.go b/pkg/utils/opsqueue.go index 63fd2c863..473430a3a 100644 --- a/pkg/utils/opsqueue.go +++ b/pkg/utils/opsqueue.go @@ -13,6 +13,7 @@ type OpsQueue struct { lock sync.RWMutex ops chan func() + isStarted bool isStopped bool } @@ -30,6 +31,15 @@ func (oq *OpsQueue) SetLogger(logger logger.Logger) { } func (oq *OpsQueue) Start() { + oq.lock.Lock() + if oq.isStarted { + oq.lock.Unlock() + return + } + + oq.isStarted = true + oq.lock.Unlock() + go oq.process() } @@ -45,6 +55,13 @@ func (oq *OpsQueue) Stop() { oq.lock.Unlock() } +func (oq *OpsQueue) IsStarted() bool { + oq.lock.RLock() + defer oq.lock.RUnlock() + + return oq.isStarted +} + func (oq *OpsQueue) Enqueue(op func()) { oq.lock.RLock() if oq.isStopped {