@@ -168,7 +168,7 @@ type StreamAllocator struct {
|
||||
|
||||
state streamAllocatorState
|
||||
|
||||
eventsQueue *utils.OpsQueue
|
||||
eventsQueue *utils.TypedOpsQueue[Event]
|
||||
|
||||
isStopped atomic.Bool
|
||||
}
|
||||
@@ -182,7 +182,7 @@ func NewStreamAllocator(params StreamAllocatorParams) *StreamAllocator {
|
||||
}),
|
||||
// STREAM-ALLOCATOR-DATA rateMonitor: NewRateMonitor(),
|
||||
videoTracks: make(map[livekit.TrackID]*Track),
|
||||
eventsQueue: utils.NewOpsQueue(utils.OpsQueueParams{
|
||||
eventsQueue: utils.NewTypedOpsQueue[Event](utils.OpsQueueParams{
|
||||
Name: "stream-allocator",
|
||||
MinSize: 64,
|
||||
Logger: params.Logger,
|
||||
@@ -559,9 +559,7 @@ func (s *StreamAllocator) maybePostEventAllocateTrack(downTrack *sfu.DownTrack)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) postEvent(event Event) {
|
||||
s.eventsQueue.Enqueue(func() {
|
||||
s.handleEvent(&event)
|
||||
})
|
||||
s.eventsQueue.Enqueue(s.handleEvent, event)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) ping() {
|
||||
@@ -580,7 +578,7 @@ func (s *StreamAllocator) ping() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleEvent(event *Event) {
|
||||
func (s *StreamAllocator) handleEvent(event Event) {
|
||||
switch event.Signal {
|
||||
case streamAllocatorSignalAllocateTrack:
|
||||
s.handleSignalAllocateTrack(event)
|
||||
@@ -611,7 +609,7 @@ func (s *StreamAllocator) handleEvent(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalAllocateTrack(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalAllocateTrack(event Event) {
|
||||
s.videoTracksMu.Lock()
|
||||
track := s.videoTracks[event.TrackID]
|
||||
if track != nil {
|
||||
@@ -624,7 +622,7 @@ func (s *StreamAllocator) handleSignalAllocateTrack(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalAllocateAllTracks(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalAllocateAllTracks(Event) {
|
||||
s.videoTracksMu.Lock()
|
||||
s.isAllocateAllPending = false
|
||||
s.videoTracksMu.Unlock()
|
||||
@@ -634,11 +632,11 @@ func (s *StreamAllocator) handleSignalAllocateAllTracks(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalAdjustState(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalAdjustState(Event) {
|
||||
s.adjustState()
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalEstimate(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalEstimate(event Event) {
|
||||
receivedEstimate, _ := event.Data.(int64)
|
||||
s.lastReceivedEstimate = receivedEstimate
|
||||
// s.monitorRate(receivedEstimate)
|
||||
@@ -651,7 +649,7 @@ func (s *StreamAllocator) handleSignalEstimate(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalPeriodicPing(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalPeriodicPing(Event) {
|
||||
// finalize probe if necessary
|
||||
trend, _ := s.channelObserver.GetTrend()
|
||||
isHandled, isNotFailing, isGoalReached := s.probeController.MaybeFinalizeProbe(
|
||||
@@ -671,7 +669,7 @@ func (s *StreamAllocator) handleSignalPeriodicPing(event *Event) {
|
||||
// s.updateTracksHistory()
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalSendProbe(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalSendProbe(event Event) {
|
||||
bytesToSend := event.Data.(int)
|
||||
if bytesToSend <= 0 {
|
||||
return
|
||||
@@ -692,12 +690,12 @@ func (s *StreamAllocator) handleSignalSendProbe(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalProbeClusterDone(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalProbeClusterDone(event Event) {
|
||||
info, _ := event.Data.(ProbeClusterInfo)
|
||||
s.probeController.ProbeClusterDone(info)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalResume(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalResume(event Event) {
|
||||
s.videoTracksMu.Lock()
|
||||
track := s.videoTracks[event.TrackID]
|
||||
s.videoTracksMu.Unlock()
|
||||
@@ -711,11 +709,11 @@ func (s *StreamAllocator) handleSignalResume(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalSetAllowPause(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalSetAllowPause(event Event) {
|
||||
s.allowPause = event.Data.(bool)
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalSetChannelCapacity(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalSetChannelCapacity(event Event) {
|
||||
s.overriddenChannelCapacity = event.Data.(int64)
|
||||
if s.overriddenChannelCapacity > 0 {
|
||||
s.params.Logger.Infow("allocating on override channel capacity", "override", s.overriddenChannelCapacity)
|
||||
@@ -726,7 +724,7 @@ func (s *StreamAllocator) handleSignalSetChannelCapacity(event *Event) {
|
||||
}
|
||||
|
||||
/* STREAM-ALLOCATOR-DATA
|
||||
func (s *StreamAllocator) handleSignalNACK(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalNACK(event Event) {
|
||||
nackInfos := event.Data.([]sfu.NackInfo)
|
||||
|
||||
s.videoTracksMu.Lock()
|
||||
@@ -738,7 +736,7 @@ func (s *StreamAllocator) handleSignalNACK(event *Event) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *StreamAllocator) handleSignalRTCPReceiverReport(event *Event) {
|
||||
func (s *StreamAllocator) handleSignalRTCPReceiverReport(event Event) {
|
||||
rr := event.Data.(rtcp.ReceptionReport)
|
||||
|
||||
s.videoTracksMu.Lock()
|
||||
|
||||
+53
-10
@@ -19,8 +19,8 @@ import (
|
||||
"sync"
|
||||
|
||||
"github.com/gammazero/deque"
|
||||
|
||||
"github.com/livekit/protocol/logger"
|
||||
"github.com/livekit/protocol/utils"
|
||||
)
|
||||
|
||||
type OpsQueueParams struct {
|
||||
@@ -30,28 +30,71 @@ type OpsQueueParams struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type untypedOpsQueueOp func()
|
||||
|
||||
func (it untypedOpsQueueOp) run() {
|
||||
it()
|
||||
}
|
||||
|
||||
type OpsQueue struct {
|
||||
OpsQueueBase[untypedOpsQueueOp]
|
||||
}
|
||||
|
||||
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
||||
return &OpsQueue{
|
||||
OpsQueueBase: *newOpsQueueBase[untypedOpsQueueOp](params),
|
||||
}
|
||||
}
|
||||
|
||||
type typedOpsQueueOp[T any] struct {
|
||||
fn func(T)
|
||||
arg T
|
||||
}
|
||||
|
||||
func (it typedOpsQueueOp[T]) run() {
|
||||
it.fn(it.arg)
|
||||
}
|
||||
|
||||
type TypedOpsQueue[T any] struct {
|
||||
OpsQueueBase[typedOpsQueueOp[T]]
|
||||
}
|
||||
|
||||
func NewTypedOpsQueue[T any](params OpsQueueParams) *TypedOpsQueue[T] {
|
||||
return &TypedOpsQueue[T]{
|
||||
OpsQueueBase: *newOpsQueueBase[typedOpsQueueOp[T]](params),
|
||||
}
|
||||
}
|
||||
|
||||
func (oq *TypedOpsQueue[T]) Enqueue(fn func(T), arg T) {
|
||||
oq.OpsQueueBase.Enqueue(typedOpsQueueOp[T]{fn, arg})
|
||||
}
|
||||
|
||||
type opsQueueItem interface {
|
||||
run()
|
||||
}
|
||||
|
||||
type OpsQueueBase[T opsQueueItem] struct {
|
||||
params OpsQueueParams
|
||||
|
||||
lock sync.Mutex
|
||||
ops deque.Deque[func()]
|
||||
ops deque.Deque[T]
|
||||
wake chan struct{}
|
||||
isStarted bool
|
||||
doneChan chan struct{}
|
||||
isStopped bool
|
||||
}
|
||||
|
||||
func NewOpsQueue(params OpsQueueParams) *OpsQueue {
|
||||
oq := &OpsQueue{
|
||||
func newOpsQueueBase[T opsQueueItem](params OpsQueueParams) *OpsQueueBase[T] {
|
||||
oq := &OpsQueueBase[T]{
|
||||
params: params,
|
||||
wake: make(chan struct{}, 1),
|
||||
doneChan: make(chan struct{}),
|
||||
}
|
||||
oq.ops.SetMinCapacity(uint(utils.Min(bits.Len64(uint64(oq.params.MinSize-1)), 7)))
|
||||
oq.ops.SetMinCapacity(uint(min(bits.Len64(uint64(oq.params.MinSize-1)), 7)))
|
||||
return oq
|
||||
}
|
||||
|
||||
func (oq *OpsQueue) Start() {
|
||||
func (oq *OpsQueueBase[T]) Start() {
|
||||
oq.lock.Lock()
|
||||
if oq.isStarted {
|
||||
oq.lock.Unlock()
|
||||
@@ -64,7 +107,7 @@ func (oq *OpsQueue) Start() {
|
||||
go oq.process()
|
||||
}
|
||||
|
||||
func (oq *OpsQueue) Stop() <-chan struct{} {
|
||||
func (oq *OpsQueueBase[T]) Stop() <-chan struct{} {
|
||||
oq.lock.Lock()
|
||||
if oq.isStopped {
|
||||
oq.lock.Unlock()
|
||||
@@ -77,7 +120,7 @@ func (oq *OpsQueue) Stop() <-chan struct{} {
|
||||
return oq.doneChan
|
||||
}
|
||||
|
||||
func (oq *OpsQueue) Enqueue(op func()) {
|
||||
func (oq *OpsQueueBase[T]) Enqueue(op T) {
|
||||
oq.lock.Lock()
|
||||
defer oq.lock.Unlock()
|
||||
|
||||
@@ -94,7 +137,7 @@ func (oq *OpsQueue) Enqueue(op func()) {
|
||||
}
|
||||
}
|
||||
|
||||
func (oq *OpsQueue) process() {
|
||||
func (oq *OpsQueueBase[T]) process() {
|
||||
defer close(oq.doneChan)
|
||||
|
||||
for {
|
||||
@@ -113,7 +156,7 @@ func (oq *OpsQueue) process() {
|
||||
op := oq.ops.PopFront()
|
||||
oq.lock.Unlock()
|
||||
|
||||
op()
|
||||
op.run()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user