Delay getting forwarder state till migration is complete. (#2909)

This commit is contained in:
Raja Subramanian
2024-08-06 12:45:46 +05:30
committed by GitHub
parent 8c323330b6
commit 13ee1aca28
5 changed files with 54 additions and 22 deletions
+7 -1
View File
@@ -21,6 +21,7 @@ import (
"github.com/pion/rtcp" "github.com/pion/rtcp"
"github.com/pion/webrtc/v3" "github.com/pion/webrtc/v3"
"go.uber.org/atomic"
sutils "github.com/livekit/livekit-server/pkg/utils" sutils "github.com/livekit/livekit-server/pkg/utils"
"github.com/livekit/protocol/livekit" "github.com/livekit/protocol/livekit"
@@ -161,6 +162,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
}) })
// Bind callback can happen from replaceTrack, so set it up early // Bind callback can happen from replaceTrack, so set it up early
var reusingTransceiver atomic.Bool
var dtState sfu.DownTrackState var dtState sfu.DownTrackState
downTrack.OnBinding(func(err error) { downTrack.OnBinding(func(err error) {
if err != nil { if err != nil {
@@ -168,7 +170,9 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
return return
} }
wr.DetermineReceiver(downTrack.Codec()) wr.DetermineReceiver(downTrack.Codec())
downTrack.SeedState(dtState) if reusingTransceiver.Load() {
downTrack.SeedState(dtState)
}
if err = wr.AddDownTrack(downTrack); err != nil && err != sfu.ErrReceiverClosed { if err = wr.AddDownTrack(downTrack); err != nil && err != sfu.ErrReceiverClosed {
sub.GetLogger().Errorw( sub.GetLogger().Errorw(
"could not add down track", err, "could not add down track", err,
@@ -216,6 +220,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
"publisherID", subTrack.PublisherID(), "publisherID", subTrack.PublisherID(),
"trackID", trackID, "trackID", trackID,
) )
reusingTransceiver.Store(true)
rtpSender := existingTransceiver.Sender() rtpSender := existingTransceiver.Sender()
if rtpSender != nil { if rtpSender != nil {
// replaced track will bind immediately without negotiation, SetTransceiver first before bind // replaced track will bind immediately without negotiation, SetTransceiver first before bind
@@ -246,6 +251,7 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, wr *
existingTransceiver.Stop() existingTransceiver.Stop()
} }
} }
reusingTransceiver.Store(false)
// if cannot replace, find an unused transceiver or add new one // if cannot replace, find an unused transceiver or add new one
if transceiver == nil { if transceiver == nil {
+29 -8
View File
@@ -129,6 +129,7 @@ type ParticipantParams struct {
TURNSEnabled bool TURNSEnabled bool
GetParticipantInfo func(pID livekit.ParticipantID) *livekit.ParticipantInfo GetParticipantInfo func(pID livekit.ParticipantID) *livekit.ParticipantInfo
GetRegionSettings func(ip string) *livekit.RegionSettings GetRegionSettings func(ip string) *livekit.RegionSettings
GetSubscriberForwarderState func(p types.LocalParticipant) (map[livekit.TrackID]*livekit.RTPForwarderState, error)
DisableSupervisor bool DisableSupervisor bool
ReconnectOnPublicationError bool ReconnectOnPublicationError bool
ReconnectOnSubscriptionError bool ReconnectOnSubscriptionError bool
@@ -231,6 +232,7 @@ type ParticipantImpl struct {
onICEConfigChanged func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig) onICEConfigChanged func(participant types.LocalParticipant, iceConfig *livekit.ICEConfig)
cachedDownTracks map[livekit.TrackID]*downTrackState cachedDownTracks map[livekit.TrackID]*downTrackState
forwarderState map[livekit.TrackID]*livekit.RTPForwarderState
supervisor *supervisor.ParticipantSupervisor supervisor *supervisor.ParticipantSupervisor
@@ -862,7 +864,6 @@ func (p *ParticipantImpl) SetMigrateInfo(
previousOffer, previousAnswer *webrtc.SessionDescription, previousOffer, previousAnswer *webrtc.SessionDescription,
mediaTracks []*livekit.TrackPublishedResponse, mediaTracks []*livekit.TrackPublishedResponse,
dataChannels []*livekit.DataChannelInfo, dataChannels []*livekit.DataChannelInfo,
forwarderStates map[livekit.TrackID]*livekit.RTPForwarderState,
) { ) {
p.pendingTracksLock.Lock() p.pendingTracksLock.Lock()
for _, t := range mediaTracks { for _, t := range mediaTracks {
@@ -883,10 +884,6 @@ func (p *ParticipantImpl) SetMigrateInfo(
} }
p.TransportManager.SetMigrateInfo(previousOffer, previousAnswer, dataChannels) 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 { func (p *ParticipantImpl) Close(sendLeave bool, reason types.ParticipantCloseReason, isExpectedToResume bool) error {
@@ -1056,6 +1053,7 @@ func (p *ParticipantImpl) SetMigrateState(s types.MigrateState) {
case types.MigrateStateComplete: case types.MigrateStateComplete:
p.TransportManager.ProcessPendingPublisherDataChannels() p.TransportManager.ProcessPendingPublisherDataChannels()
p.cacheForwarderState()
} }
if onMigrateStateChange := p.getOnMigrateStateChange(); onMigrateStateChange != nil { if onMigrateStateChange := p.getOnMigrateStateChange(); onMigrateStateChange != nil {
@@ -1214,7 +1212,9 @@ func (p *ParticipantImpl) onTrackSubscribed(subTrack types.SubscribedTrack) {
return return
} }
if p.TransportManager.HasSubscriberEverConnected() { if p.TransportManager.HasSubscriberEverConnected() {
subTrack.DownTrack().SetConnected() dt := subTrack.DownTrack()
dt.SeedState(sfu.DownTrackState{ForwarderState: p.getAndDeleteForwarderState(subTrack.ID())})
dt.SetConnected()
} }
p.TransportManager.AddSubscribedTrack(subTrack) p.TransportManager.AddSubscribedTrack(subTrack)
}) })
@@ -1641,7 +1641,7 @@ func (p *ParticipantImpl) onPublisherInitialConnected() {
func (p *ParticipantImpl) onSubscriberInitialConnected() { func (p *ParticipantImpl) onSubscriberInitialConnected() {
go p.subscriberRTCPWorker() go p.subscriberRTCPWorker()
p.setDowntracksConnected() p.setDownTracksConnected()
} }
func (p *ParticipantImpl) onPrimaryTransportInitialConnected() { func (p *ParticipantImpl) onPrimaryTransportInitialConnected() {
@@ -2453,14 +2453,35 @@ func (p *ParticipantImpl) postRtcp(pkts []rtcp.Packet) {
}, postRtcpOp{p, pkts}) }, postRtcpOp{p, pkts})
} }
func (p *ParticipantImpl) setDowntracksConnected() { func (p *ParticipantImpl) setDownTracksConnected() {
for _, t := range p.SubscriptionManager.GetSubscribedTracks() { for _, t := range p.SubscriptionManager.GetSubscribedTracks() {
if dt := t.DownTrack(); dt != nil { if dt := t.DownTrack(); dt != nil {
dt.SeedState(sfu.DownTrackState{ForwarderState: p.getAndDeleteForwarderState(t.ID())})
dt.SetConnected() dt.SetConnected()
} }
} }
} }
func (p *ParticipantImpl) cacheForwarderState() {
// if migrating in, get forwarder state from migrating out node to facilitate resume
if f := p.params.GetSubscriberForwarderState; f != nil {
if fs, err := f(p); err == nil {
p.lock.Lock()
p.forwarderState = fs
p.lock.Unlock()
}
}
}
func (p *ParticipantImpl) getAndDeleteForwarderState(trackID livekit.TrackID) *livekit.RTPForwarderState {
p.lock.Lock()
fs := p.forwarderState[trackID]
delete(p.forwarderState, trackID)
p.lock.Unlock()
return fs
}
func (p *ParticipantImpl) CacheDownTrack(trackID livekit.TrackID, rtpTransceiver *webrtc.RTPTransceiver, downTrack sfu.DownTrackState) { func (p *ParticipantImpl) CacheDownTrack(trackID livekit.TrackID, rtpTransceiver *webrtc.RTPTransceiver, downTrack sfu.DownTrackState) {
p.lock.Lock() p.lock.Lock()
if existing := p.cachedDownTracks[trackID]; existing != nil && existing.transceiver != rtpTransceiver { if existing := p.cachedDownTracks[trackID]; existing != nil && existing.transceiver != rtpTransceiver {
-1
View File
@@ -417,7 +417,6 @@ type LocalParticipant interface {
previousOffer, previousAnswer *webrtc.SessionDescription, previousOffer, previousAnswer *webrtc.SessionDescription,
mediaTracks []*livekit.TrackPublishedResponse, mediaTracks []*livekit.TrackPublishedResponse,
dataChannels []*livekit.DataChannelInfo, dataChannels []*livekit.DataChannelInfo,
forwarderStates map[livekit.TrackID]*livekit.RTPForwarderState,
) )
UpdateMediaRTT(rtt uint32) UpdateMediaRTT(rtt uint32)
@@ -782,14 +782,13 @@ type FakeLocalParticipant struct {
setMetadataArgsForCall []struct { setMetadataArgsForCall []struct {
arg1 string arg1 string
} }
SetMigrateInfoStub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState) SetMigrateInfoStub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo)
setMigrateInfoMutex sync.RWMutex setMigrateInfoMutex sync.RWMutex
setMigrateInfoArgsForCall []struct { setMigrateInfoArgsForCall []struct {
arg1 *webrtc.SessionDescription arg1 *webrtc.SessionDescription
arg2 *webrtc.SessionDescription arg2 *webrtc.SessionDescription
arg3 []*livekit.TrackPublishedResponse arg3 []*livekit.TrackPublishedResponse
arg4 []*livekit.DataChannelInfo arg4 []*livekit.DataChannelInfo
arg5 map[livekit.TrackID]*livekit.RTPForwarderState
} }
SetMigrateStateStub func(types.MigrateState) SetMigrateStateStub func(types.MigrateState)
setMigrateStateMutex sync.RWMutex setMigrateStateMutex sync.RWMutex
@@ -5204,7 +5203,7 @@ func (fake *FakeLocalParticipant) SetMetadataArgsForCall(i int) string {
return argsForCall.arg1 return argsForCall.arg1
} }
func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription, arg2 *webrtc.SessionDescription, arg3 []*livekit.TrackPublishedResponse, arg4 []*livekit.DataChannelInfo, arg5 map[livekit.TrackID]*livekit.RTPForwarderState) { func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription, arg2 *webrtc.SessionDescription, arg3 []*livekit.TrackPublishedResponse, arg4 []*livekit.DataChannelInfo) {
var arg3Copy []*livekit.TrackPublishedResponse var arg3Copy []*livekit.TrackPublishedResponse
if arg3 != nil { if arg3 != nil {
arg3Copy = make([]*livekit.TrackPublishedResponse, len(arg3)) arg3Copy = make([]*livekit.TrackPublishedResponse, len(arg3))
@@ -5221,13 +5220,12 @@ func (fake *FakeLocalParticipant) SetMigrateInfo(arg1 *webrtc.SessionDescription
arg2 *webrtc.SessionDescription arg2 *webrtc.SessionDescription
arg3 []*livekit.TrackPublishedResponse arg3 []*livekit.TrackPublishedResponse
arg4 []*livekit.DataChannelInfo arg4 []*livekit.DataChannelInfo
arg5 map[livekit.TrackID]*livekit.RTPForwarderState }{arg1, arg2, arg3Copy, arg4Copy})
}{arg1, arg2, arg3Copy, arg4Copy, arg5})
stub := fake.SetMigrateInfoStub stub := fake.SetMigrateInfoStub
fake.recordInvocation("SetMigrateInfo", []interface{}{arg1, arg2, arg3Copy, arg4Copy, arg5}) fake.recordInvocation("SetMigrateInfo", []interface{}{arg1, arg2, arg3Copy, arg4Copy})
fake.setMigrateInfoMutex.Unlock() fake.setMigrateInfoMutex.Unlock()
if stub != nil { if stub != nil {
fake.SetMigrateInfoStub(arg1, arg2, arg3, arg4, arg5) fake.SetMigrateInfoStub(arg1, arg2, arg3, arg4)
} }
} }
@@ -5237,17 +5235,17 @@ func (fake *FakeLocalParticipant) SetMigrateInfoCallCount() int {
return len(fake.setMigrateInfoArgsForCall) return len(fake.setMigrateInfoArgsForCall)
} }
func (fake *FakeLocalParticipant) SetMigrateInfoCalls(stub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState)) { func (fake *FakeLocalParticipant) SetMigrateInfoCalls(stub func(*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo)) {
fake.setMigrateInfoMutex.Lock() fake.setMigrateInfoMutex.Lock()
defer fake.setMigrateInfoMutex.Unlock() defer fake.setMigrateInfoMutex.Unlock()
fake.SetMigrateInfoStub = stub fake.SetMigrateInfoStub = stub
} }
func (fake *FakeLocalParticipant) SetMigrateInfoArgsForCall(i int) (*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo, map[livekit.TrackID]*livekit.RTPForwarderState) { func (fake *FakeLocalParticipant) SetMigrateInfoArgsForCall(i int) (*webrtc.SessionDescription, *webrtc.SessionDescription, []*livekit.TrackPublishedResponse, []*livekit.DataChannelInfo) {
fake.setMigrateInfoMutex.RLock() fake.setMigrateInfoMutex.RLock()
defer fake.setMigrateInfoMutex.RUnlock() defer fake.setMigrateInfoMutex.RUnlock()
argsForCall := fake.setMigrateInfoArgsForCall[i] argsForCall := fake.setMigrateInfoArgsForCall[i]
return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4, argsForCall.arg5 return argsForCall.arg1, argsForCall.arg2, argsForCall.arg3, argsForCall.arg4
} }
func (fake *FakeLocalParticipant) SetMigrateState(arg1 types.MigrateState) { func (fake *FakeLocalParticipant) SetMigrateState(arg1 types.MigrateState) {
+10 -2
View File
@@ -385,11 +385,13 @@ func (f *Forwarder) GetState() *livekit.RTPForwarderState {
state := &livekit.RTPForwarderState{ state := &livekit.RTPForwarderState{
Started: f.started, Started: f.started,
ReferenceLayerSpatial: f.referenceLayerSpatial, ReferenceLayerSpatial: f.referenceLayerSpatial,
PreStartTime: f.preStartTime.UnixNano(),
ExtFirstTimestamp: f.extFirstTS, ExtFirstTimestamp: f.extFirstTS,
DummyStartTimestampOffset: f.dummyStartTSOffset, DummyStartTimestampOffset: f.dummyStartTSOffset,
RtpMunger: f.rtpMunger.GetState(), RtpMunger: f.rtpMunger.GetState(),
} }
if !f.preStartTime.IsZero() {
state.PreStartTime = f.preStartTime.UnixNano()
}
codecMungerState := f.codecMunger.GetState() codecMungerState := f.codecMunger.GetState()
if vp8MungerState, ok := codecMungerState.(*livekit.VP8MungerState); ok { if vp8MungerState, ok := codecMungerState.(*livekit.VP8MungerState); ok {
@@ -413,7 +415,9 @@ func (f *Forwarder) SeedState(state *livekit.RTPForwarderState) {
f.started = true f.started = true
f.referenceLayerSpatial = state.ReferenceLayerSpatial f.referenceLayerSpatial = state.ReferenceLayerSpatial
f.preStartTime = time.Unix(0, state.PreStartTime) if state.PreStartTime != 0 {
f.preStartTime = time.Unix(0, state.PreStartTime)
}
f.extFirstTS = state.ExtFirstTimestamp f.extFirstTS = state.ExtFirstTimestamp
f.dummyStartTSOffset = state.DummyStartTimestampOffset f.dummyStartTSOffset = state.DummyStartTimestampOffset
} }
@@ -1633,12 +1637,14 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
f.logger.Debugw( f.logger.Debugw(
message, message,
"layer", layer, "layer", layer,
"referenceLayerSpatial", f.referenceLayerSpatial,
"extExpectedTS", extExpectedTS, "extExpectedTS", extExpectedTS,
"incomingTS", extPkt.Packet.Timestamp, "incomingTS", extPkt.Packet.Timestamp,
"extIncomingTS", extPkt.ExtTimestamp, "extIncomingTS", extPkt.ExtTimestamp,
"extRefTS", extRefTS, "extRefTS", extRefTS,
"extLastTS", extLastTS, "extLastTS", extLastTS,
"diffSeconds", math.Abs(diffSeconds), "diffSeconds", math.Abs(diffSeconds),
"refInfos", wrappedRefInfoLogger{f},
) )
} }
// TODO-REMOVE-AFTER-DATA-COLLECTION // TODO-REMOVE-AFTER-DATA-COLLECTION
@@ -1646,12 +1652,14 @@ func (f *Forwarder) processSourceSwitch(extPkt *buffer.ExtPacket, layer int32) e
f.logger.Infow( f.logger.Infow(
message, message,
"layer", layer, "layer", layer,
"referenceLayerSpatial", f.referenceLayerSpatial,
"extExpectedTS", extExpectedTS, "extExpectedTS", extExpectedTS,
"incomingTS", extPkt.Packet.Timestamp, "incomingTS", extPkt.Packet.Timestamp,
"extIncomingTS", extPkt.ExtTimestamp, "extIncomingTS", extPkt.ExtTimestamp,
"extRefTS", extRefTS, "extRefTS", extRefTS,
"extLastTS", extLastTS, "extLastTS", extLastTS,
"diffSeconds", math.Abs(diffSeconds), "diffSeconds", math.Abs(diffSeconds),
"refInfos", wrappedRefInfoLogger{f},
) )
} }