replace keyframe ticker with timer (#2661)
This commit is contained in:
@@ -19,6 +19,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"math"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -625,21 +626,21 @@ func (d *DownTrack) keyFrameRequester() {
|
|||||||
return time.Duration(interval) * time.Millisecond
|
return time.Duration(interval) * time.Millisecond
|
||||||
}
|
}
|
||||||
|
|
||||||
interval := getInterval()
|
timer := time.NewTimer(math.MaxInt64)
|
||||||
ticker := time.NewTicker(interval)
|
defer timer.Stop()
|
||||||
defer ticker.Stop()
|
|
||||||
|
|
||||||
for {
|
for !d.IsClosed() {
|
||||||
if d.IsClosed() {
|
if !timer.Stop() {
|
||||||
return
|
<-timer.C
|
||||||
}
|
}
|
||||||
|
timer.Reset(getInterval())
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case _, more := <-d.keyFrameRequesterCh:
|
case _, more := <-d.keyFrameRequesterCh:
|
||||||
if !more {
|
if !more {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
case <-ticker.C:
|
case <-timer.C:
|
||||||
}
|
}
|
||||||
|
|
||||||
locked, layer := d.forwarder.CheckSync()
|
locked, layer := d.forwarder.CheckSync()
|
||||||
@@ -648,8 +649,6 @@ func (d *DownTrack) keyFrameRequester() {
|
|||||||
d.params.Receiver.SendPLI(layer, false)
|
d.params.Receiver.SendPLI(layer, false)
|
||||||
d.rtpStats.UpdateLayerLockPliAndTime(1)
|
d.rtpStats.UpdateLayerLockPliAndTime(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker.Reset(getInterval())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+16
-20
@@ -30,50 +30,46 @@ type OpsQueueParams struct {
|
|||||||
Logger logger.Logger
|
Logger logger.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
type untypedOpsQueueOp func()
|
type UntypedQueueOp func()
|
||||||
|
|
||||||
func (it untypedOpsQueueOp) run() {
|
func (it UntypedQueueOp) run() {
|
||||||
it()
|
it()
|
||||||
}
|
}
|
||||||
|
|
||||||
type OpsQueue struct {
|
type OpsQueue struct {
|
||||||
OpsQueueBase[untypedOpsQueueOp]
|
opsQueueBase[UntypedQueueOp]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
||||||
return &OpsQueue{
|
return &OpsQueue{*newOpsQueueBase[UntypedQueueOp](params)}
|
||||||
OpsQueueBase: *newOpsQueueBase[untypedOpsQueueOp](params),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type typedOpsQueueOp[T any] struct {
|
type typedQueueOp[T any] struct {
|
||||||
fn func(T)
|
fn func(T)
|
||||||
arg T
|
arg T
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it typedOpsQueueOp[T]) run() {
|
func (it typedQueueOp[T]) run() {
|
||||||
it.fn(it.arg)
|
it.fn(it.arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TypedOpsQueue[T any] struct {
|
type TypedOpsQueue[T any] struct {
|
||||||
OpsQueueBase[typedOpsQueueOp[T]]
|
opsQueueBase[typedQueueOp[T]]
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTypedOpsQueue[T any](params OpsQueueParams) *TypedOpsQueue[T] {
|
func NewTypedOpsQueue[T any](params OpsQueueParams) *TypedOpsQueue[T] {
|
||||||
return &TypedOpsQueue[T]{
|
return &TypedOpsQueue[T]{*newOpsQueueBase[typedQueueOp[T]](params)}
|
||||||
OpsQueueBase: *newOpsQueueBase[typedOpsQueueOp[T]](params),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
|
func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
|
||||||
oq.OpsQueueBase.Enqueue(typedOpsQueueOp[T]{fn, arg})
|
oq.opsQueueBase.Enqueue(typedQueueOp[T]{fn, arg})
|
||||||
}
|
}
|
||||||
|
|
||||||
type opsQueueItem interface {
|
type opsQueueItem interface {
|
||||||
run()
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
type OpsQueueBase[T opsQueueItem] struct {
|
type opsQueueBase[T opsQueueItem] struct {
|
||||||
params OpsQueueParams
|
params OpsQueueParams
|
||||||
|
|
||||||
lock sync.Mutex
|
lock sync.Mutex
|
||||||
@@ -84,8 +80,8 @@ type OpsQueueBase[T opsQueueItem] struct {
|
|||||||
isStopped bool
|
isStopped bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *OpsQueueBase[T] {
|
func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *opsQueueBase[T] {
|
||||||
oq := &OpsQueueBase[T]{
|
oq := &opsQueueBase[T]{
|
||||||
params: params,
|
params: params,
|
||||||
wake: make(chan struct{}, 1),
|
wake: make(chan struct{}, 1),
|
||||||
doneChan: make(chan struct{}),
|
doneChan: make(chan struct{}),
|
||||||
@@ -94,7 +90,7 @@ func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *OpsQueueBase[T] {
|
|||||||
return oq
|
return oq
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oq *OpsQueueBase[T]) Start() {
|
func (oq *opsQueueBase[T]) Start() {
|
||||||
oq.lock.Lock()
|
oq.lock.Lock()
|
||||||
if oq.isStarted {
|
if oq.isStarted {
|
||||||
oq.lock.Unlock()
|
oq.lock.Unlock()
|
||||||
@@ -107,7 +103,7 @@ func (oq *OpsQueueBase[T]) Start() {
|
|||||||
go oq.process()
|
go oq.process()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oq *OpsQueueBase[T]) Stop() <-chan struct{} {
|
func (oq *opsQueueBase[T]) Stop() <-chan struct{} {
|
||||||
oq.lock.Lock()
|
oq.lock.Lock()
|
||||||
if oq.isStopped {
|
if oq.isStopped {
|
||||||
oq.lock.Unlock()
|
oq.lock.Unlock()
|
||||||
@@ -120,7 +116,7 @@ func (oq *OpsQueueBase[T]) Stop() <-chan struct{} {
|
|||||||
return oq.doneChan
|
return oq.doneChan
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oq *OpsQueueBase[T]) Enqueue(op T) {
|
func (oq *opsQueueBase[T]) Enqueue(op T) {
|
||||||
oq.lock.Lock()
|
oq.lock.Lock()
|
||||||
defer oq.lock.Unlock()
|
defer oq.lock.Unlock()
|
||||||
|
|
||||||
@@ -137,7 +133,7 @@ func (oq *OpsQueueBase[T]) Enqueue(op T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (oq *OpsQueueBase[T]) process() {
|
func (oq *opsQueueBase[T]) process() {
|
||||||
defer close(oq.doneChan)
|
defer close(oq.doneChan)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
|
|||||||
Reference in New Issue
Block a user