Files
livekit-3d/pkg/rtc/supervisor/subscription_monitor.go
T
Raja Subramanian 07c43e0972 Supervisor beginnings (#1005)
* Remove VP9 from media engine set up.

* Remove vp9 from config sample

* Supervisor beginnings

Eventual goal is to have a reconciler which moves state from
actual -> desired. First step along the way is to observe/monitor.
The first step even in that is an initial implementation to get
feedback on the direction.

This PR is a start in that direction
- Concept of a supervisor at local participant level
- This supervisor will be responsible for periodically monitor
  actual vs desired (this is the one which will eventually trigger
  other things to reconcile, but for now it just logs on error)
- A new interface `OperationMonitor` which requires two methods
  o Check() returns an error based on actual vs desired state.
  o IsIdle() returns bool. Returns true if the monitor is idle.
- The supervisor maintains a list of monitors and does periodic check.

In the above framework, starting with list of
subscriptions/unsubscriptions. There is a new module
`SubscriptionMonitor` which checks subscription transitions.
A subscription transition is queued on subscribe/unsubscribe.
The transition can be satisfied when a subscribedTrack is added OR
removed. Error condition is when a transition is not satisfied for
10 seconds. Idle is when the transition queue is empty and
subscribedTrack is nil, i. e. the last transition would have been
unsubscribe and subscribed track removed (unsubscribe satisfied).

The idea is individual monitors can check on different things.
Some more things that I am thinking about are
- PublishedTrackMonitor - started when an add track happens,
  satisfied when OnTrack happens, error if `OnTrack` does not
  fire for a while and track is not muted, idle when there is
  nothing pending.
- PublishedTrackStreamingMonitor - to ensure that a published track
  is receiving media at the server (accounting for dynacast, mute, etc)
- SubscribedTrackStreamingMonitor - to ensure down track is sending
  data unless muted.

* Remove debug

* Protect against early casting errors

* Adding PublicationMonitor
2022-09-15 11:16:37 +05:30

129 lines
2.5 KiB
Go

package supervisor
import (
"errors"
"sync"
"time"
"github.com/gammazero/deque"
"github.com/livekit/livekit-server/pkg/rtc/types"
"github.com/livekit/protocol/livekit"
"github.com/livekit/protocol/logger"
)
const (
transitionWaitDuration = 10 * time.Second
)
var (
errTransitionTimeout = errors.New("transition time out")
)
type transition struct {
isSubscribed bool
at time.Time
}
type SubscriptionMonitorParams struct {
TrackID livekit.TrackID
Logger logger.Logger
}
type SubscriptionMonitor struct {
params SubscriptionMonitorParams
lock sync.RWMutex
desiredTransitions deque.Deque
subscribedTrack types.SubscribedTrack
}
func NewSubscriptionMonitor(params SubscriptionMonitorParams) *SubscriptionMonitor {
s := &SubscriptionMonitor{
params: params,
}
s.desiredTransitions.SetMinCapacity(2)
return s
}
func (s *SubscriptionMonitor) UpdateSubscription(isSubscribed bool) {
s.lock.Lock()
s.desiredTransitions.PushBack(
&transition{
isSubscribed: isSubscribed,
at: time.Now(),
},
)
s.update()
s.lock.Unlock()
}
func (s *SubscriptionMonitor) SetSubscribedTrack(subTrack types.SubscribedTrack) {
s.lock.Lock()
s.subscribedTrack = subTrack
s.update()
s.lock.Unlock()
}
func (s *SubscriptionMonitor) ClearSubscribedTrack(subTrack types.SubscribedTrack) {
s.lock.Lock()
if s.subscribedTrack == subTrack {
s.subscribedTrack = nil
} else {
s.params.Logger.Errorw("mismatched subscribed track on clear", nil, "trackID", s.params.TrackID)
}
s.update()
s.lock.Unlock()
}
func (s *SubscriptionMonitor) Check() error {
s.lock.RLock()
var tx *transition
if s.desiredTransitions.Len() > 0 {
tx = s.desiredTransitions.Front().(*transition)
}
s.lock.RUnlock()
if tx == nil {
return nil
}
if time.Since(tx.at) > transitionWaitDuration {
// timed out waiting for transition
return errTransitionTimeout
}
// give more time for transition to happen
return nil
}
func (s *SubscriptionMonitor) IsIdle() bool {
s.lock.RLock()
defer s.lock.RUnlock()
return s.desiredTransitions.Len() == 0 && s.subscribedTrack == nil
}
func (s *SubscriptionMonitor) update() {
var tx *transition
if s.desiredTransitions.Len() > 0 {
tx = s.desiredTransitions.PopFront().(*transition)
}
if tx == nil {
return
}
switch {
case tx.isSubscribed && s.subscribedTrack != nil:
return
case !tx.isSubscribed && s.subscribedTrack == nil:
return
default:
// put it back as the condition is not satisfied
s.desiredTransitions.PushFront(tx)
return
}
}