From d6c155e060e722fbba7ed062fdd4168ef2a4870b Mon Sep 17 00:00:00 2001 From: David Zhao Date: Wed, 21 Apr 2021 21:52:31 -0700 Subject: [PATCH] set joinedAt when participant is created --- pkg/rtc/participant.go | 10 ++++------ pkg/rtc/participant_internal_test.go | 15 +++++++-------- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index 03137b564..6516f8d5a 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -41,7 +41,7 @@ type ParticipantImpl struct { protocolVersion types.ProtocolVersion // when first connected - connectedAt atomic.Value // time.Time + connectedAt time.Time // JSON encoded metadata to pass to clients metadata string @@ -82,12 +82,11 @@ func NewParticipant(identity string, conf *WebRTCConfig, rs routing.MessageSink, lock: sync.RWMutex{}, publishedTracks: make(map[string]types.PublishedTrack, 0), pendingTracks: make(map[string]*livekit.TrackInfo), + connectedAt: time.Now(), } - // store empty value - p.connectedAt.Store(time.Time{}) p.state.Store(livekit.ParticipantInfo_JOINING) - var err error + var err error p.publisher, err = NewPCTransport(livekit.SignalTarget_PUBLISHER, conf) if err != nil { return nil, err @@ -141,7 +140,7 @@ func (p *ParticipantImpl) IsReady() bool { } func (p *ParticipantImpl) ConnectedAt() time.Time { - return p.connectedAt.Load().(time.Time) + return p.connectedAt } // 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(), // "participant", p.identity) if state == webrtc.ICEConnectionStateConnected { - p.connectedAt.Store(time.Now()) p.updateState(livekit.ParticipantInfo_ACTIVE) } else if state == webrtc.ICEConnectionStateDisconnected || state == webrtc.ICEConnectionStateFailed { go p.Close() diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 592fcd17e..3fa9da5d1 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -49,14 +49,6 @@ 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) @@ -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 { conf, _ := config.NewConfig("") rtcConf, _ := NewWebRTCConfig(&conf.RTC, "")