fix repeated AddSubscriber of MediaTrackSubscription (#723)

This commit is contained in:
cnderrauber
2022-05-27 15:42:17 +08:00
committed by David Zhao
parent 29f99b9ae9
commit fff6f84856
+10 -3
View File
@@ -29,9 +29,10 @@ const (
type MediaTrackSubscriptions struct {
params MediaTrackSubscriptionsParams
subscribedTracksMu sync.RWMutex
subscribedTracks map[livekit.ParticipantID]types.SubscribedTrack
pendingClose map[livekit.ParticipantID]types.SubscribedTrack
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 {