fix repeated AddSubscriber of MediaTrackSubscription
@@ -31,6 +31,7 @@ type MediaTrackSubscriptions struct {
subscribedTracksMu sync.RWMutex
subscribedTracks map[livekit.ParticipantID]types.SubscribedTrack
+ pendingSubscribeTracks sync.Map // livekit.ParticipantID -> bool
pendingClose map[livekit.ParticipantID]types.SubscribedTrack
onNoSubscribers func()
@@ -108,6 +109,12 @@ func (t *MediaTrackSubscriptions) AddSubscriber(sub types.LocalParticipant, code
trackID := t.params.MediaTrack.ID()
subscriberID := sub.ID()
+ if _, pending := t.pendingSubscribeTracks.LoadOrStore(subscriberID, true); pending {
+ return nil, nil
+ } else {
+ defer t.pendingSubscribeTracks.Delete(subscriberID)
+ }
+
// don't subscribe to the same track multiple times
t.subscribedTracksMu.Lock()
if _, ok := t.subscribedTracks[subscriberID]; ok {
