From b90f851be53697f874f02a8444f5d07c7608a469 Mon Sep 17 00:00:00 2001 From: David Zhao Date: Sun, 28 Mar 2021 21:05:20 -0700 Subject: [PATCH] return participant connected timestamps --- pkg/rtc/participant.go | 38 +++++++---- pkg/rtc/participant_internal_test.go | 31 ++++++++- pkg/rtc/types/interfaces.go | 1 + pkg/rtc/types/typesfakes/fake_participant.go | 66 ++++++++++++++++++++ proto/livekit_models.pb.go | 59 ++++++++++------- version/version.go | 2 +- 6 files changed, 158 insertions(+), 39 deletions(-) diff --git a/pkg/rtc/participant.go b/pkg/rtc/participant.go index db5e8e0c..b8ce5aca 100644 --- a/pkg/rtc/participant.go +++ b/pkg/rtc/participant.go @@ -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() { diff --git a/pkg/rtc/participant_internal_test.go b/pkg/rtc/participant_internal_test.go index 9efe108f..8d4a62b0 100644 --- a/pkg/rtc/participant_internal_test.go +++ b/pkg/rtc/participant_internal_test.go @@ -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") diff --git a/pkg/rtc/types/interfaces.go b/pkg/rtc/types/interfaces.go index 73e27f61..5b900329 100644 --- a/pkg/rtc/types/interfaces.go +++ b/pkg/rtc/types/interfaces.go @@ -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) diff --git a/pkg/rtc/types/typesfakes/fake_participant.go b/pkg/rtc/types/typesfakes/fake_participant.go index dcd4b392..11b03a6e 100644 --- a/pkg/rtc/types/typesfakes/fake_participant.go +++ b/pkg/rtc/types/typesfakes/fake_participant.go @@ -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() diff --git a/proto/livekit_models.pb.go b/proto/livekit_models.pb.go index 8340b15d..f079fe5c 100644 --- a/proto/livekit_models.pb.go +++ b/proto/livekit_models.pb.go @@ -222,6 +222,8 @@ type ParticipantInfo struct { State ParticipantInfo_State `protobuf:"varint,3,opt,name=state,proto3,enum=livekit.ParticipantInfo_State" json:"state,omitempty"` Tracks []*TrackInfo `protobuf:"bytes,4,rep,name=tracks,proto3" json:"tracks,omitempty"` Metadata string `protobuf:"bytes,5,opt,name=metadata,proto3" json:"metadata,omitempty"` + // timestamp when participant joined room + JoinedAt int64 `protobuf:"varint,6,opt,name=joined_at,json=joinedAt,proto3" json:"joined_at,omitempty"` } func (x *ParticipantInfo) Reset() { @@ -291,6 +293,13 @@ func (x *ParticipantInfo) GetMetadata() string { return "" } +func (x *ParticipantInfo) GetJoinedAt() int64 { + if x != nil { + return x.JoinedAt + } + return 0 +} + type TrackInfo struct { state protoimpl.MessageState sizeCache protoimpl.SizeCache @@ -459,7 +468,7 @@ var file_livekit_models_proto_rawDesc = []byte{ 0x05, 0x20, 0x01, 0x28, 0x03, 0x52, 0x0c, 0x63, 0x72, 0x65, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x54, 0x69, 0x6d, 0x65, 0x12, 0x23, 0x0a, 0x0d, 0x74, 0x75, 0x72, 0x6e, 0x5f, 0x70, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x18, 0x06, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c, 0x74, 0x75, 0x72, 0x6e, - 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0xfd, 0x01, 0x0a, 0x0f, 0x50, 0x61, 0x72, + 0x50, 0x61, 0x73, 0x73, 0x77, 0x6f, 0x72, 0x64, 0x22, 0x9a, 0x02, 0x0a, 0x0f, 0x50, 0x61, 0x72, 0x74, 0x69, 0x63, 0x69, 0x70, 0x61, 0x6e, 0x74, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x73, 0x69, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x69, 0x64, 0x65, 0x6e, 0x74, 0x69, 0x74, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, @@ -471,29 +480,31 @@ var file_livekit_models_proto_rawDesc = []byte{ 0x32, 0x12, 0x2e, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2e, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x49, 0x6e, 0x66, 0x6f, 0x52, 0x06, 0x74, 0x72, 0x61, 0x63, 0x6b, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x18, 0x05, 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, - 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x22, 0x3e, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, - 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x4a, 0x4f, 0x49, 0x4e, 0x49, 0x4e, 0x47, 0x10, 0x00, 0x12, 0x0a, - 0x0a, 0x06, 0x4a, 0x4f, 0x49, 0x4e, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x43, - 0x54, 0x49, 0x56, 0x45, 0x10, 0x02, 0x12, 0x10, 0x0a, 0x0c, 0x44, 0x49, 0x53, 0x43, 0x4f, 0x4e, - 0x4e, 0x45, 0x43, 0x54, 0x45, 0x44, 0x10, 0x03, 0x22, 0x6f, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x63, - 0x6b, 0x49, 0x6e, 0x66, 0x6f, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, - 0x28, 0x09, 0x52, 0x03, 0x73, 0x69, 0x64, 0x12, 0x26, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0e, 0x32, 0x12, 0x2e, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2e, - 0x54, 0x72, 0x61, 0x63, 0x6b, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, - 0x12, 0x0a, 0x04, 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, - 0x61, 0x6d, 0x65, 0x12, 0x14, 0x0a, 0x05, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, - 0x28, 0x08, 0x52, 0x05, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x22, 0x46, 0x0a, 0x0b, 0x44, 0x61, 0x74, - 0x61, 0x4d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, - 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x18, - 0x0a, 0x06, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, - 0x52, 0x06, 0x62, 0x69, 0x6e, 0x61, 0x72, 0x79, 0x42, 0x07, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, - 0x65, 0x2a, 0x2b, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, - 0x0a, 0x05, 0x41, 0x55, 0x44, 0x49, 0x4f, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x56, 0x49, 0x44, - 0x45, 0x4f, 0x10, 0x01, 0x12, 0x08, 0x0a, 0x04, 0x44, 0x41, 0x54, 0x41, 0x10, 0x02, 0x42, 0x31, - 0x5a, 0x2f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x76, - 0x65, 0x6b, 0x69, 0x74, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2d, 0x73, 0x65, 0x72, - 0x76, 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, - 0x74, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x6d, 0x65, 0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0x12, 0x1b, 0x0a, 0x09, 0x6a, 0x6f, 0x69, 0x6e, + 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x06, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x6a, 0x6f, 0x69, + 0x6e, 0x65, 0x64, 0x41, 0x74, 0x22, 0x3e, 0x0a, 0x05, 0x53, 0x74, 0x61, 0x74, 0x65, 0x12, 0x0b, + 0x0a, 0x07, 0x4a, 0x4f, 0x49, 0x4e, 0x49, 0x4e, 0x47, 0x10, 0x00, 0x12, 0x0a, 0x0a, 0x06, 0x4a, + 0x4f, 0x49, 0x4e, 0x45, 0x44, 0x10, 0x01, 0x12, 0x0a, 0x0a, 0x06, 0x41, 0x43, 0x54, 0x49, 0x56, + 0x45, 0x10, 0x02, 0x12, 0x10, 0x0a, 0x0c, 0x44, 0x49, 0x53, 0x43, 0x4f, 0x4e, 0x4e, 0x45, 0x43, + 0x54, 0x45, 0x44, 0x10, 0x03, 0x22, 0x6f, 0x0a, 0x09, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x49, 0x6e, + 0x66, 0x6f, 0x12, 0x10, 0x0a, 0x03, 0x73, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, + 0x03, 0x73, 0x69, 0x64, 0x12, 0x26, 0x0a, 0x04, 0x74, 0x79, 0x70, 0x65, 0x18, 0x02, 0x20, 0x01, + 0x28, 0x0e, 0x32, 0x12, 0x2e, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2e, 0x54, 0x72, 0x61, + 0x63, 0x6b, 0x54, 0x79, 0x70, 0x65, 0x52, 0x04, 0x74, 0x79, 0x70, 0x65, 0x12, 0x12, 0x0a, 0x04, + 0x6e, 0x61, 0x6d, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x6e, 0x61, 0x6d, 0x65, + 0x12, 0x14, 0x0a, 0x05, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x18, 0x04, 0x20, 0x01, 0x28, 0x08, 0x52, + 0x05, 0x6d, 0x75, 0x74, 0x65, 0x64, 0x22, 0x46, 0x0a, 0x0b, 0x44, 0x61, 0x74, 0x61, 0x4d, 0x65, + 0x73, 0x73, 0x61, 0x67, 0x65, 0x12, 0x14, 0x0a, 0x04, 0x74, 0x65, 0x78, 0x74, 0x18, 0x01, 0x20, + 0x01, 0x28, 0x09, 0x48, 0x00, 0x52, 0x04, 0x74, 0x65, 0x78, 0x74, 0x12, 0x18, 0x0a, 0x06, 0x62, + 0x69, 0x6e, 0x61, 0x72, 0x79, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0c, 0x48, 0x00, 0x52, 0x06, 0x62, + 0x69, 0x6e, 0x61, 0x72, 0x79, 0x42, 0x07, 0x0a, 0x05, 0x76, 0x61, 0x6c, 0x75, 0x65, 0x2a, 0x2b, + 0x0a, 0x09, 0x54, 0x72, 0x61, 0x63, 0x6b, 0x54, 0x79, 0x70, 0x65, 0x12, 0x09, 0x0a, 0x05, 0x41, + 0x55, 0x44, 0x49, 0x4f, 0x10, 0x00, 0x12, 0x09, 0x0a, 0x05, 0x56, 0x49, 0x44, 0x45, 0x4f, 0x10, + 0x01, 0x12, 0x08, 0x0a, 0x04, 0x44, 0x41, 0x54, 0x41, 0x10, 0x02, 0x42, 0x31, 0x5a, 0x2f, 0x67, + 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, + 0x74, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x2d, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, + 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x6c, 0x69, 0x76, 0x65, 0x6b, 0x69, 0x74, 0x62, 0x06, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( diff --git a/version/version.go b/version/version.go index 5449f147..a100d0ec 100644 --- a/version/version.go +++ b/version/version.go @@ -1,3 +1,3 @@ package version -const Version = "0.7.1" +const Version = "0.7.2"