Cache transceiver before closing subscribed track. (#2594)

On migration, when subscription moved from remote -> local,
transceiver caching was racing. Although a very small possibility,
it could happen like so

1. down track close
2. down track close callback fires go routine to close subscribed track
3. subscribed track close handler in subscription manager tries to
   reconcile
4. reconcile adds subscribed track again
5. cannot find cached transceiver as caching happens after down track
   close finishes in stap 1 above. Although there are a couple of
   gortouine jumps (step 2 fires a goroutine to close subscribed track
   and step 4 will reconcile in a goroutine too), it is theoretically
   possible that the step 1 has not finished and hence transceiver is
   not cached.

Fix is to move caching to before closing subscribed track.
This commit is contained in:
Raja Subramanian
2024-03-22 11:56:50 +05:30
committed by GitHub
parent 0f597e7e46
commit ffb831aa8c
4 changed files with 40 additions and 25 deletions
+10 -7
View File
@@ -322,13 +322,6 @@ func (t *MediaTrackSubscriptions) closeSubscribedTrack(subTrack types.Subscribed
if willBeResumed { if willBeResumed {
dt.CloseWithFlush(false) dt.CloseWithFlush(false)
// cache transceiver for potential re-use on resume
tr := dt.GetTransceiver()
if tr != nil {
sub := subTrack.Subscriber()
sub.CacheDownTrack(subTrack.ID(), tr, dt.GetState())
}
} else { } else {
// flushing blocks, avoid blocking when publisher removes all its subscribers // flushing blocks, avoid blocking when publisher removes all its subscribers
go dt.CloseWithFlush(true) go dt.CloseWithFlush(true)
@@ -426,6 +419,16 @@ func (t *MediaTrackSubscriptions) downTrackClosed(
t.subscribedTracksMu.Unlock() t.subscribedTracksMu.Unlock()
if subTrack != nil { if subTrack != nil {
// cache transceiver for potential re-use on resume
if willBeResumed {
dt := subTrack.DownTrack()
tr := dt.GetTransceiver()
if tr != nil {
sub := subTrack.Subscriber()
sub.CacheDownTrack(subTrack.ID(), tr, dt.GetState())
}
}
subTrack.Close(willBeResumed) subTrack.Close(willBeResumed)
} }
} }
+5 -2
View File
@@ -2362,6 +2362,7 @@ func (p *ParticipantImpl) CacheDownTrack(trackID livekit.TrackID, rtpTransceiver
p.subLogger.Infow("cached transceiver changed", "trackID", trackID) p.subLogger.Infow("cached transceiver changed", "trackID", trackID)
} }
p.cachedDownTracks[trackID] = &downTrackState{transceiver: rtpTransceiver, downTrack: downTrack} p.cachedDownTracks[trackID] = &downTrackState{transceiver: rtpTransceiver, downTrack: downTrack}
p.subLogger.Debugw("caching downtrack", "trackID", trackID)
p.lock.Unlock() p.lock.Unlock()
} }
@@ -2369,6 +2370,9 @@ func (p *ParticipantImpl) UncacheDownTrack(rtpTransceiver *webrtc.RTPTransceiver
p.lock.Lock() p.lock.Lock()
for trackID, dts := range p.cachedDownTracks { for trackID, dts := range p.cachedDownTracks {
if dts.transceiver == rtpTransceiver { if dts.transceiver == rtpTransceiver {
if dts := p.cachedDownTracks[trackID]; dts != nil {
p.subLogger.Debugw("uncaching downtrack", "trackID", trackID)
}
delete(p.cachedDownTracks, trackID) delete(p.cachedDownTracks, trackID)
break break
} }
@@ -2380,8 +2384,7 @@ func (p *ParticipantImpl) GetCachedDownTrack(trackID livekit.TrackID) (*webrtc.R
p.lock.RLock() p.lock.RLock()
defer p.lock.RUnlock() defer p.lock.RUnlock()
dts := p.cachedDownTracks[trackID] if dts := p.cachedDownTracks[trackID]; dts != nil {
if dts != nil {
return dts.transceiver, dts.downTrack return dts.transceiver, dts.downTrack
} }
+18 -15
View File
@@ -533,13 +533,9 @@ func (d *DownTrack) SubscriberID() livekit.ParticipantID {
// Sets RTP header extensions for this track // Sets RTP header extensions for this track
func (d *DownTrack) SetRTPHeaderExtensions(rtpHeaderExtensions []webrtc.RTPHeaderExtensionParameter) { func (d *DownTrack) SetRTPHeaderExtensions(rtpHeaderExtensions []webrtc.RTPHeaderExtensionParameter) {
d.streamAllocatorLock.RLock()
listener := d.streamAllocatorListener
d.streamAllocatorLock.RUnlock()
isBWEEnabled := true isBWEEnabled := true
if listener != nil { if sal := d.getStreamAllocatorListener(); sal != nil {
isBWEEnabled = listener.IsBWEEnabled(d) isBWEEnabled = sal.IsBWEEnabled(d)
} }
for _, ext := range rtpHeaderExtensions { for _, ext := range rtpHeaderExtensions {
switch ext.URI { switch ext.URI {
@@ -669,13 +665,23 @@ func (d *DownTrack) maxLayerNotifierWorker() {
d.params.Logger.Debugw("max subscribed layer processed", "layer", maxLayerSpatial, "event", event) d.params.Logger.Debugw("max subscribed layer processed", "layer", maxLayerSpatial, "event", event)
if onMaxSubscribedLayerChanged := d.getOnMaxLayerChanged(); onMaxSubscribedLayerChanged != nil { if onMaxSubscribedLayerChanged := d.getOnMaxLayerChanged(); onMaxSubscribedLayerChanged != nil {
d.params.Logger.Debugw("notifying max subscribed layer", "layer", maxLayerSpatial, "event", event) d.params.Logger.Debugw(
"notifying max subscribed layer",
"layer", maxLayerSpatial,
"event", event,
"subscriberID", d.SubscriberID(),
)
onMaxSubscribedLayerChanged(d, maxLayerSpatial) onMaxSubscribedLayerChanged(d, maxLayerSpatial)
} }
} }
if onMaxSubscribedLayerChanged := d.getOnMaxLayerChanged(); onMaxSubscribedLayerChanged != nil { if onMaxSubscribedLayerChanged := d.getOnMaxLayerChanged(); onMaxSubscribedLayerChanged != nil {
d.params.Logger.Debugw("notifying max subscribed layer", "layer", buffer.InvalidLayerSpatial, "event", "close") d.params.Logger.Debugw(
"notifying max subscribed layer",
"layer", buffer.InvalidLayerSpatial,
"event", "close",
"subscriberID", d.SubscriberID(),
)
onMaxSubscribedLayerChanged(d, buffer.InvalidLayerSpatial) onMaxSubscribedLayerChanged(d, buffer.InvalidLayerSpatial)
} }
} }
@@ -865,13 +871,9 @@ func (d *DownTrack) WritePaddingRTP(bytesToSend int, paddingOnMute bool, forceMa
// Mute enables or disables media forwarding - subscriber triggered // Mute enables or disables media forwarding - subscriber triggered
func (d *DownTrack) Mute(muted bool) { func (d *DownTrack) Mute(muted bool) {
d.streamAllocatorLock.RLock()
listener := d.streamAllocatorListener
d.streamAllocatorLock.RUnlock()
isSubscribeMutable := true isSubscribeMutable := true
if listener != nil { if sal := d.getStreamAllocatorListener(); sal != nil {
isSubscribeMutable = listener.IsSubscribeMutable(d) isSubscribeMutable = sal.IsSubscribeMutable(d)
} }
changed := d.forwarder.Mute(muted, isSubscribeMutable) changed := d.forwarder.Mute(muted, isSubscribeMutable)
d.handleMute(muted, changed) d.handleMute(muted, changed)
@@ -987,9 +989,10 @@ func (d *DownTrack) CloseWithFlush(flush bool) {
d.rtcpReader.Close() d.rtcpReader.Close()
d.rtcpReader.OnPacket(nil) d.rtcpReader.OnPacket(nil)
} }
d.bindLock.Unlock() d.bindLock.Unlock()
d.connectionStats.Close() d.connectionStats.Close()
d.rtpStats.Stop() d.rtpStats.Stop()
d.params.Logger.Debugw("rtp stats", d.params.Logger.Debugw("rtp stats",
"direction", "downstream", "direction", "downstream",
+7 -1
View File
@@ -241,10 +241,16 @@ func (s *StreamAllocator) AddTrack(downTrack *sfu.DownTrack, params AddTrackPara
track := NewTrack(downTrack, params.Source, params.IsSimulcast, params.PublisherID, s.params.Logger) track := NewTrack(downTrack, params.Source, params.IsSimulcast, params.PublisherID, s.params.Logger)
track.SetPriority(params.Priority) track.SetPriority(params.Priority)
trackID := livekit.TrackID(downTrack.ID())
s.videoTracksMu.Lock() s.videoTracksMu.Lock()
s.videoTracks[livekit.TrackID(downTrack.ID())] = track oldTrack := s.videoTracks[trackID]
s.videoTracks[trackID] = track
s.videoTracksMu.Unlock() s.videoTracksMu.Unlock()
if oldTrack != nil {
oldTrack.DownTrack().SetStreamAllocatorListener(nil)
}
downTrack.SetStreamAllocatorListener(s) downTrack.SetStreamAllocatorListener(s)
if s.prober.IsRunning() { if s.prober.IsRunning() {
// STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate // STREAM-ALLOCATOR-TODO: this can be changed to adapt to probe rate