Store subscriber forwarder state (#2907)

* Forwarder state for migrating participant.

* clean up

* update protocol deps

* cleanup debug
This commit is contained in:
Raja Subramanian
2024-08-05 21:13:07 +05:30
committed by GitHub
parent 5e4c6d46fb
commit 8c323330b6
11 changed files with 193 additions and 145 deletions
+1 -7
View File
@@ -21,7 +21,6 @@ import (
"github.com/pion/rtcp"
"github.com/pion/webrtc/v3"
"go.uber.org/atomic"
sutils "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit"
@@ -162,7 +161,6 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
})
// Bind callback can happen from replaceTrack, so set it up early
var reusingTransceiver atomic.Bool
var dtState sfu.DownTrackState
downTrack.OnBinding(func(err error) {
if err != nil {
@@ -170,9 +168,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
return
}
wr.DetermineReceiver(downTrack.Codec())
if reusingTransceiver.Load() {
downTrack.SeedState(dtState)
}
downTrack.SeedState(dtState)
if err = wr.AddDownTrack(downTrack); err != nil && err != sfu.ErrReceiverClosed {
sub.GetLogger().Errorw(
"could not add down track", err,
@@ -220,7 +216,6 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
"publisherID", subTrack.PublisherID(),
"trackID", trackID,
)
reusingTransceiver.Store(true)
rtpSender := existingTransceiver.Sender()
if rtpSender != nil {
// replaced track will bind immediately without negotiation, SetTransceiver first before bind
@@ -251,7 +246,6 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
existingTransceiver.Stop()
}
}
reusingTransceiver.Store(false)
// if cannot replace, find an unused transceiver or add new one
if transceiver == nil {
+6 -1
View File
@@ -862,6 +862,7 @@ func (p *ParticipantImpl) SetMigrateInfo(
previousOffer, previousAnswer *webrtc.SessionDescription,
mediaTracks []*livekit.TrackPublishedResponse,
dataChannels []*livekit.DataChannelInfo,
forwarderStates map[livekit.TrackID]*livekit.RTPForwarderState,
) {
p.pendingTracksLock.Lock()
for _, t := range mediaTracks {
@@ -882,6 +883,10 @@ func (p *ParticipantImpl) SetMigrateInfo(
}
p.TransportManager.SetMigrateInfo(previousOffer, previousAnswer, dataChannels)
for trackID, fs := range forwarderStates {
p.CacheDownTrack(trackID, nil, sfu.DownTrackState{ForwarderState: fs})
}
}
func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseReason, isExpectedToResume bool) error {
@@ -2459,7 +2464,7 @@ func (p *ParticipantImpl) setDowntracksConnected() {
func (p *ParticipantImpl) CacheDownTrack(trackID livekit.TrackID, rtpTransceiver *webrtc.RTPTransceiver, downTrack sfu.DownTrackState) {
p.lock.Lock()
if existing := p.cachedDownTracks[trackID]; existing != nil && existing.transceiver != rtpTransceiver {
p.subLogger.Infow("cached transceiver changed", "trackID", trackID)
p.subLogger.Warnw("cached transceiver changed", nil, "trackID", trackID)
}
p.cachedDownTracks[trackID] = &downTrackState{transceiver: rtpTransceiver, downTrack: downTrack}
p.subLogger.Debugw("caching downtrack", "trackID", trackID)
+20
View File
@@ -180,6 +180,26 @@ func (m *SubscriptionManager) GetSubscribedTracks() []types.SubscribedTrack {
return tracks
}
func (m *SubscriptionManager) StopAndGetSubscribedTracksForwarderState() map[livekit.TrackID]*livekit.RTPForwarderState {
m.lock.RLock()
defer m.lock.RUnlock()
states := make(map[livekit.TrackID]*livekit.RTPForwarderState, len(m.subscriptions))
for trackID, t := range m.subscriptions {
st := t.getSubscribedTrack()
if st != nil {
dt := st.DownTrack()
if dt != nil {
state := dt.StopWriteAndGetState()
if state.ForwarderState != nil {
states[trackID] = state.ForwarderState
}
}
}
}
return states
}
func (m *SubscriptionManager) HasSubscriptions() bool {
m.lock.RLock()
defer m.lock.RUnlock()
+7 -1
View File
@@ -369,6 +369,7 @@ type LocalParticipant interface {
// WaitUntilSubscribed waits until all subscriptions have been settled, or if the timeout
// has been reached. If the timeout expires, it will return an error.
WaitUntilSubscribed(timeout time.Duration) error
StopAndGetSubscribedTracksForwarderState() map[livekit.TrackID]*livekit.RTPForwarderState
// returns list of participant identities that the current participant is subscribed to
GetSubscribedParticipants() []livekit.ParticipantID
@@ -412,7 +413,12 @@ type LocalParticipant interface {
NotifyMigration()
SetMigrateState(s MigrateState)
MigrateState() MigrateState
SetMigrateInfo(previousOffer, previousAnswer *webrtc.SessionDescription, mediaTracks []*livekit.TrackPublishedResponse, dataChannels []*livekit.DataChannelInfo)
SetMigrateInfo(
previousOffer, previousAnswer *webrtc.SessionDescription,
mediaTracks []*livekit.TrackPublishedResponse,
dataChannels []*livekit.DataChannelInfo,
forwarderStates map[livekit.TrackID]*livekit.RTPForwarderState,
)
UpdateMediaRTT(rtt uint32)
UpdateSignalingRTT(rtt uint32)
@@ -782,13 +782,14 @@ type FakeLocalParticipant struct {
setMetadataArgsForCall []struct {
arg1 string
}
SetMigrateInfoStub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo)
SetMigrateInfoStub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState)
setMigrateInfoMutex sync.RWMutex
setMigrateInfoArgsForCall []struct {
arg1 *webrtc.SessionDescription
arg2 *webrtc.SessionDescription
arg3 []*livekit.TrackPublishedResponse
arg4 []*livekit.DataChannelInfo
arg5 map[livekit.TrackID]*livekit.RTPForwarderState
}
SetMigrateStateStub func(types.MigrateState)
setMigrateStateMutex sync.RWMutex
@@ -854,6 +855,16 @@ type FakeLocalParticipant struct {
stateReturnsOnCall map[int]struct {
result1 livekit.ParticipantInfo_State
}
StopAndGetSubscribedTracksForwarderStateStub func() map[livekit.TrackID]*livekit.RTPForwarderState
stopAndGetSubscribedTracksForwarderStateMutex sync.RWMutex
stopAndGetSubscribedTracksForwarderStateArgsForCall []struct {
}
stopAndGetSubscribedTracksForwarderStateReturns struct {
result1 map[livekit.TrackID]*livekit.RTPForwarderState
}
stopAndGetSubscribedTracksForwarderStateReturnsOnCall map[int]struct {
result1 map[livekit.TrackID]*livekit.RTPForwarderState
}
SubscribeToTrackStub func(livekit.TrackID)
subscribeToTrackMutex sync.RWMutex
subscribeToTrackArgsForCall []struct {
@@ -5193,7 +5204,7 @@ func (fake *FakeLocalParticipant) SetMetadataArgsForCall(i int) string {
return argsForCall.arg1
}
func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription, arg2 *webrtc.SessionDescription, arg3 []*livekit.TrackPublishedResponse, arg4 []*livekit.DataChannelInfo) {
func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription, arg2 *webrtc.SessionDescription, arg3 []*livekit.TrackPublishedResponse, arg4 []*livekit.DataChannelInfo, arg5 map[livekit.TrackID]*livekit.RTPForwarderState) {
var arg3Copy []*livekit.TrackPublishedResponse
if arg3 != nil {
arg3Copy = make([]*livekit.TrackPublishedResponse, len(arg3))
@@ -5210,12 +5221,13 @@ func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription
arg2 *webrtc.SessionDescription
arg3 []*livekit.TrackPublishedResponse
arg4 []*livekit.DataChannelInfo
}{arg1, arg2, arg3Copy, arg4Copy})
arg5 map[livekit.TrackID]*livekit.RTPForwarderState
}{arg1, arg2, arg3Copy, arg4Copy, arg5})
stub := fake.SetMigrateInfoStub
fake.recordInvocation("SetMigrateInfo", []interface{}{arg1, arg2, arg3Copy, arg4Copy})
fake.recordInvocation("SetMigrateInfo", []interface{}{arg1, arg2, arg3Copy, arg4Copy, arg5})
fake.setMigrateInfoMutex.Unlock()
if stub != nil {
fake.SetMigrateInfoStub(arg1, arg2, arg3, arg4)
fake.SetMigrateInfoStub(arg1, arg2, arg3, arg4, arg5)
}
}
@@ -5225,17 +5237,17 @@ func (fake *FakeLocalParticipant) SetMigrateInfoCallCount() int {
return len(fake.setMigrateInfoArgsForCall)
}
func (fake *FakeLocalParticipant) SetMigrateInfoCalls(stub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo)) {
func (fake *FakeLocalParticipant) SetMigrateInfoCalls(stub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState)) {
fake.setMigrateInfoMutex.Lock()
defer fake.setMigrateInfoMutex.Unlock()
fake.SetMigrateInfoStub = stub
}
func (fake *FakeLocalParticipant) SetMigrateInfoArgsForCall(i int) (*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo) {
func (fake *FakeLocalParticipant) SetMigrateInfoArgsForCall(i int) (*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState) {
fake.setMigrateInfoMutex.RLock()
defer fake.setMigrateInfoMutex.RUnlock()
argsForCall := fake.setMigrateInfoArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5
}
func (fake *FakeLocalParticipant) SetMigrateState(arg1 types.MigrateState) {
@@ -5607,6 +5619,59 @@ func (fake *FakeLocalParticipant) StateReturnsOnCall(i int, result1 livekit.Part
}{result1}
}
func (fake *FakeLocalParticipant) StopAndGetSubscribedTracksForwarderState() map[livekit.TrackID]*livekit.RTPForwarderState {
fake.stopAndGetSubscribedTracksForwarderStateMutex.Lock()
ret, specificReturn := fake.stopAndGetSubscribedTracksForwarderStateReturnsOnCall[len(fake.stopAndGetSubscribedTracksForwarderStateArgsForCall)]
fake.stopAndGetSubscribedTracksForwarderStateArgsForCall = append(fake.stopAndGetSubscribedTracksForwarderStateArgsForCall, struct {
}{})
stub := fake.StopAndGetSubscribedTracksForwarderStateStub
fakeReturns := fake.stopAndGetSubscribedTracksForwarderStateReturns
fake.recordInvocation("StopAndGetSubscribedTracksForwarderState", []interface{}{})
fake.stopAndGetSubscribedTracksForwarderStateMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeLocalParticipant) StopAndGetSubscribedTracksForwarderStateCallCount() int {
fake.stopAndGetSubscribedTracksForwarderStateMutex.RLock()
defer fake.stopAndGetSubscribedTracksForwarderStateMutex.RUnlock()
return len(fake.stopAndGetSubscribedTracksForwarderStateArgsForCall)
}
func (fake *FakeLocalParticipant) StopAndGetSubscribedTracksForwarderStateCalls(stub func() map[livekit.TrackID]*livekit.RTPForwarderState) {
fake.stopAndGetSubscribedTracksForwarderStateMutex.Lock()
defer fake.stopAndGetSubscribedTracksForwarderStateMutex.Unlock()
fake.StopAndGetSubscribedTracksForwarderStateStub = stub
}
func (fake *FakeLocalParticipant) StopAndGetSubscribedTracksForwarderStateReturns(result1 map[livekit.TrackID]*livekit.RTPForwarderState) {
fake.stopAndGetSubscribedTracksForwarderStateMutex.Lock()
defer fake.stopAndGetSubscribedTracksForwarderStateMutex.Unlock()
fake.StopAndGetSubscribedTracksForwarderStateStub = nil
fake.stopAndGetSubscribedTracksForwarderStateReturns = struct {
result1 map[livekit.TrackID]*livekit.RTPForwarderState
}{result1}
}
func (fake *FakeLocalParticipant) StopAndGetSubscribedTracksForwarderStateReturnsOnCall(i int, result1 map[livekit.TrackID]*livekit.RTPForwarderState) {
fake.stopAndGetSubscribedTracksForwarderStateMutex.Lock()
defer fake.stopAndGetSubscribedTracksForwarderStateMutex.Unlock()
fake.StopAndGetSubscribedTracksForwarderStateStub = nil
if fake.stopAndGetSubscribedTracksForwarderStateReturnsOnCall == nil {
fake.stopAndGetSubscribedTracksForwarderStateReturnsOnCall = make(map[int]struct {
result1 map[livekit.TrackID]*livekit.RTPForwarderState
})
}
fake.stopAndGetSubscribedTracksForwarderStateReturnsOnCall[i] = struct {
result1 map[livekit.TrackID]*livekit.RTPForwarderState
}{result1}
}
func (fake *FakeLocalParticipant) SubscribeToTrack(arg1 livekit.TrackID) {
fake.subscribeToTrackMutex.Lock()
fake.subscribeToTrackArgsForCall = append(fake.subscribeToTrackArgsForCall, struct {
@@ -6851,6 +6916,8 @@ func (fake *FakeLocalParticipant) Invocations() map[string][][]interface{} {
defer fake.setTrackMutedMutex.RUnlock()
fake.stateMutex.RLock()
defer fake.stateMutex.RUnlock()
fake.stopAndGetSubscribedTracksForwarderStateMutex.RLock()
defer fake.stopAndGetSubscribedTracksForwarderStateMutex.RUnlock()
fake.subscribeToTrackMutex.RLock()
defer fake.subscribeToTrackMutex.RUnlock()
fake.subscriberAsPrimaryMutex.RLock()