Fixed deadlocks with updated simulcast logic

This commit is contained in:
David Zhao
2021-06-25 17:12:22 -07:00
parent 871ecd0366
commit c0c38f2e1d
7 changed files with 21 additions and 15 deletions
+14 -8
View File
@@ -173,14 +173,15 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
subTrack.SetPublisherMuted(t.IsMuted())
go t.sendDownTrackBindingReports(sub)
})
downTrack.OnCloseHandler(func() {
t.lock.Lock()
delete(t.subscribedTracks, sub.ID())
t.lock.Unlock()
t.params.Stats.SubSubscribedTrack(t.kind.String())
go func() {
t.lock.Lock()
delete(t.subscribedTracks, sub.ID())
t.lock.Unlock()
t.params.Stats.SubSubscribedTrack(t.kind.String())
// ignore if the subscribing sub is not connected
if sub.SubscriberPC().ConnectionState() == webrtc.PeerConnectionStateClosed {
return
@@ -214,7 +215,7 @@ func (t *MediaTrack) AddSubscriber(sub types.Participant) error {
t.subscribedTracks[sub.ID()] = subTrack
t.receiver.AddDownTrack(downTrack, len(t.subscribedTracks) < 10)
t.receiver.AddDownTrack(downTrack, t.shouldStartWithBestQuality())
// since sub will lock, run it in a gorountine to avoid deadlocks
go func() {
sub.AddSubscribedTrack(t.params.ParticipantID, subTrack)
@@ -285,7 +286,7 @@ func (t *MediaTrack) AddReceiver(receiver *webrtc.RTPReceiver, track *webrtc.Tra
})
t.params.Stats.AddPublishedTrack(t.kind.String())
}
t.receiver.AddUpTrack(track, buff, false)
t.receiver.AddUpTrack(track, buff, t.shouldStartWithBestQuality())
// when RID is set, track is simulcasted
t.simulcasted = track.RID() != ""
@@ -327,6 +328,11 @@ func (t *MediaTrack) ToProto() *livekit.TrackInfo {
}
}
// this function assumes caller holds lock
func (t *MediaTrack) shouldStartWithBestQuality() bool {
return len(t.subscribedTracks) < 10
}
// TODO: send for all downtracks from the source participant
// https://tools.ietf.org/html/rfc7941
func (t *MediaTrack) sendDownTrackBindingReports(sub types.Participant) {
+1 -1
View File
@@ -533,8 +533,8 @@ func (p *ParticipantImpl) SendDataPacket(dp *livekit.DataPacket) error {
func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool) {
p.lock.RLock()
defer p.lock.RUnlock()
track := p.publishedTracks[trackId]
p.lock.RUnlock()
if track == nil {
logger.Warnw("could not locate track", nil, "track", trackId)
return
+1 -1
View File
@@ -62,7 +62,7 @@ func (t *SubscribedTrack) updateDownTrackMute() {
t.dt.Mute(muted)
}
func spatialLayerForQuality(quality livekit.VideoQuality) int64 {
func spatialLayerForQuality(quality livekit.VideoQuality) int32 {
switch quality {
case livekit.VideoQuality_LOW:
return 0