Track subscription operations per source track. (#1248)
This commit is contained in:
@@ -18,24 +18,30 @@ type ParticipantSupervisorParams struct {
|
||||
Logger logger.Logger
|
||||
}
|
||||
|
||||
type trackMonitor struct {
|
||||
opMon types.OperationMonitor
|
||||
err error
|
||||
}
|
||||
|
||||
type ParticipantSupervisor struct {
|
||||
params ParticipantSupervisorParams
|
||||
|
||||
lock sync.RWMutex
|
||||
isPublisherConnected bool
|
||||
publications map[livekit.TrackID]types.OperationMonitor
|
||||
subscriptions map[livekit.TrackID]types.OperationMonitor
|
||||
publications map[livekit.TrackID]*trackMonitor
|
||||
subscriptions map[livekit.TrackID]*trackMonitor
|
||||
|
||||
isStopped atomic.Bool
|
||||
|
||||
onPublicationError func(trackID livekit.TrackID)
|
||||
onPublicationError func(trackID livekit.TrackID)
|
||||
onSubscriptionError func(trackID livekit.TrackID)
|
||||
}
|
||||
|
||||
func NewParticipantSupervisor(params ParticipantSupervisorParams) *ParticipantSupervisor {
|
||||
p := &ParticipantSupervisor{
|
||||
params: params,
|
||||
publications: make(map[livekit.TrackID]types.OperationMonitor),
|
||||
subscriptions: make(map[livekit.TrackID]types.OperationMonitor),
|
||||
publications: make(map[livekit.TrackID]*trackMonitor),
|
||||
subscriptions: make(map[livekit.TrackID]*trackMonitor),
|
||||
}
|
||||
|
||||
go p.checkState()
|
||||
@@ -61,12 +67,26 @@ func (p *ParticipantSupervisor) getOnPublicationError() func(trackID livekit.Tra
|
||||
return p.onPublicationError
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) OnSubscriptionError(f func(trackID livekit.TrackID)) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.onSubscriptionError = f
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) getOnSubscriptionError() func(trackID livekit.TrackID) {
|
||||
p.lock.RLock()
|
||||
defer p.lock.RUnlock()
|
||||
|
||||
return p.onSubscriptionError
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) SetPublisherPeerConnectionConnected(isConnected bool) {
|
||||
p.lock.Lock()
|
||||
p.isPublisherConnected = isConnected
|
||||
|
||||
for _, pm := range p.publications {
|
||||
pm.PostEvent(types.OperationMonitorEventPublisherPeerConnectionConnected, p.isPublisherConnected)
|
||||
pm.opMon.PostEvent(types.OperationMonitorEventPublisherPeerConnectionConnected, p.isPublisherConnected)
|
||||
}
|
||||
p.lock.Unlock()
|
||||
}
|
||||
@@ -75,16 +95,18 @@ func (p *ParticipantSupervisor) AddPublication(trackID livekit.TrackID) {
|
||||
p.lock.Lock()
|
||||
pm, ok := p.publications[trackID]
|
||||
if !ok {
|
||||
pm = NewPublicationMonitor(
|
||||
PublicationMonitorParams{
|
||||
TrackID: trackID,
|
||||
IsPeerConnectionConnected: p.isPublisherConnected,
|
||||
Logger: p.params.Logger,
|
||||
},
|
||||
)
|
||||
pm = &trackMonitor{
|
||||
opMon: NewPublicationMonitor(
|
||||
PublicationMonitorParams{
|
||||
TrackID: trackID,
|
||||
IsPeerConnectionConnected: p.isPublisherConnected,
|
||||
Logger: p.params.Logger,
|
||||
},
|
||||
),
|
||||
}
|
||||
p.publications[trackID] = pm
|
||||
}
|
||||
pm.PostEvent(types.OperationMonitorEventAddPendingPublication, nil)
|
||||
pm.opMon.PostEvent(types.OperationMonitorEventAddPendingPublication, nil)
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
@@ -92,7 +114,7 @@ func (p *ParticipantSupervisor) SetPublicationMute(trackID livekit.TrackID, isMu
|
||||
p.lock.Lock()
|
||||
pm, ok := p.publications[trackID]
|
||||
if ok {
|
||||
pm.PostEvent(types.OperationMonitorEventSetPublicationMute, isMuted)
|
||||
pm.opMon.PostEvent(types.OperationMonitorEventSetPublicationMute, isMuted)
|
||||
}
|
||||
p.lock.Unlock()
|
||||
}
|
||||
@@ -101,7 +123,7 @@ func (p *ParticipantSupervisor) SetPublishedTrack(trackID livekit.TrackID, pubTr
|
||||
p.lock.RLock()
|
||||
pm, ok := p.publications[trackID]
|
||||
if ok {
|
||||
pm.PostEvent(types.OperationMonitorEventSetPublishedTrack, pubTrack)
|
||||
pm.opMon.PostEvent(types.OperationMonitorEventSetPublishedTrack, pubTrack)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
@@ -110,36 +132,56 @@ func (p *ParticipantSupervisor) ClearPublishedTrack(trackID livekit.TrackID, pub
|
||||
p.lock.RLock()
|
||||
pm, ok := p.publications[trackID]
|
||||
if ok {
|
||||
pm.PostEvent(types.OperationMonitorEventClearPublishedTrack, pubTrack)
|
||||
pm.opMon.PostEvent(types.OperationMonitorEventClearPublishedTrack, pubTrack)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) UpdateSubscription(trackID livekit.TrackID, isSubscribed bool) {
|
||||
func (p *ParticipantSupervisor) UpdateSubscription(trackID livekit.TrackID, isSubscribe bool, sourceTrack types.MediaTrack) {
|
||||
p.lock.Lock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if !ok {
|
||||
sm = NewSubscriptionMonitor(SubscriptionMonitorParams{TrackID: trackID, Logger: p.params.Logger})
|
||||
sm = &trackMonitor{
|
||||
opMon: NewSubscriptionMonitor(SubscriptionMonitorParams{TrackID: trackID, Logger: p.params.Logger}),
|
||||
}
|
||||
p.subscriptions[trackID] = sm
|
||||
}
|
||||
sm.PostEvent(types.OperationMonitorEventUpdateSubscription, isSubscribed)
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventUpdateSubscription,
|
||||
SubscriptionOpParams{
|
||||
SourceTrack: sourceTrack,
|
||||
IsSubscribe: isSubscribe,
|
||||
},
|
||||
)
|
||||
p.lock.Unlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) SetSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack) {
|
||||
func (p *ParticipantSupervisor) SetSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack, sourceTrack types.MediaTrack) {
|
||||
p.lock.RLock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if ok {
|
||||
sm.PostEvent(types.OperationMonitorEventSetSubscribedTrack, subTrack)
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventSetSubscribedTrack,
|
||||
UpdateSubscribedTrackParams{
|
||||
SourceTrack: sourceTrack,
|
||||
SubscribedTrack: subTrack,
|
||||
},
|
||||
)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) ClearSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack) {
|
||||
func (p *ParticipantSupervisor) ClearSubscribedTrack(trackID livekit.TrackID, subTrack types.SubscribedTrack, sourceTrack types.MediaTrack) {
|
||||
p.lock.RLock()
|
||||
sm, ok := p.subscriptions[trackID]
|
||||
if ok {
|
||||
sm.PostEvent(types.OperationMonitorEventClearSubscribedTrack, subTrack)
|
||||
sm.opMon.PostEvent(
|
||||
types.OperationMonitorEventClearSubscribedTrack,
|
||||
UpdateSubscribedTrackParams{
|
||||
SourceTrack: sourceTrack,
|
||||
SubscribedTrack: subTrack,
|
||||
},
|
||||
)
|
||||
}
|
||||
p.lock.RUnlock()
|
||||
}
|
||||
@@ -161,11 +203,18 @@ func (p *ParticipantSupervisor) checkPublications() {
|
||||
var removablePublications []livekit.TrackID
|
||||
p.lock.RLock()
|
||||
for trackID, pm := range p.publications {
|
||||
if err := pm.Check(); err != nil {
|
||||
p.params.Logger.Errorw("supervisor error on publication", err, "trackID", trackID)
|
||||
erroredPublications = append(erroredPublications, trackID)
|
||||
if err := pm.opMon.Check(); err != nil {
|
||||
if pm.err == nil {
|
||||
p.params.Logger.Errorw("supervisor error on publication", err, "trackID", trackID)
|
||||
pm.err = err
|
||||
erroredPublications = append(erroredPublications, trackID)
|
||||
}
|
||||
} else {
|
||||
if pm.IsIdle() {
|
||||
if pm.err != nil {
|
||||
p.params.Logger.Infow("supervisor publication recovered", "trackID", trackID)
|
||||
pm.err = err
|
||||
}
|
||||
if pm.opMon.IsIdle() {
|
||||
removablePublications = append(removablePublications, trackID)
|
||||
}
|
||||
}
|
||||
@@ -186,13 +235,22 @@ func (p *ParticipantSupervisor) checkPublications() {
|
||||
}
|
||||
|
||||
func (p *ParticipantSupervisor) checkSubscriptions() {
|
||||
var erroredSubscriptions []livekit.TrackID
|
||||
var removableSubscriptions []livekit.TrackID
|
||||
p.lock.RLock()
|
||||
for trackID, sm := range p.subscriptions {
|
||||
if err := sm.Check(); err != nil {
|
||||
p.params.Logger.Errorw("supervisor error on subscription", err, "trackID", trackID)
|
||||
if err := sm.opMon.Check(); err != nil {
|
||||
if sm.err == nil {
|
||||
p.params.Logger.Errorw("supervisor error on subscription", err, "trackID", trackID)
|
||||
sm.err = err
|
||||
erroredSubscriptions = append(erroredSubscriptions, trackID)
|
||||
}
|
||||
} else {
|
||||
if sm.IsIdle() {
|
||||
if sm.err != nil {
|
||||
p.params.Logger.Infow("supervisor subscription recovered", "trackID", trackID)
|
||||
sm.err = err
|
||||
}
|
||||
if sm.opMon.IsIdle() {
|
||||
removableSubscriptions = append(removableSubscriptions, trackID)
|
||||
}
|
||||
}
|
||||
@@ -204,4 +262,10 @@ func (p *ParticipantSupervisor) checkSubscriptions() {
|
||||
delete(p.subscriptions, trackID)
|
||||
}
|
||||
p.lock.Unlock()
|
||||
|
||||
if onSubscriptionError := p.getOnSubscriptionError(); onSubscriptionError != nil {
|
||||
for _, trackID := range erroredSubscriptions {
|
||||
onSubscriptionError(trackID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ type PublicationMonitor struct {
|
||||
publishedTrack types.LocalMediaTrack
|
||||
isMuted bool
|
||||
unmutedAt time.Time
|
||||
|
||||
lastError error
|
||||
}
|
||||
|
||||
func NewPublicationMonitor(params PublicationMonitorParams) *PublicationMonitor {
|
||||
@@ -118,7 +116,7 @@ func (p *PublicationMonitor) clearPublishedTrack(pubTrack types.LocalMediaTrack)
|
||||
if p.publishedTrack == pubTrack {
|
||||
p.publishedTrack = nil
|
||||
} else {
|
||||
p.params.Logger.Errorw("mismatched published track on clear", nil, "trackID", p.params.TrackID)
|
||||
p.params.Logger.Errorw("supervisor: mismatched published track on clear", nil, "trackID", p.params.TrackID)
|
||||
}
|
||||
|
||||
p.update()
|
||||
@@ -127,12 +125,6 @@ func (p *PublicationMonitor) clearPublishedTrack(pubTrack types.LocalMediaTrack)
|
||||
|
||||
func (p *PublicationMonitor) Check() error {
|
||||
p.lock.RLock()
|
||||
if p.lastError != nil {
|
||||
p.lock.RUnlock()
|
||||
// return an error only once
|
||||
return nil
|
||||
}
|
||||
|
||||
var pub *publish
|
||||
if p.desiredPublishes.Len() > 0 {
|
||||
pub = p.desiredPublishes.Front().(*publish)
|
||||
@@ -148,10 +140,6 @@ func (p *PublicationMonitor) Check() error {
|
||||
|
||||
if pub.isStart && !isMuted && !unmutedAt.IsZero() && time.Since(unmutedAt) > publishWaitDuration {
|
||||
// timed out waiting for publish
|
||||
p.lock.Lock()
|
||||
p.lastError = errPublishTimeout
|
||||
p.lock.Unlock()
|
||||
|
||||
return errPublishTimeout
|
||||
}
|
||||
|
||||
@@ -183,7 +171,5 @@ func (p *PublicationMonitor) update() {
|
||||
p.desiredPublishes.PushFront(pub)
|
||||
return
|
||||
}
|
||||
|
||||
p.lastError = nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
transitionWaitDuration = 10 * time.Second
|
||||
transitionWaitDuration = 20 * time.Second
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -25,6 +25,22 @@ type transition struct {
|
||||
at time.Time
|
||||
}
|
||||
|
||||
type subscriptionOps struct {
|
||||
desiredTransitions deque.Deque
|
||||
|
||||
subscribedTrack types.SubscribedTrack
|
||||
}
|
||||
|
||||
type SubscriptionOpParams struct {
|
||||
SourceTrack types.MediaTrack
|
||||
IsSubscribe bool
|
||||
}
|
||||
|
||||
type UpdateSubscribedTrackParams struct {
|
||||
SourceTrack types.MediaTrack
|
||||
SubscribedTrack types.SubscribedTrack
|
||||
}
|
||||
|
||||
type SubscriptionMonitorParams struct {
|
||||
TrackID livekit.TrackID
|
||||
Logger logger.Logger
|
||||
@@ -33,38 +49,36 @@ type SubscriptionMonitorParams struct {
|
||||
type SubscriptionMonitor struct {
|
||||
params SubscriptionMonitorParams
|
||||
|
||||
lock sync.RWMutex
|
||||
desiredTransitions deque.Deque
|
||||
|
||||
subscribedTrack types.SubscribedTrack
|
||||
|
||||
lastError error
|
||||
lock sync.RWMutex
|
||||
subscriptionOpsBySource map[types.MediaTrack]*subscriptionOps
|
||||
}
|
||||
|
||||
func NewSubscriptionMonitor(params SubscriptionMonitorParams) *SubscriptionMonitor {
|
||||
s := &SubscriptionMonitor{
|
||||
params: params,
|
||||
params: params,
|
||||
subscriptionOpsBySource: make(map[types.MediaTrack]*subscriptionOps),
|
||||
}
|
||||
s.desiredTransitions.SetMinCapacity(2)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) PostEvent(ome types.OperationMonitorEvent, omd types.OperationMonitorData) {
|
||||
switch ome {
|
||||
case types.OperationMonitorEventUpdateSubscription:
|
||||
s.updateSubscription(omd.(bool))
|
||||
s.updateSubscription(omd.(SubscriptionOpParams))
|
||||
case types.OperationMonitorEventSetSubscribedTrack:
|
||||
s.setSubscribedTrack(omd.(types.SubscribedTrack))
|
||||
s.setSubscribedTrack(omd.(UpdateSubscribedTrackParams))
|
||||
case types.OperationMonitorEventClearSubscribedTrack:
|
||||
s.clearSubscribedTrack(omd.(types.SubscribedTrack))
|
||||
s.clearSubscribedTrack(omd.(UpdateSubscribedTrackParams))
|
||||
}
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) updateSubscription(isSubscribe bool) {
|
||||
func (s *SubscriptionMonitor) updateSubscription(params SubscriptionOpParams) {
|
||||
s.lock.Lock()
|
||||
s.desiredTransitions.PushBack(
|
||||
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
so.desiredTransitions.PushBack(
|
||||
&transition{
|
||||
isSubscribe: isSubscribe,
|
||||
isSubscribe: params.IsSubscribe,
|
||||
at: time.Now(),
|
||||
},
|
||||
)
|
||||
@@ -72,19 +86,21 @@ func (s *SubscriptionMonitor) updateSubscription(isSubscribe bool) {
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) setSubscribedTrack(subTrack types.SubscribedTrack) {
|
||||
func (s *SubscriptionMonitor) setSubscribedTrack(params UpdateSubscribedTrackParams) {
|
||||
s.lock.Lock()
|
||||
s.subscribedTrack = subTrack
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
so.subscribedTrack = params.SubscribedTrack
|
||||
s.update()
|
||||
s.lock.Unlock()
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) clearSubscribedTrack(subTrack types.SubscribedTrack) {
|
||||
func (s *SubscriptionMonitor) clearSubscribedTrack(params UpdateSubscribedTrackParams) {
|
||||
s.lock.Lock()
|
||||
if s.subscribedTrack == subTrack {
|
||||
s.subscribedTrack = nil
|
||||
so := s.getOrCreateSubscriptionOpsForSource(params.SourceTrack)
|
||||
if so.subscribedTrack == params.SubscribedTrack {
|
||||
so.subscribedTrack = nil
|
||||
} else {
|
||||
s.params.Logger.Errorw("mismatched subscribed track on clear", nil, "trackID", s.params.TrackID)
|
||||
s.params.Logger.Errorw("supervisor: mismatched subscribed track on clear", nil, "trackID", s.params.TrackID)
|
||||
}
|
||||
|
||||
s.update()
|
||||
@@ -92,37 +108,27 @@ func (s *SubscriptionMonitor) clearSubscribedTrack(subTrack types.SubscribedTrac
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) Check() error {
|
||||
s.lock.RLock()
|
||||
if s.lastError != nil {
|
||||
s.lock.RUnlock()
|
||||
// return an error only once
|
||||
return nil
|
||||
}
|
||||
s.lock.Lock()
|
||||
defer s.lock.Unlock()
|
||||
|
||||
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
|
||||
var err error
|
||||
if tx.isSubscribe {
|
||||
err = errSubscribeTimeout
|
||||
} else {
|
||||
err = errUnsubscribeTimeout
|
||||
for _, so := range s.subscriptionOpsBySource {
|
||||
var tx *transition
|
||||
if so.desiredTransitions.Len() > 0 {
|
||||
tx = so.desiredTransitions.Front().(*transition)
|
||||
}
|
||||
|
||||
s.lock.Lock()
|
||||
s.lastError = err
|
||||
s.lock.Unlock()
|
||||
if tx == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
return err
|
||||
if time.Since(tx.at) > transitionWaitDuration {
|
||||
// timed out waiting for transition
|
||||
if tx.isSubscribe {
|
||||
return errSubscribeTimeout
|
||||
} else {
|
||||
return errUnsubscribeTimeout
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// give more time for transition to happen
|
||||
@@ -133,26 +139,41 @@ func (s *SubscriptionMonitor) IsIdle() bool {
|
||||
s.lock.RLock()
|
||||
defer s.lock.RUnlock()
|
||||
|
||||
return s.desiredTransitions.Len() == 0 && s.subscribedTrack == nil
|
||||
return len(s.subscriptionOpsBySource) == 0
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) getOrCreateSubscriptionOpsForSource(sourceTrack types.MediaTrack) *subscriptionOps {
|
||||
so := s.subscriptionOpsBySource[sourceTrack]
|
||||
if so == nil {
|
||||
so = &subscriptionOps{}
|
||||
so.desiredTransitions.SetMinCapacity(4)
|
||||
s.subscriptionOpsBySource[sourceTrack] = so
|
||||
}
|
||||
|
||||
return so
|
||||
}
|
||||
|
||||
func (s *SubscriptionMonitor) update() {
|
||||
for {
|
||||
var tx *transition
|
||||
if s.desiredTransitions.Len() > 0 {
|
||||
tx = s.desiredTransitions.PopFront().(*transition)
|
||||
}
|
||||
for sourceTrack, so := range s.subscriptionOpsBySource {
|
||||
for {
|
||||
var tx *transition
|
||||
if so.desiredTransitions.Len() > 0 {
|
||||
tx = so.desiredTransitions.PopFront().(*transition)
|
||||
}
|
||||
|
||||
if tx == nil {
|
||||
return
|
||||
}
|
||||
if tx == nil {
|
||||
break
|
||||
}
|
||||
|
||||
if (tx.isSubscribe && s.subscribedTrack == nil) || (!tx.isSubscribe && s.subscribedTrack != nil) {
|
||||
// put it back as the condition is not satisfied
|
||||
s.desiredTransitions.PushFront(tx)
|
||||
return
|
||||
}
|
||||
if (tx.isSubscribe && so.subscribedTrack == nil) || (!tx.isSubscribe && so.subscribedTrack != nil) {
|
||||
// put it back as the condition is not satisfied
|
||||
so.desiredTransitions.PushFront(tx)
|
||||
break
|
||||
}
|
||||
|
||||
s.lastError = nil
|
||||
if so.desiredTransitions.Len() == 0 && so.subscribedTrack == nil {
|
||||
delete(s.subscriptionOpsBySource, sourceTrack)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user