set joinedAt when participant is created

This commit is contained in:
David Zhao
2021-04-21 21:52:31 -07:00
parent 07e4ab0c2a
commit d6c155e060
2 changed files with 11 additions and 14 deletions
+4 -6
View File
@@ -41,7 +41,7 @@ type ParticipantImpl struct {
protocolVersion types.ProtocolVersion protocolVersion types.ProtocolVersion
// when first connected // when first connected
connectedAt atomic.Value // time.Time connectedAt time.Time
// JSON encoded metadata to pass to clients // JSON encoded metadata to pass to clients
metadata string metadata string
@@ -82,12 +82,11 @@ func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink,
lock: sync.RWMutex{}, lock: sync.RWMutex{},
publishedTracks: make(map[string]types.PublishedTrack, 0), publishedTracks: make(map[string]types.PublishedTrack, 0),
pendingTracks: make(map[string]*livekit.TrackInfo), pendingTracks: make(map[string]*livekit.TrackInfo),
connectedAt: time.Now(),
} }
// store empty value
p.connectedAt.Store(time.Time{})
p.state.Store(livekit.ParticipantInfo_JOINING) p.state.Store(livekit.ParticipantInfo_JOINING)
var err error
var err error
p.publisher, err = NewPCTransport(livekit.SignalTarget_PUBLISHER, conf) p.publisher, err = NewPCTransport(livekit.SignalTarget_PUBLISHER, conf)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -141,7 +140,7 @@ func (p *ParticipantImpl) IsReady() bool {
} }
func (p *ParticipantImpl) ConnectedAt() time.Time { func (p *ParticipantImpl) ConnectedAt() time.Time {
return p.connectedAt.Load().(time.Time) return p.connectedAt
} }
// SetMetadata attaches metadata to the participant // SetMetadata attaches metadata to the participant
@@ -729,7 +728,6 @@ func (p *ParticipantImpl) handlePublisherICEStateChange(state webrtc.ICEConnecti
//logger.Debugw("ICE connection state changed", "state", state.String(), //logger.Debugw("ICE connection state changed", "state", state.String(),
// "participant", p.identity) // "participant", p.identity)
if state == webrtc.ICEConnectionStateConnected { if state == webrtc.ICEConnectionStateConnected {
p.connectedAt.Store(time.Now())
p.updateState(livekit.ParticipantInfo_ACTIVE) p.updateState(livekit.ParticipantInfo_ACTIVE)
} else if state == webrtc.ICEConnectionStateDisconnected || state == webrtc.ICEConnectionStateFailed { } else if state == webrtc.ICEConnectionStateDisconnected || state == webrtc.ICEConnectionStateFailed {
go p.Close() go p.Close()
+7 -8
View File
@@ -49,14 +49,6 @@ func TestIsReady(t *testing.T) {
} }
func TestICEStateChange(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) { t.Run("onClose gets called when ICE disconnected", func(t *testing.T) {
p := newParticipantForTest("test") p := newParticipantForTest("test")
closeChan := make(chan bool, 1) closeChan := make(chan bool, 1)
@@ -145,6 +137,13 @@ func TestDisconnectTiming(t *testing.T) {
}) })
} }
func TestCorrectJoinedAt(t *testing.T) {
p := newParticipantForTest("test")
info := p.ToProto()
require.NotZero(t, info.JoinedAt)
require.True(t, time.Now().Unix()-info.JoinedAt <= 1)
}
func newParticipantForTest(identity string) *ParticipantImpl { func newParticipantForTest(identity string) *ParticipantImpl {
conf, _ := config.NewConfig("") conf, _ := config.NewConfig("")
rtcConf, _ := NewWebRTCConfig(&conf.RTC, "") rtcConf, _ := NewWebRTCConfig(&conf.RTC, "")