This repository has been archived by the owner on Oct 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
767 lines (655 loc) · 26 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
import { isEqual } from 'lodash';
import { ColorsVariables } from '../../common/types/colors.types';
import {
DeviceEvent,
Dimensions,
FrameEvent,
MeetingConnectionStatus,
MeetingControlsEvent,
MeetingEvent,
MeetingState,
RealtimeEvent,
TranscriptState,
} from '../../common/types/events.types';
import { Participant, ParticipantType } from '../../common/types/participant.types';
import { StoreType } from '../../common/types/stores.types';
import { Logger } from '../../common/utils';
import { BrowserService } from '../../services/browser';
import config from '../../services/config';
import { ConnectionService } from '../../services/connection-status';
import { AblyParticipant, AblyRealtimeData } from '../../services/realtime/ably/types';
import VideoConfereceManager from '../../services/video-conference-manager';
import {
CamerasPosition,
DrawingData,
LayoutMode,
LayoutPosition,
RealtimeObserverPayload,
VideoFrameState,
VideoManagerOptions,
} from '../../services/video-conference-manager/types';
import { BaseComponent } from '../base';
import { ComponentNames } from '../types';
import { ParticipandToFrame, VideoComponentOptions } from './types';
const KICK_PARTICIPANTS_TIME = 1000 * 60;
let KICK_PARTICIPANTS_TIMEOUT: ReturnType<typeof setTimeout> | null = null;
export class VideoConference extends BaseComponent {
public name: ComponentNames;
protected logger: Logger;
private participantToFrameList: ParticipandToFrame[] = [];
private participantsOnMeeting: Partial<Participant>[] = [];
private localParticipant: Participant;
private videoManager: VideoConfereceManager;
private connectionService: ConnectionService;
private browserService: BrowserService;
private videoConfig: VideoManagerOptions;
private params?: VideoComponentOptions;
private kickParticipantsOnHostLeave = false;
constructor(params?: VideoComponentOptions) {
super();
this.params = {
...params,
userType: params?.participantType ?? params?.userType ?? ParticipantType.GUEST,
};
this.name = ComponentNames.VIDEO_CONFERENCE;
const { localParticipant, group } = this.useStore(StoreType.GLOBAL);
localParticipant.subscribe();
group.subscribe();
this.logger = new Logger(`@superviz/sdk/${ComponentNames.VIDEO_CONFERENCE}`);
this.browserService = new BrowserService();
this.connectionService = new ConnectionService();
this.connectionService.addListeners();
// Connection observers
this.connectionService.connectionStatusObserver.subscribe(this.onConnectionStatusChange);
}
/**
* @function toggleMeetingSetup
* @description open/close meeting setup
* @returns {void}
*/
public toggleMeetingSetup(): void {
return this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_MEETING_SETUP);
}
/**
* @function toggleMicrophone
* @description mute/unmute user's microphone
* @returns {void}
*/
public toggleMicrophone(): void {
return this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_MICROPHONE);
}
/**
* @function toggleCam
* @description enable/disable user's camera
* @returns {void}
*/
public toggleCam(): void {
this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_CAM);
}
/**
* @function toggleScreenShare
* @description enable/disable user's screen share
* @returns {void}
*/
public toggleScreenShare(): void {
return this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_SCREENSHARE);
}
/**
* @function toggleChat
* @description open/close meeting chat
* @returns {void}
*/
public toggleChat(): void {
return this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_MEETING_CHAT);
}
/**
* @function toggleTranscript
* @description open/close meeting transcript
* @returns {void}
*/
public toggleTranscript(): void {
return this.videoManager.publishMessageToFrame(MeetingControlsEvent.TOGGLE_TRANSCRIPT);
}
/**
* @function hangUp
* @description hang up user's call
* @returns {void}
* */
public hangUp(): void {
this.videoManager.publishMessageToFrame(MeetingControlsEvent.HANG_UP);
}
/**
* @function start
* @description start video conference
* @returns {void}
*/
protected start(): void {
this.logger.log('video conference @ start');
this.suscribeToRealtimeEvents();
this.startVideo();
}
/**
* @function destroy
* @description destroy video conference
* @returns {void}
*/
protected destroy(): void {
this.logger.log('video conference @ destroy');
const { data } = this.realtime.participant;
const activeComponents = data.activeComponents.filter((componentName: string) => {
return componentName !== ComponentNames.VIDEO_CONFERENCE;
});
this.realtime.updateMyProperties({
activeComponents,
type: ParticipantType.GUEST,
});
this.kickParticipantsOnHostLeave = false;
this.publish(MeetingEvent.DESTROY);
this.unsubscribeFromRealtimeEvents();
this.unsubscribeFromVideoEvents();
this.videoManager.leave();
this.connectionService.removeListeners();
}
/**
* @function startVideo
* @description start video manager
* @returns {void}
*/
private startVideo = (): void => {
this.videoConfig = {
language: this.params?.language,
canUseTranscription: this.params?.transcriptOff === false,
canShowAudienceList: this.params?.showAudienceList ?? true,
canUseChat: !this.params?.chatOff,
canUseCams: !this.params?.camsOff,
canUseScreenshare: !this.params?.screenshareOff,
canUseDefaultAvatars:
!!this.params?.defaultAvatars && !this.localParticipant?.avatar?.model3DUrl,
canUseGather: !!this.params?.enableGather,
canUseFollow: !!this.params?.enableFollow,
canUseGoTo: !!this.params?.enableGoTo,
canUseDefaultToolbar: this.params?.defaultToolbar ?? true,
camerasPosition: this.params?.collaborationMode?.position as CamerasPosition,
devices: this.params?.devices,
skipMeetingSettings: this.params?.skipMeetingSettings,
browserService: this.browserService,
offset: this.params?.offset,
locales: this.params?.locales ?? [],
avatars: this.params?.avatars ?? [],
customColors: config.get<ColorsVariables>('colors'),
waterMark: config.get<boolean>('waterMark'),
styles: this.params?.styles,
collaborationMode: this.params?.collaborationMode?.enabled ?? true,
layoutPosition:
this.params?.collaborationMode?.enabled === false
? LayoutPosition.CENTER
: (this.params?.collaborationMode?.modalPosition as LayoutPosition) ??
LayoutPosition.CENTER,
layoutMode: (this.params?.collaborationMode?.initialView as LayoutMode) ?? LayoutMode.LIST,
callbacks: this.params?.callbacks,
};
this.logger.log('video conference @ start video', this.videoConfig);
this.videoManager = new VideoConfereceManager(this.videoConfig);
this.subscribeToVideoEvents();
};
/**
* @function subscribeToVideoEvents
* @description subscribe to video events
* @returns {void}
*/
private subscribeToVideoEvents = (): void => {
this.logger.log('video conference @ subscribe to video events');
this.videoManager.meetingConnectionObserver.subscribe(
this.connectionService.updateMeetingConnectionStatus,
);
this.videoManager.participantListObserver.subscribe(this.onParticipantListUpdate);
this.videoManager.waitingForHostObserver.subscribe(this.onWaitingForHost);
this.videoManager.frameSizeObserver.subscribe(this.onFrameSizeDidChange);
this.videoManager.meetingStateObserver.subscribe(this.onMeetingStateChange);
this.videoManager.frameStateObserver.subscribe(this.onFrameStateChange);
this.videoManager.realtimeEventsObserver.subscribe(this.onRealtimeEventFromFrame);
this.videoManager.participantJoinedObserver.subscribe(this.onParticipantJoined);
this.videoManager.participantLeftObserver.subscribe(this.onParticipantLeft);
this.videoManager.sameAccountErrorObserver.subscribe(this.onSameAccountError);
this.videoManager.devicesObserver.subscribe(this.onDevicesChange);
};
/**
* @function unsubscribeFromVideoEvents
* @description unsubscribe from video events
* @returns {void}
* */
private unsubscribeFromVideoEvents = (): void => {
this.logger.log('video conference @ unsubscribe from video events');
this.videoManager.meetingConnectionObserver.unsubscribe(
this.connectionService.updateMeetingConnectionStatus,
);
this.videoManager.participantListObserver.unsubscribe(this.onParticipantListUpdate);
this.videoManager.waitingForHostObserver.unsubscribe(this.onWaitingForHost);
this.videoManager.frameSizeObserver.unsubscribe(this.onFrameSizeDidChange);
this.videoManager.meetingStateObserver.unsubscribe(this.onMeetingStateChange);
this.videoManager.frameStateObserver.unsubscribe(this.onFrameStateChange);
this.videoManager.realtimeEventsObserver.unsubscribe(this.onRealtimeEventFromFrame);
this.videoManager.participantJoinedObserver.unsubscribe(this.onParticipantJoined);
this.videoManager.participantLeftObserver.unsubscribe(this.onParticipantLeft);
this.videoManager.sameAccountErrorObserver.unsubscribe(this.onSameAccountError);
this.videoManager.devicesObserver.unsubscribe(this.onDevicesChange);
};
/**
* @function suscribeToRealtimeEvents
* @description subscribe to realtime events
* @returns {void}
*/
private suscribeToRealtimeEvents = (): void => {
this.logger.log('video conference @ subscribe to realtime events');
this.realtime.kickAllParticipantsObserver.subscribe(this.onKickAllParticipantsDidChange);
this.realtime.kickParticipantObserver.subscribe(this.onKickLocalParticipant);
this.realtime.participantJoinedObserver.subscribe(this.onParticipantJoinedOnRealtime);
this.realtime.participantLeaveObserver.subscribe(this.onParticipantLeftOnRealtime);
this.realtime.roomInfoUpdatedObserver.subscribe(this.onRoomInfoUpdated);
this.realtime.participantsObserver.subscribe(this.onRealtimeParticipantsDidChange);
};
/**
* @function unsubscribeFromRealtimeEvents
* @description subscribe to realtime events
* @returns {void}
*/
private unsubscribeFromRealtimeEvents = (): void => {
this.logger.log('video conference @ unsubscribe from realtime events');
this.realtime.kickAllParticipantsObserver.unsubscribe(this.onKickAllParticipantsDidChange);
this.realtime.participantJoinedObserver.unsubscribe(this.onParticipantJoinedOnRealtime);
this.realtime.participantLeaveObserver.unsubscribe(this.onParticipantLeftOnRealtime);
this.realtime.roomInfoUpdatedObserver.unsubscribe(this.onRoomInfoUpdated);
this.realtime.participantsObserver.unsubscribe(this.onRealtimeParticipantsDidChange);
};
/**
* @function createParticipantListFromAblyList
* @description update participant list from ably participant list
* @param {Record<string, AblyParticipant>} participants - ably participant list
* @returns {Participant[]} participant list
* */
private createParticipantFromAblyPresence = (participant: AblyParticipant): Participant => {
return {
id: participant.clientId,
color: this.realtime.getSlotColor(participant.data?.slotIndex).color,
avatar: participant.data.avatar,
type: participant.data.type,
name: participant.data.name,
isHost: this.realtime.hostClientId === participant.clientId,
};
};
/** Video Events */
/**
* @function onFrameSizeDidChange
* @description handler for frame size change event
* @param {Dimensions} dimensions - frame dimensions
* @returns {void}
* */
private onFrameSizeDidChange = (dimensions: Dimensions): void => {
this.publish(FrameEvent.FRAME_DIMENSIONS_UPDATE, dimensions);
};
/**
* @function onWaitingForHost
* @description handler for waiting for host event
* @param {boolean} waiting - whether or not waiting for host
* @returns {void}
*/
private onWaitingForHost = (waiting: boolean): void => {
this.publish(MeetingEvent.MEETING_WAITING_FOR_HOST, waiting);
};
/**
* @function onCOnnectionStatusChange
* @description handler for connection status change event
* @param {MeetingConnectionStatus} newStatus - new connection status
* @returns {void}
*/
private onConnectionStatusChange = (newStatus: MeetingConnectionStatus): void => {
this.logger.log('video conference @ on connection status change', newStatus);
const connectionProblemStatus = [
MeetingConnectionStatus.BAD,
MeetingConnectionStatus.DISCONNECTED,
MeetingConnectionStatus.POOR,
MeetingConnectionStatus.LOST_CONNECTION,
];
if (connectionProblemStatus.includes(newStatus)) {
this.realtime.freezeSync(true);
}
if (
connectionProblemStatus.includes(this.connectionService.oldConnectionStatus) &&
!connectionProblemStatus.includes(newStatus)
) {
this.realtime.freezeSync(false);
}
this.publish(MeetingEvent.MEETING_CONNECTION_STATUS_CHANGE, newStatus);
};
/**
* @function onMeetingStateChange
* @description handler for meeting state change event
* @param {MeetingState} state - meeting state
* @returns {void}
*/
private onMeetingStateChange = (state: MeetingState): void => {
this.logger.log('video conference @ on meeting state change', state);
this.publish(MeetingEvent.MEETING_STATE_UPDATE, state);
};
/**
* @function onSameAccountError
* @description handler for same account error event
* @param {string} error - error message
* @returns {void}
* */
private onSameAccountError = (error: string): void => {
this.publish(MeetingEvent.MEETING_SAME_PARTICIPANT_ERROR, error);
this.detach();
};
/**
* @function onDevicesChange
* @description handler for devices change event
* @param {DeviceEvent} state - device state
* @returns {void}
* */
private onDevicesChange = (state: DeviceEvent): void => {
this.publish(MeetingEvent.MEETING_DEVICES_CHANGE, state);
};
/**
* @function onFrameStateChange
* @description handler for frame state change event
* @param {VideoFrameState} state - frame state
* @returns
*/
private onFrameStateChange = (state: VideoFrameState): void => {
this.logger.log('video conference @ on frame state change', state);
if (state !== VideoFrameState.INITIALIZED) return;
if (this.params.userType !== ParticipantType.GUEST) {
this.localParticipant = Object.assign(this.localParticipant, {
type: this.params.userType,
});
this.realtime.updateMyProperties({
...this.localParticipant,
});
}
this.videoManager.start({
group: this.group,
participant: this.localParticipant,
roomId: config.get<string>('roomId'),
});
this.publish(MeetingEvent.MEETING_START);
};
/**
* @function onRealtimeEventFromFrame
* @description handler for realtime event
* @param {RealtimeObserverPayload} payload - realtime event payload
* @returns {void}
* */
private onRealtimeEventFromFrame = ({ event, data }: RealtimeObserverPayload): void => {
this.logger.log('video conference @ on realtime event from frame', event, data);
const _ = {
[RealtimeEvent.REALTIME_HOST_CHANGE]: (data: string) => this.realtime.setHost(data),
[RealtimeEvent.REALTIME_GATHER]: (data: boolean) => this.realtime.setGather(data),
[RealtimeEvent.REALTIME_GRID_MODE_CHANGE]: (data: boolean) => this.realtime.setGridMode(data),
[RealtimeEvent.REALTIME_DRAWING_CHANGE]: (data: DrawingData) => {
this.realtime.setDrawing(data);
},
[RealtimeEvent.REALTIME_FOLLOW_PARTICIPANT]: (data: string) => {
this.realtime.setFollowParticipant(data);
},
[MeetingEvent.MEETING_KICK_PARTICIPANT]: (data: string) => {
this.realtime.setKickParticipant(data);
},
[RealtimeEvent.REALTIME_TRANSCRIPT_CHANGE]: (data: TranscriptState) => {
this.realtime.setTranscript(data);
},
[RealtimeEvent.REALTIME_GO_TO_PARTICIPANT]: (data: string) => {
this.eventBus.publish(RealtimeEvent.REALTIME_GO_TO_PARTICIPANT, data);
},
}[event](data);
this.publish(event, data);
};
/**
* @function onParticipantJoined
* @description handler for participant joined event
* @param {Participant} participant - participant
* @returns {void}
*/
private onParticipantJoined = (participant: Participant): void => {
this.logger.log('video conference @ on participant joined', participant);
this.localParticipant = participant;
this.publish(MeetingEvent.MEETING_PARTICIPANT_JOINED, participant);
this.publish(MeetingEvent.MY_PARTICIPANT_JOINED, participant);
this.kickParticipantsOnHostLeave = !this.params?.allowGuests;
if (this.videoConfig.canUseDefaultAvatars) {
this.realtime.updateMyProperties({
avatar: participant.avatar,
name: participant.name,
type: participant.type,
});
return;
}
this.realtime.updateMyProperties({
name: participant.name,
type: participant.type,
});
};
/**
* @function onParticipantLeft
* @description handler for participant left event
* @param {Participant} _ - participant
* @returns {void}
*/
private onParticipantLeft = (_: Participant): void => {
this.logger.log('video conference @ on participant left', this.localParticipant);
this.publish(MeetingEvent.MY_PARTICIPANT_LEFT, this.localParticipant);
this.detach();
};
private onParticipantListUpdate = (participants: Partial<Participant>[]): void => {
this.logger.log('video conference @ on participant list update', participants);
const list = participants.map((participant) => {
const participantOnRealtime = this.realtime.getParticipants[participant.id];
return {
id: participant.id,
color:
participant.color ??
this.realtime.getSlotColor(participantOnRealtime.data.slotIndex).color,
avatar: participant.avatar,
name: participant.name,
type: participant.type,
isHost: participant.isHost,
};
});
if (isEqual(this.participantsOnMeeting, list)) return;
this.publish(MeetingEvent.MEETING_PARTICIPANT_LIST_UPDATE, list);
if (this.participantsOnMeeting.length !== list.length) {
this.publish(MeetingEvent.MEETING_PARTICIPANT_AMOUNT_UPDATE, list.length);
}
this.participantsOnMeeting = list;
};
/** Realtime Events */
/**
* @function onKickAllParticipantsDidChange
* @description handler for kick all participants event
* @param {boolean} kick - whether or not to kick all participants
* @returns {void}
*/
private onKickAllParticipantsDidChange = (kick: boolean): void => {
this.logger.log('video conference @ on kick all participants did change', kick);
this.publish(MeetingEvent.MEETING_KICK_PARTICIPANTS, kick);
this.detach();
};
/**
* @function onKickLocalParticipant
* @description handler for kick local participant event
* @param {string} participantId - participant id
* @returns {void}
*/
private onKickLocalParticipant = (): void => {
this.logger.log('video conference @ on kick local participant');
this.publish(MeetingEvent.MEETING_KICK_PARTICIPANT, this.localParticipant);
this.detach();
};
/**
* @function onRoomInfoUpdated
* @description handler for room info update event
* @param {AblyRealtimeData} room - room info
* @returns {void}
* */
private onRoomInfoUpdated = (room: AblyRealtimeData): void => {
this.logger.log('video conference @ on room info updated', room);
const { isGridModeEnable, followParticipantId, gather, drawing, transcript, hostClientId } =
room;
this.videoManager.publishMessageToFrame(RealtimeEvent.REALTIME_HOST_CHANGE, hostClientId);
this.onHostParticipantDidChange(hostClientId);
this.videoManager.publishMessageToFrame(
RealtimeEvent.REALTIME_GRID_MODE_CHANGE,
isGridModeEnable,
);
this.videoManager.publishMessageToFrame(RealtimeEvent.REALTIME_DRAWING_CHANGE, drawing);
this.videoManager.publishMessageToFrame(
RealtimeEvent.REALTIME_FOLLOW_PARTICIPANT,
followParticipantId,
);
this.videoManager.publishMessageToFrame(RealtimeEvent.REALTIME_TRANSCRIPT_CHANGE, transcript);
if (this.realtime.hostClientId === this.localParticipant.id && gather) {
this.realtime.setGather(false);
}
};
/**
* @function onRealtimeParticipantsDidChange
* @description handler for participant list update event
* @param {Record<string, AblyParticipant>} participants - participants
* @returns {void}
*/
private onRealtimeParticipantsDidChange = (
participants: Record<string, AblyParticipant>,
): void => {
this.logger.log('video conference @ on participants did change', participants);
const participantList: ParticipandToFrame[] = Object.values(participants).map(
(participant: AblyParticipant) => {
return {
timestamp: participant.timestamp,
connectionId: participant.connectionId,
participantId: participant.clientId,
color: this.realtime.getSlotColor(participant.data.slotIndex).name,
name: participant.data.name,
isHost: this.realtime.hostClientId === participant.clientId,
avatar: participant.data.avatar,
type: participant.data.type,
};
},
);
if (!isEqual(this.participantToFrameList, participantList)) {
this.videoManager.publishMessageToFrame(
RealtimeEvent.REALTIME_PARTICIPANT_LIST_UPDATE,
participantList,
);
}
this.participantToFrameList = participantList;
this.validateIfInTheRoomHasHost(participants);
};
/**
* @function onHostParticipantDidChange
* @description handler for host participant change event
* @param {hostId} string - new host ud
* @returns {void}
* */
private onHostParticipantDidChange = (hostId: string): void => {
this.logger.log('video conference @ on host participant did change', hostId);
this.videoManager.publishMessageToFrame(RealtimeEvent.REALTIME_HOST_CHANGE, hostId);
const newHost = this.participantsOnMeeting.find((participant) => {
return participant.id === hostId;
});
if (this.realtime.isLocalParticipantHost) {
this.realtime.setSyncProperty(MeetingEvent.MEETING_HOST_CHANGE, newHost);
this.publish(MeetingEvent.MEETING_HOST_CHANGE, newHost);
}
};
/**
* @function onHostAvailabilityChange
* @description Callback function that is called when the availability of the host changes.
* @param {boolean} isHostAvailable - A boolean indicating whether the host is available or not.
* @returns {void}
*/
private onHostAvailabilityChange = (isHostAvailable: boolean): void => {
this.logger.log('launcher service @ onHostAvailabilityChange');
if (isHostAvailable) {
this.publish(MeetingEvent.MEETING_HOST_AVAILABLE);
return;
}
this.publish(MeetingEvent.MEETING_NO_HOST_AVAILABLE);
};
/**
* @function onParticipantJoinedOnRealtime
* @description handler for participant joined event
* @param {AblyParticipant} participant - participant
* @returns {void}
*/
private onParticipantJoinedOnRealtime = (participant: AblyParticipant): void => {
this.logger.log('video conference @ on participant joined on realtime', participant);
this.publish(
MeetingEvent.MEETING_PARTICIPANT_JOINED,
this.createParticipantFromAblyPresence(participant),
);
};
/**
* @function onParticipantLeftOnRealtime
* @description handler for participant left event
* @param {AblyParticipant} participant
* @returns {void}
*/
private onParticipantLeftOnRealtime = (participant: AblyParticipant): void => {
this.logger.log('video conference @ on participant left on realtime', participant);
this.publish(
MeetingEvent.MEETING_PARTICIPANT_LEFT,
this.createParticipantFromAblyPresence(participant),
);
};
private validateIfInTheRoomHasHost = (participants: Record<string, AblyParticipant>): void => {
const participantsCanBeHost = Object.values(participants).filter(
(participant) => participant.data.type === ParticipantType.HOST,
);
const hostAlreadyInRoom = participantsCanBeHost.some(
(participant) => participant.clientId === this.realtime.hostClientId,
);
if (
!participantsCanBeHost.length &&
this.kickParticipantsOnHostLeave &&
this.localParticipant.type !== ParticipantType.HOST &&
!KICK_PARTICIPANTS_TIMEOUT
) {
this.logger.log(
'video conference @ validate if in the room has host - init kick all participants timeout',
);
KICK_PARTICIPANTS_TIMEOUT = setTimeout(() => {
this.logger.log(
'video conference @ validate if in the room has host - kick all participants',
);
this.onKickLocalParticipant();
}, KICK_PARTICIPANTS_TIME);
}
if (participantsCanBeHost.length && KICK_PARTICIPANTS_TIMEOUT) {
this.logger.log(
'video conference @ validade if in the room has host - clear kick all participants timeout',
);
clearTimeout(KICK_PARTICIPANTS_TIMEOUT);
KICK_PARTICIPANTS_TIMEOUT = null;
}
this.onHostAvailabilityChange(!!participantsCanBeHost.length);
if (!participantsCanBeHost.length || hostAlreadyInRoom) return;
const host = participantsCanBeHost.reduce((previus, current) => {
this.logger.log(
'video conference @ validade if in the room has host - reducing participants',
{
previus,
current,
},
);
if (!previus) return current;
if (current.clientId === this.realtime.hostClientId) return current;
// set the first participant with host privileges as host
if (current.timestamp > previus.timestamp) return current;
return previus;
}, null);
if (host.clientId === this.realtime.hostClientId || host.clientId !== this.localParticipant.id)
return;
this.logger.log('video conference @ validade if in the room has host - set host', host);
this.realtime.setHost(host.clientId);
};
}