Fix mute of simulcasted video tracks with < 3 encodings (#114)

This commit is contained in:
David Zhao
2021-09-14 15:50:34 -07:00
committed by GitHub
parent 95a5e3ca85
commit 20d0c0ca43
2 changed files with 15 additions and 15 deletions
+14 -14
View File
@@ -28,6 +28,7 @@ const (
lossyDataChannel = "_lossy" lossyDataChannel = "_lossy"
reliableDataChannel = "_reliable" reliableDataChannel = "_reliable"
sdBatchSize = 20 sdBatchSize = 20
trackCleanupDelay = 5 * time.Second
) )
type ParticipantParams struct { type ParticipantParams struct {
@@ -582,12 +583,10 @@ func (p *ParticipantImpl) SetTrackMuted(trackId string, muted bool, fromAdmin bo
track := p.publishedTracks[trackId] track := p.publishedTracks[trackId]
p.lock.RUnlock() p.lock.RUnlock()
// already handled
if isPending {
return
}
if track == nil { if track == nil {
logger.Warnw("could not locate track", nil, "track", trackId) if !isPending {
logger.Warnw("could not locate track", nil, "track", trackId)
}
return return
} }
currentMuted := track.IsMuted() currentMuted := track.IsMuted()
@@ -783,12 +782,13 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
// use existing mediatrack to handle simulcast // use existing mediatrack to handle simulcast
p.lock.Lock() p.lock.Lock()
ti := p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()), false) cid, ti := p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()))
if ti == nil { if ti == nil {
p.lock.Unlock() p.lock.Unlock()
return return
} }
ptrack := p.publishedTracks[ti.Sid] ptrack := p.publishedTracks[ti.Sid]
p.lock.Unlock()
var mt *MediaTrack var mt *MediaTrack
var newTrack bool var newTrack bool
@@ -822,10 +822,12 @@ func (p *ParticipantImpl) onMediaTrack(track *webrtc.TrackRemote, rtpReceiver *w
mt.AddReceiver(rtpReceiver, track, p.twcc) mt.AddReceiver(rtpReceiver, track, p.twcc)
// cleanup pendingTracks // cleanup pendingTracks
if !mt.simulcasted || mt.NumUpTracks() == 3 { defer func() {
_ = p.getPendingTrack(track.ID(), ToProtoTrackKind(track.Kind()), true) time.Sleep(trackCleanupDelay)
} p.lock.Lock()
p.lock.Unlock() delete(p.pendingTracks, cid)
p.lock.Unlock()
}()
if newTrack { if newTrack {
p.handleTrackPublished(mt) p.handleTrackPublished(mt)
@@ -852,7 +854,7 @@ func (p *ParticipantImpl) onDataChannel(dc *webrtc.DataChannel) {
} }
} }
func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType, deleteAfter bool) *livekit.TrackInfo { func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackType) (string, *livekit.TrackInfo) {
ti := p.pendingTracks[clientId] ti := p.pendingTracks[clientId]
// then find the first one that matches type. with MediaStreamTrack, it's possible for the client id to // then find the first one that matches type. with MediaStreamTrack, it's possible for the client id to
@@ -870,10 +872,8 @@ func (p *ParticipantImpl) getPendingTrack(clientId string, kind livekit.TrackTyp
// if still not found, we are done // if still not found, we are done
if ti == nil { if ti == nil {
logger.Errorw("track info not published prior to track", nil, "clientId", clientId) logger.Errorw("track info not published prior to track", nil, "clientId", clientId)
} else if deleteAfter {
delete(p.pendingTracks, clientId)
} }
return ti return clientId, ti
} }
func (p *ParticipantImpl) handleDataMessage(kind livekit.DataPacket_Kind, data []byte) { func (p *ParticipantImpl) handleDataMessage(kind livekit.DataPacket_Kind, data []byte) {
+1 -1
View File
@@ -178,7 +178,7 @@ func TestMuteSetting(t *testing.T) {
Muted: true, Muted: true,
}) })
ti := p.getPendingTrack("cid", livekit.TrackType_AUDIO, false) _, ti := p.getPendingTrack("cid", livekit.TrackType_AUDIO)
require.NotNil(t, ti) require.NotNil(t, ti)
require.True(t, ti.Muted) require.True(t, ti.Muted)
}) })