use IsReady instead of checking for multiple statuses

This commit is contained in:
David Zhao
2021-01-10 22:09:36 -08:00
parent 7dcb88343a
commit 456df0fb91
7 changed files with 130 additions and 22 deletions
+3 -5
View File
@@ -262,14 +262,12 @@ func (t *MediaTrack) forwardRTPWorker() {
if delta < maxPLIFrequency { if delta < maxPLIFrequency {
continue continue
} }
logger.GetLogger().Infow("keyframe required, sending PLI") logger.GetLogger().Infow("keyframe required, sending PLI",
"srcParticipant", t.participantId)
rtcpPkts := []rtcp.Packet{ rtcpPkts := []rtcp.Packet{
&rtcp.PictureLossIndication{SenderSSRC: uint32(t.ssrc), MediaSSRC: pkt.SSRC}, &rtcp.PictureLossIndication{SenderSSRC: uint32(t.ssrc), MediaSSRC: pkt.SSRC},
} }
// queue up a PLI, but don't block channel t.rtcpCh <- rtcpPkts
go func() {
t.rtcpCh <- rtcpPkts
}()
t.lastPLI = time.Now() t.lastPLI = time.Now()
} else if err != nil { } else if err != nil {
logger.GetLogger().Warnw("could not forward packet to participant", logger.GetLogger().Warnw("could not forward packet to participant",
+9 -6
View File
@@ -85,7 +85,7 @@ func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name str
receiverConfig: receiverConfig, receiverConfig: receiverConfig,
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
rtcpCh: make(chan []rtcp.Packet, 10), rtcpCh: make(chan []rtcp.Packet, 50),
subscribedTracks: make(map[string][]*sfu.DownTrack), subscribedTracks: make(map[string][]*sfu.DownTrack),
state: livekit.ParticipantInfo_JOINING, state: livekit.ParticipantInfo_JOINING,
lock: sync.RWMutex{}, lock: sync.RWMutex{},
@@ -133,11 +133,10 @@ func NewParticipant(pc types.PeerConnection, sc types.SignalConnection, name str
// only set after answered // only set after answered
pc.OnNegotiationNeeded(func() { pc.OnNegotiationNeeded(func() {
if participant.state != livekit.ParticipantInfo_JOINED && participant.state != livekit.ParticipantInfo_ACTIVE { if !participant.IsReady() {
// ignore negotiation requests before connected // ignore negotiation requests before connected
return return
} }
logger.GetLogger().Debugw("negotiation needed", "participantId", participant.ID())
participant.scheduleNegotiate() participant.scheduleNegotiate()
}) })
@@ -156,6 +155,10 @@ func (p *ParticipantImpl) State() livekit.ParticipantInfo_State {
return p.state return p.state
} }
func (p *ParticipantImpl) IsReady() bool {
return p.state == livekit.ParticipantInfo_JOINED || p.state == livekit.ParticipantInfo_ACTIVE
}
func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo { func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
info := &livekit.ParticipantInfo{ info := &livekit.ParticipantInfo{
Sid: p.id, Sid: p.id,
@@ -625,9 +628,9 @@ func (p *ParticipantImpl) downTracksRTCPWorker() {
func (p *ParticipantImpl) rtcpSendWorker() { func (p *ParticipantImpl) rtcpSendWorker() {
// read from rtcpChan // read from rtcpChan
for pkts := range p.rtcpCh { for pkts := range p.rtcpCh {
for _, pkt := range pkts { //for _, pkt := range pkts {
logger.GetLogger().Debugw("writing RTCP", "packet", pkt) // logger.GetLogger().Debugw("writing RTCP", "packet", pkt)
} //}
if err := p.peerConn.WriteRTCP(pkts); err != nil { if err := p.peerConn.WriteRTCP(pkts); err != nil {
logger.GetLogger().Errorw("could not write RTCP to participant", logger.GetLogger().Errorw("could not write RTCP to participant",
"participant", p.id, "participant", p.id,
+42
View File
@@ -0,0 +1,42 @@
package rtc
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/livekit/livekit-server/proto/livekit"
)
func TestIsReady(t *testing.T) {
tests := []struct {
state livekit.ParticipantInfo_State
ready bool
}{
{
state: livekit.ParticipantInfo_JOINING,
ready: false,
},
{
state: livekit.ParticipantInfo_JOINED,
ready: true,
},
{
state: livekit.ParticipantInfo_ACTIVE,
ready: true,
},
{
state: livekit.ParticipantInfo_DISCONNECTED,
ready: false,
},
}
for _, test := range tests {
t.Run(test.state.String(), func(t *testing.T) {
p := &ParticipantImpl{
state: test.state,
}
assert.Equal(t, test.ready, p.IsReady())
})
}
}
+1 -1
View File
@@ -141,7 +141,7 @@ func (r *Room) onTrackAdded(participant types.Participant, track types.Published
// skip publishing participant // skip publishing participant
continue continue
} }
if existingParticipant.State() != livekit.ParticipantInfo_JOINED && existingParticipant.State() != livekit.ParticipantInfo_ACTIVE { if !existingParticipant.IsReady() {
// not fully joined. don't subscribe yet // not fully joined. don't subscribe yet
continue continue
} }
+9 -10
View File
@@ -100,25 +100,24 @@ func TestRoomJoin(t *testing.T) {
} }
func TestNewTrack(t *testing.T) { func TestNewTrack(t *testing.T) {
t.Run("new track should be added to connected participants", func(t *testing.T) { t.Run("new track should be added to ready participants", func(t *testing.T) {
rm := newRoomWithParticipants(t, 4) rm := newRoomWithParticipants(t, 3)
participants := rm.GetParticipants() participants := rm.GetParticipants()
p0 := participants[0].(*typesfakes.FakeParticipant) p0 := participants[0].(*typesfakes.FakeParticipant)
p0.StateReturns(livekit.ParticipantInfo_JOINING) p0.IsReadyReturns(false)
p1 := participants[1].(*typesfakes.FakeParticipant) p1 := participants[1].(*typesfakes.FakeParticipant)
p1.StateReturns(livekit.ParticipantInfo_DISCONNECTED) p1.IsReadyReturns(true)
p2 := participants[2].(*typesfakes.FakeParticipant)
p2.StateReturns(livekit.ParticipantInfo_JOINED) pub := participants[2].(*typesfakes.FakeParticipant)
p3 := participants[3].(*typesfakes.FakeParticipant)
// p3 adds track // p3 adds track
track := newMockTrack(livekit.TrackType_VIDEO, "webcam") track := newMockTrack(livekit.TrackType_VIDEO, "webcam")
trackCB := p3.OnTrackPublishedArgsForCall(0) trackCB := pub.OnTrackPublishedArgsForCall(0)
assert.NotNil(t, trackCB) assert.NotNil(t, trackCB)
trackCB(p3, track) trackCB(pub, track)
// only p2 should've been called // only p2 should've been called
assert.Equal(t, 1, track.AddSubscriberCallCount()) assert.Equal(t, 1, track.AddSubscriberCallCount())
assert.Equal(t, p2, track.AddSubscriberArgsForCall(0)) assert.Equal(t, p1, track.AddSubscriberArgsForCall(0))
}) })
} }
+1
View File
@@ -54,6 +54,7 @@ type Participant interface {
ID() string ID() string
Name() string Name() string
State() livekit.ParticipantInfo_State State() livekit.ParticipantInfo_State
IsReady() bool
ToProto() *livekit.ParticipantInfo ToProto() *livekit.ParticipantInfo
AddTrack(clientId, name string, trackType livekit.TrackType) AddTrack(clientId, name string, trackType livekit.TrackType)
RemoveTrack(sid string) error RemoveTrack(sid string) error
@@ -90,6 +90,16 @@ type FakeParticipant struct {
iDReturnsOnCall map[int]struct { iDReturnsOnCall map[int]struct {
result1 string result1 string
} }
IsReadyStub func() bool
isReadyMutex sync.RWMutex
isReadyArgsForCall []struct {
}
isReadyReturns struct {
result1 bool
}
isReadyReturnsOnCall map[int]struct {
result1 bool
}
NameStub func() string NameStub func() string
nameMutex sync.RWMutex nameMutex sync.RWMutex
nameArgsForCall []struct { nameArgsForCall []struct {
@@ -634,6 +644,59 @@ func (fake *FakeParticipant) IDReturnsOnCall(i int, result1 string) {
}{result1} }{result1}
} }
func (fake *FakeParticipant) IsReady() bool {
fake.isReadyMutex.Lock()
ret, specificReturn := fake.isReadyReturnsOnCall[len(fake.isReadyArgsForCall)]
fake.isReadyArgsForCall = append(fake.isReadyArgsForCall, struct {
}{})
stub := fake.IsReadyStub
fakeReturns := fake.isReadyReturns
fake.recordInvocation("IsReady", []interface{}{})
fake.isReadyMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) IsReadyCallCount() int {
fake.isReadyMutex.RLock()
defer fake.isReadyMutex.RUnlock()
return len(fake.isReadyArgsForCall)
}
func (fake *FakeParticipant) IsReadyCalls(stub func() bool) {
fake.isReadyMutex.Lock()
defer fake.isReadyMutex.Unlock()
fake.IsReadyStub = stub
}
func (fake *FakeParticipant) IsReadyReturns(result1 bool) {
fake.isReadyMutex.Lock()
defer fake.isReadyMutex.Unlock()
fake.IsReadyStub = nil
fake.isReadyReturns = struct {
result1 bool
}{result1}
}
func (fake *FakeParticipant) IsReadyReturnsOnCall(i int, result1 bool) {
fake.isReadyMutex.Lock()
defer fake.isReadyMutex.Unlock()
fake.IsReadyStub = nil
if fake.isReadyReturnsOnCall == nil {
fake.isReadyReturnsOnCall = make(map[int]struct {
result1 bool
})
}
fake.isReadyReturnsOnCall[i] = struct {
result1 bool
}{result1}
}
func (fake *FakeParticipant) Name() string { func (fake *FakeParticipant) Name() string {
fake.nameMutex.Lock() fake.nameMutex.Lock()
ret, specificReturn := fake.nameReturnsOnCall[len(fake.nameArgsForCall)] ret, specificReturn := fake.nameReturnsOnCall[len(fake.nameArgsForCall)]
@@ -1341,6 +1404,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.handleAnswerMutex.RUnlock() defer fake.handleAnswerMutex.RUnlock()
fake.iDMutex.RLock() fake.iDMutex.RLock()
defer fake.iDMutex.RUnlock() defer fake.iDMutex.RUnlock()
fake.isReadyMutex.RLock()
defer fake.isReadyMutex.RUnlock()
fake.nameMutex.RLock() fake.nameMutex.RLock()
defer fake.nameMutex.RUnlock() defer fake.nameMutex.RUnlock()
fake.onCloseMutex.RLock() fake.onCloseMutex.RLock()