mirror of
https://github.com/livekit/livekit.git
synced 2026-09-01 20:09:08 +00:00
fix deadlocks in track subscription
This commit is contained in:
@@ -212,8 +212,11 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
|
||||
t.subscribedTracks[sub.ID()] = subTrack
|
||||
|
||||
t.receiver.AddDownTrack(downTrack, true)
|
||||
sub.AddSubscribedTrack(t.params.ParticipantID, subTrack)
|
||||
sub.Negotiate()
|
||||
// since sub will lock, run it in a gorountine to avoid deadlocks
|
||||
go func() {
|
||||
sub.AddSubscribedTrack(t.params.ParticipantID, subTrack)
|
||||
sub.Negotiate()
|
||||
}()
|
||||
|
||||
t.params.Stats.AddSubscribedTrack(t.kind.String())
|
||||
return nil
|
||||
|
||||
+5
-4
@@ -318,14 +318,12 @@ func (r *Room) OnParticipantChanged(f func(participant types.Participant)) {
|
||||
r.onParticipantChanged = f
|
||||
}
|
||||
|
||||
// checks if participant should be autosubscribed to new tracks, assumes lock is already acquired
|
||||
func (r *Room) autoSubscribe(participant types.Participant) bool {
|
||||
if !participant.CanSubscribe() {
|
||||
return false
|
||||
}
|
||||
|
||||
r.lock.RLock()
|
||||
defer r.lock.RUnlock()
|
||||
|
||||
opts := r.participantOpts[participant.Identity()]
|
||||
// default to true if no options are set
|
||||
if opts != nil && !opts.AutoSubscribe {
|
||||
@@ -402,7 +400,10 @@ func (r *Room) onDataPacket(source types.Participant, dp *livekit.DataPacket) {
|
||||
}
|
||||
|
||||
func (r *Room) subscribeToExistingTracks(p types.Participant) {
|
||||
if !r.autoSubscribe(p) {
|
||||
r.lock.RLock()
|
||||
shouldSubscribe := r.autoSubscribe(p)
|
||||
r.lock.RUnlock()
|
||||
if !shouldSubscribe {
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user