feat: SubscriptionManager to consolidate subscription handling (#1317)

Added a new manager to handle all subscription needs. Implemented using reconciler pattern. The goals are:

improve subscription resilience by separating desired state and current state
reduce complexity of synchronous processing
better detect failures with the ability to trigger full reconnect
This commit is contained in:
David Zhao
2023-01-24 23:06:16 -08:00
committed by GitHub
parent c696626fe8
commit cd6b8b80b9
31 changed files with 2625 additions and 2208 deletions
+7 -78
View File
@@ -115,10 +115,6 @@ func NewMediaTrackReceiver(params MediaTrackReceiverParams) *MediaTrackReceiver
Logger: params.Logger,
})
t.MediaTrackSubscriptions.OnDownTrackCreated(t.onDownTrackCreated)
t.MediaTrackSubscriptions.OnSubscriptionOperationComplete(func(sub types.LocalParticipant) {
t.removePendingSubscribeOp(sub.ID())
sub.ClearInProgressAndProcessSubscriptionRequestsQueue(t.ID())
})
if t.trackInfo.Muted {
t.SetMuted(true)
@@ -405,47 +401,12 @@ func (t *MediaTrackReceiver) AddOnClose(f func()) {
t.lock.Unlock()
}
func (t *MediaTrackReceiver) addPendingSubscribeOp(subscriberID livekit.ParticipantID) {
t.lock.Lock()
if c, ok := t.pendingSubscribeOp[subscriberID]; !ok {
t.pendingSubscribeOp[subscriberID] = 1
} else {
t.pendingSubscribeOp[subscriberID] = c + 1
}
t.lock.Unlock()
}
func (t *MediaTrackReceiver) removePendingSubscribeOp(subscriberID livekit.ParticipantID) {
t.lock.Lock()
if c, ok := t.pendingSubscribeOp[subscriberID]; ok {
t.pendingSubscribeOp[subscriberID] = c - 1
if t.pendingSubscribeOp[subscriberID] == 0 {
delete(t.pendingSubscribeOp, subscriberID)
}
}
t.lock.Unlock()
}
// AddSubscriber subscribes sub to current mediaTrack
func (t *MediaTrackReceiver) AddSubscriber(sub types.LocalParticipant) error {
if sub.EnqueueSubscribeTrack(t.ID(), t.params.MediaTrack, t.params.IsRelayed, t.addSubscriber) {
t.addPendingSubscribeOp(sub.ID())
}
return nil
}
func (t *MediaTrackReceiver) addSubscriber(sub types.LocalParticipant) (err error) {
defer func() {
if err != nil {
t.removePendingSubscribeOp(sub.ID())
}
}()
func (t *MediaTrackReceiver) AddSubscriber(sub types.LocalParticipant) (types.SubscribedTrack, error) {
t.lock.RLock()
if t.state != mediaTrackReceiverStateOpen {
t.lock.RUnlock()
err = ErrNotOpen
return
return nil, ErrNotOpen
}
receivers := t.receiversShadow
@@ -455,8 +416,7 @@ func (t *MediaTrackReceiver) addSubscriber(sub types.LocalParticipant) (err erro
if len(receivers) == 0 {
// cannot add, no receiver
err = ErrNoReceiver
return
return nil, ErrNoReceiver
}
for _, receiver := range receivers {
@@ -473,7 +433,6 @@ func (t *MediaTrackReceiver) addSubscriber(sub types.LocalParticipant) (err erro
}
}
// using DownTrack from ion-sfu
streamId := string(t.PublisherID())
if sub.ProtocolVersion().SupportsPackedStreamId() {
// when possible, pack both IDs in streamID to allow new streams to be generated
@@ -482,44 +441,21 @@ func (t *MediaTrackReceiver) addSubscriber(sub types.LocalParticipant) (err erro
}
tLogger := LoggerWithTrack(sub.GetLogger(), t.ID(), t.params.IsRelayed)
err = t.MediaTrackSubscriptions.AddSubscriber(sub, NewWrappedReceiver(WrappedReceiverParams{
wrParams := NewWrappedReceiver(WrappedReceiverParams{
Receivers: receivers,
TrackID: t.ID(),
StreamId: streamId,
UpstreamCodecs: potentialCodecs,
Logger: tLogger,
DisableRed: t.trackInfo.GetDisableRed(),
}))
if err != nil {
return
}
return nil
})
return t.MediaTrackSubscriptions.AddSubscriber(sub, wrParams)
}
// RemoveSubscriber removes participant from subscription
// stop all forwarders to the client
func (t *MediaTrackReceiver) RemoveSubscriber(subscriberID livekit.ParticipantID, willBeResumed bool) {
subTrack := t.getSubscribedTrack(subscriberID)
if subTrack == nil {
return
}
sub := subTrack.Subscriber()
if sub.EnqueueUnsubscribeTrack(subTrack.ID(), t.params.MediaTrack, t.params.IsRelayed, willBeResumed, t.removeSubscriber) {
t.addPendingSubscribeOp(sub.ID())
}
}
func (t *MediaTrackReceiver) removeSubscriber(subscriberID livekit.ParticipantID, willBeResumed bool) (err error) {
defer func() {
if err != nil {
t.removePendingSubscribeOp(subscriberID)
}
}()
err = t.MediaTrackSubscriptions.RemoveSubscriber(subscriberID, willBeResumed)
return
_ = t.MediaTrackSubscriptions.RemoveSubscriber(subscriberID, willBeResumed)
}
func (t *MediaTrackReceiver) removeAllSubscribersForMime(mime string, willBeResumed bool) {
@@ -529,13 +465,6 @@ func (t *MediaTrackReceiver) removeAllSubscribersForMime(mime string, willBeResu
}
}
func (t *MediaTrackReceiver) IsSubscribed() bool {
t.lock.RLock()
defer t.lock.RUnlock()
return t.MediaTrackSubscriptions.GetNumSubscribers() != 0 || len(t.pendingSubscribeOp) != 0
}
func (t *MediaTrackReceiver) RevokeDisallowedSubscribers(allowedSubscriberIdentities []livekit.ParticipantIdentity) []livekit.ParticipantIdentity {
var revokedSubscriberIdentities []livekit.ParticipantIdentity