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
+59 -14
View File
@@ -1,6 +1,7 @@
package rtc
import (
"sync"
"time"
"github.com/bep/debounce"
@@ -30,14 +31,18 @@ type SubscribedTrackParams struct {
}
type SubscribedTrack struct {
params SubscribedTrackParams
subMuted atomic.Bool
pubMuted atomic.Bool
settings atomic.Pointer[livekit.UpdateTrackSettings]
logger logger.Logger
params SubscribedTrackParams
subMuted atomic.Bool
pubMuted atomic.Bool
settings atomic.Pointer[livekit.UpdateTrackSettings]
logger logger.Logger
sender atomic.Pointer[webrtc.RTPSender]
needsNegotiation atomic.Bool
onBind atomic.Value // func()
bound atomic.Bool
bindLock sync.Mutex
onBindCallbacks []func()
onClose atomic.Value // func(bool)
bound atomic.Bool
debouncer func(func())
}
@@ -56,28 +61,52 @@ func NewSubscribedTrack(params SubscribedTrackParams) *SubscribedTrack {
return s
}
func (t *SubscribedTrack) OnBind(f func()) {
t.onBind.Store(f)
func (t *SubscribedTrack) AddOnBind(f func()) {
t.bindLock.Lock()
bound := t.bound.Load()
if !bound {
t.onBindCallbacks = append(t.onBindCallbacks, f)
}
t.bindLock.Unlock()
t.maybeOnBind()
if bound {
// fire immediately, do not need to persist since bind is a one time event
go f()
}
}
// for DownTrack callback to notify us that it's bound
func (t *SubscribedTrack) Bound() {
t.bindLock.Lock()
t.bound.Store(true)
callbacks := t.onBindCallbacks
t.onBindCallbacks = nil
t.bindLock.Unlock()
if !t.params.AdaptiveStream {
t.params.DownTrack.SetMaxSpatialLayer(
buffer.VideoQualityToSpatialLayer(livekit.VideoQuality_HIGH, t.params.MediaTrack.ToProto()),
)
}
t.maybeOnBind()
for _, cb := range callbacks {
go cb()
}
}
func (t *SubscribedTrack) maybeOnBind() {
if onBind := t.onBind.Load(); onBind != nil && t.bound.Load() {
go onBind.(func())()
// for DownTrack callback to notify us that it's closed
func (t *SubscribedTrack) Close(willBeResumed bool) {
if onClose := t.onClose.Load(); onClose != nil {
go onClose.(func(bool))(willBeResumed)
}
}
func (t *SubscribedTrack) OnClose(f func(bool)) {
t.onClose.Store(f)
}
func (t *SubscribedTrack) IsBound() bool {
return t.bound.Load()
}
func (t *SubscribedTrack) ID() livekit.TrackID {
return livekit.TrackID(t.params.DownTrack.ID())
}
@@ -167,6 +196,22 @@ func (t *SubscribedTrack) UpdateVideoLayer() {
}
}
func (t *SubscribedTrack) NeedsNegotiation() bool {
return t.needsNegotiation.Load()
}
func (t *SubscribedTrack) SetNeedsNegotiation(needs bool) {
t.needsNegotiation.Store(needs)
}
func (t *SubscribedTrack) RTPSender() *webrtc.RTPSender {
return t.sender.Load()
}
func (t *SubscribedTrack) SetRTPSender(sender *webrtc.RTPSender) {
t.sender.Store(sender)
}
func (t *SubscribedTrack) updateDownTrackMute() {
muted := t.subMuted.Load() || t.pubMuted.Load()
t.DownTrack().Mute(muted)