return participant connected timestamps

This commit is contained in:
David Zhao
2021-03-28 21:05:20 -07:00
parent 8da7b17b95
commit b90f851be5
6 changed files with 158 additions and 39 deletions
+26 -12
View File
@@ -35,11 +35,15 @@ type ParticipantImpl struct {
isClosed utils.AtomicFlag
conf *WebRTCConfig
identity string
permission *livekit.ParticipantPermission
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
// when first connected
connectedAt atomic.Value // time.Time
// JSON encoded metadata to pass to clients
metadata string
permission *livekit.ParticipantPermission
state atomic.Value // livekit.ParticipantInfo_State
rtcpCh chan []rtcp.Packet
metadata string
// hold reference for MediaTrack
twcc *twcc.Responder
@@ -77,6 +81,8 @@ func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink,
publishedTracks: make(map[string]types.PublishedTrack, 0),
pendingTracks: make(map[string]*livekit.TrackInfo),
}
// store empty value
p.connectedAt.Store(time.Time{})
p.state.Store(livekit.ParticipantInfo_JOINING)
var err error
@@ -102,14 +108,7 @@ func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink,
p.sendIceCandidate(c, livekit.SignalTarget_SUBSCRIBER)
})
p.publisher.pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) {
//logger.Debugw("ICE connection state changed", "state", state.String())
if state == webrtc.ICEConnectionStateConnected {
p.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected {
go p.Close()
}
})
p.publisher.pc.OnICEConnectionStateChange(p.handlePublisherICEStateChange)
p.publisher.pc.OnTrack(p.onMediaTrack)
p.subscriber.pc.OnDataChannel(p.onDataChannel)
@@ -136,6 +135,10 @@ func (p *ParticipantImpl) IsReady() bool {
return state == livekit.ParticipantInfo_JOINED || state == livekit.ParticipantInfo_ACTIVE
}
func (p *ParticipantImpl) ConnectedAt() time.Time {
return p.connectedAt.Load().(time.Time)
}
// attach metadata to the participant
func (p *ParticipantImpl) SetMetadata(metadata string) {
p.metadata = metadata
@@ -159,6 +162,7 @@ func (p *ParticipantImpl) ToProto() *livekit.ParticipantInfo {
Identity: p.identity,
Metadata: p.metadata,
State: p.State(),
JoinedAt: p.ConnectedAt().Unix(),
}
p.lock.RLock()
@@ -717,6 +721,16 @@ func (p *ParticipantImpl) handleTrackPublished(track types.PublishedTrack) {
}
}
func (p *ParticipantImpl) handlePublisherICEStateChange(state webrtc.ICEConnectionState) {
//logger.Debugw("ICE connection state changed", "state", state.String())
if state == webrtc.ICEConnectionStateConnected {
p.connectedAt.Store(time.Now())
p.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected {
go p.Close()
}
}
// downTracksRTCPWorker sends SenderReports periodically when the participant is subscribed to
// other publishedTracks in the room.
func (p *ParticipantImpl) downTracksRTCPWorker() {
+29 -2
View File
@@ -2,8 +2,11 @@ package rtc
import (
"testing"
"time"
"github.com/pion/webrtc/v3"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/livekit/livekit-server/pkg/config"
"github.com/livekit/livekit-server/pkg/routing"
@@ -13,8 +16,6 @@ import (
livekit "github.com/livekit/livekit-server/proto"
)
//func Test
func TestIsReady(t *testing.T) {
tests := []struct {
state livekit.ParticipantInfo_State
@@ -47,6 +48,32 @@ func TestIsReady(t *testing.T) {
}
}
func TestICEStateChange(t *testing.T) {
t.Run("sets connectedAt when connected", func(t *testing.T) {
p := newParticipantForTest("test")
require.True(t, p.ConnectedAt().IsZero())
p.handlePublisherICEStateChange(webrtc.ICEConnectionStateConnected)
require.True(t, time.Now().Sub(p.ConnectedAt()) < time.Second)
})
t.Run("onClose gets called when ICE disconnected", func(t *testing.T) {
p := newParticipantForTest("test")
closeChan := make(chan bool, 1)
p.onClose = func(participant types.Participant) {
close(closeChan)
}
p.handlePublisherICEStateChange(webrtc.ICEConnectionStateDisconnected)
select {
case <-closeChan:
return
case <-time.After(time.Millisecond * 10):
t.Fatalf("onClose was not called after timeout")
}
})
}
func TestTrackPublishing(t *testing.T) {
t.Run("should send the correct events", func(t *testing.T) {
p := newParticipantForTest("test")
+1
View File
@@ -26,6 +26,7 @@ type Participant interface {
Identity() string
State() livekit.ParticipantInfo_State
IsReady() bool
ConnectedAt() time.Time
ToProto() *livekit.ParticipantInfo
RTCPChan() chan []rtcp.Packet
SetMetadata(metadata string)
@@ -3,6 +3,7 @@ package typesfakes
import (
"sync"
"time"
"github.com/livekit/livekit-server/pkg/routing"
"github.com/livekit/livekit-server/pkg/rtc/types"
@@ -78,6 +79,16 @@ type FakeParticipant struct {
closeReturnsOnCall map[int]struct {
result1 error
}
ConnectedAtStub func() time.Time
connectedAtMutex sync.RWMutex
connectedAtArgsForCall []struct {
}
connectedAtReturns struct {
result1 time.Time
}
connectedAtReturnsOnCall map[int]struct {
result1 time.Time
}
GetAudioLevelStub func() (uint8, bool)
getAudioLevelMutex sync.RWMutex
getAudioLevelArgsForCall []struct {
@@ -673,6 +684,59 @@ func (fake *FakeParticipant) CloseReturnsOnCall(i int, result1 error) {
}{result1}
}
func (fake *FakeParticipant) ConnectedAt() time.Time {
fake.connectedAtMutex.Lock()
ret, specificReturn := fake.connectedAtReturnsOnCall[len(fake.connectedAtArgsForCall)]
fake.connectedAtArgsForCall = append(fake.connectedAtArgsForCall, struct {
}{})
stub := fake.ConnectedAtStub
fakeReturns := fake.connectedAtReturns
fake.recordInvocation("ConnectedAt", []interface{}{})
fake.connectedAtMutex.Unlock()
if stub != nil {
return stub()
}
if specificReturn {
return ret.result1
}
return fakeReturns.result1
}
func (fake *FakeParticipant) ConnectedAtCallCount() int {
fake.connectedAtMutex.RLock()
defer fake.connectedAtMutex.RUnlock()
return len(fake.connectedAtArgsForCall)
}
func (fake *FakeParticipant) ConnectedAtCalls(stub func() time.Time) {
fake.connectedAtMutex.Lock()
defer fake.connectedAtMutex.Unlock()
fake.ConnectedAtStub = stub
}
func (fake *FakeParticipant) ConnectedAtReturns(result1 time.Time) {
fake.connectedAtMutex.Lock()
defer fake.connectedAtMutex.Unlock()
fake.ConnectedAtStub = nil
fake.connectedAtReturns = struct {
result1 time.Time
}{result1}
}
func (fake *FakeParticipant) ConnectedAtReturnsOnCall(i int, result1 time.Time) {
fake.connectedAtMutex.Lock()
defer fake.connectedAtMutex.Unlock()
fake.ConnectedAtStub = nil
if fake.connectedAtReturnsOnCall == nil {
fake.connectedAtReturnsOnCall = make(map[int]struct {
result1 time.Time
})
}
fake.connectedAtReturnsOnCall[i] = struct {
result1 time.Time
}{result1}
}
func (fake *FakeParticipant) GetAudioLevel() (uint8, bool) {
fake.getAudioLevelMutex.Lock()
ret, specificReturn := fake.getAudioLevelReturnsOnCall[len(fake.getAudioLevelArgsForCall)]
@@ -2037,6 +2101,8 @@ func (fake *FakeParticipant) Invocations() map[string][][]interface{} {
defer fake.canSubscribeMutex.RUnlock()
fake.closeMutex.RLock()
defer fake.closeMutex.RUnlock()
fake.connectedAtMutex.RLock()
defer fake.connectedAtMutex.RUnlock()
fake.getAudioLevelMutex.RLock()
defer fake.getAudioLevelMutex.RUnlock()
fake.getPublishedTracksMutex.RLock()