From d9219a2a6722fdf1d0f74081878c9159316dc90a Mon Sep 17 00:00:00 2001 From: lukasIO Date: Sun, 21 Nov 2021 21:15:22 +0100 Subject: [PATCH] track numParticipants in room (#199) * track numParticipants in room * only track participant if not a hidden participant * adjust coding style to use ++ * fix typo * fix missing nil check * update roomstore with new numParticipants on participantChanged * only update roomstore if participant is not hidden * call StoreRoom directly after StoreParticipant when joining/leaving --- pkg/rtc/room.go | 7 +++++++ pkg/service/roommanager.go | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/pkg/rtc/room.go b/pkg/rtc/room.go index 6aa5b9443..e3ec822db 100644 --- a/pkg/rtc/room.go +++ b/pkg/rtc/room.go @@ -161,6 +161,9 @@ func (r *Room) Join(participant types.Participant, opts *ParticipantOptions, ice if r.FirstJoinedAt() == 0 { r.joinedAt.Store(time.Now().Unix()) } + if !participant.Hidden() { + r.Room.NumParticipants++ + } // it's important to set this before connection, we don't want to miss out on any publishedTracks participant.OnTrackPublished(r.onTrackPublished) @@ -256,7 +259,11 @@ func (r *Room) RemoveParticipant(identity string) { if ok { delete(r.participants, identity) delete(r.participantOpts, identity) + if !p.Hidden() { + r.Room.NumParticipants-- + } } + r.lock.Unlock() if !ok { return diff --git a/pkg/service/roommanager.go b/pkg/service/roommanager.go index cec631547..dd9559c6c 100644 --- a/pkg/service/roommanager.go +++ b/pkg/service/roommanager.go @@ -260,12 +260,26 @@ func (r *RoomManager) StartSession(ctx context.Context, roomName string, pi rout if err = r.roomStore.StoreParticipant(ctx, roomName, participant.ToProto()); err != nil { logger.Errorw("could not store participant", err) } + // update roomstore with new numParticipants + if !participant.Hidden() { + err = r.roomStore.StoreRoom(ctx, room.Room) + if err != nil { + logger.Errorw("could not store room", err) + } + } r.telemetry.ParticipantJoined(ctx, room.Room, participant.ToProto()) participant.OnClose(func(p types.Participant) { if err := r.roomStore.DeleteParticipant(ctx, roomName, p.Identity()); err != nil { logger.Errorw("could not delete participant", err) } + // update roomstore with new numParticipants + if !participant.Hidden() { + err = r.roomStore.StoreRoom(ctx, room.Room) + if err != nil { + logger.Errorw("could not store room", err) + } + } r.telemetry.ParticipantLeft(ctx, room.Room, p.ToProto()) })