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
543 lines (468 loc) · 19 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
import { isEqual } from 'lodash';
import {
DeviceEvent,
Dimensions,
FrameEvent,
MeetingConnectionStatus,
MeetingEvent,
MeetingState,
RealtimeEvent,
TranscriptState,
} from '../../common/types/events.types';
import { Participant } from '../../common/types/participant.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 { HostObserverCallbackResponse } from '../../services/realtime/base/types';
import VideoConfereceManager from '../../services/video-conference-manager';
import {
DrawingData,
RealtimeObserverPayload,
VideoFrameState,
VideoManagerOptions,
} from '../../services/video-conference-manager/types';
import { BaseComponent } from '../base';
import { ParticipandToFrame, VideoComponentOptions } from './types';
export class VideoComponent extends BaseComponent {
public name: string;
protected logger: Logger;
private participantToFrameList: ParticipandToFrame[] = [];
private participantsOnMeeting: Partial<Participant>[] = [];
private videoManager: VideoConfereceManager;
private connectionService: ConnectionService;
private browserService: BrowserService;
private videoConfig: VideoManagerOptions;
private params?: VideoComponentOptions;
constructor(params?: VideoComponentOptions) {
super();
this.params = params;
this.name = 'video-component';
this.logger = new Logger('@superviz/sdk/video-component');
this.browserService = new BrowserService();
this.connectionService = new ConnectionService();
this.connectionService.addListeners();
// Connection observers
this.connectionService.connectionStatusObserver.subscribe(this.onConnectionStatusChange);
}
/**
* @function start
* @description start video component
* @returns {void}
*/
protected start(): void {
this.logger.log('video component @ start');
this.publish(MeetingEvent.MEETING_START);
this.suscribeToRealtimeEvents();
this.startVideo();
}
/**
* @function destroy
* @description destroy video component
* @returns {void}
*/
protected destroy(): void {
this.logger.log('video component @ destroy');
this.publish(MeetingEvent.DESTROY);
this.unsubscribeFromRealtimeEvents();
this.unsubscribeFromVideoEvents();
this.videoManager.leave();
this.connectionService.removeListeners();
}
/**
* @function startVideo
* @description start video manager
* @param {VideoManagerOptions} options - video manager params
* @returns {void}
*/
private startVideo = (): void => {
this.videoConfig = {
language: this.params?.language,
canUseChat: !this.params?.chatOff,
canUseCams: !this.params?.camsOff,
canUseScreenshare: !this.params?.screenshareOff,
canUseDefaultAvatars: !!this.params?.defaultAvatars && !this.localParticipant?.avatar?.model,
canUseGather: !!this.params?.enableGather,
canUseFollow: !!this.params?.enableFollow,
canUseGoTo: !!this.params?.enableGoTo,
canUseDefaultToolbar: this.params?.defaultToolbar ?? true,
camerasPosition: this.params?.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: this.params?.customColors,
layoutPosition: this.params?.layoutPosition,
};
this.logger.log('video component @ 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 component @ 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 component @ 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 component @ 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);
this.realtime.hostObserver.subscribe(this.onHostParticipantDidChange);
};
/**
* @function unsubscribeFromRealtimeEvents
* @description subscribe to realtime events
* @returns {void}
*/
private unsubscribeFromRealtimeEvents = (): void => {
this.logger.log('video component @ 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);
this.realtime.hostObserver.unsubscribe(this.onHostParticipantDidChange);
};
/**
* @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 component @ 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 component @ 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 component @ on frame state change', state);
if (state !== VideoFrameState.INITIALIZED) return;
this.videoManager.start({
group: this.group,
participant: this.localParticipant,
roomId: config.get<string>('roomId'),
});
};
/**
* @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 component @ 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 component @ on participant joined', participant);
this.localParticipant = participant;
this.publish(MeetingEvent.MEETING_PARTICIPANT_JOINED, participant);
this.publish(MeetingEvent.MY_PARTICIPANT_JOINED, participant);
if (this.videoConfig.canUseDefaultAvatars) {
this.realtime.updateMyProperties({
avatar: participant.avatar,
name: participant.name,
});
return;
}
this.realtime.updateMyProperties({
name: participant.name,
});
};
/**
* @function onParticipantLeft
* @description handler for participant left event
* @param {Participant} _ - participant
* @returns {void}
*/
private onParticipantLeft = (_: Participant): void => {
this.logger.log('video component @ 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 component @ on participant list update', participants);
if (isEqual(this.participantsOnMeeting, participants)) return;
this.publish(MeetingEvent.MEETING_PARTICIPANT_LIST_UPDATE, participants);
if (this.participantsOnMeeting.length !== participants.length) {
this.publish(MeetingEvent.MEETING_PARTICIPANT_AMOUNT_UPDATE, participants.length);
}
this.participantsOnMeeting = participants;
};
/** 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 component @ 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 component @ 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 component @ on room info updated', room);
const { isGridModeEnable, followParticipantId, gather, drawing, transcript } = room;
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 component @ 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;
};
/**
* @function onHostParticipantDidChange
* @description handler for host participant change event
* @param {HostObserverCallbackResponse} data - host change data
* @returns {void}
* */
private onHostParticipantDidChange = (data: HostObserverCallbackResponse): void => {
this.logger.log('video component @ on host participant did change', data);
this.videoManager.publishMessageToFrame(
RealtimeEvent.REALTIME_HOST_CHANGE,
data?.newHostParticipantId,
);
};
/**
* @function onParticipantJoinedOnRealtime
* @description handler for participant joined event
* @param {AblyParticipant} participant - participant
* @returns {void}
*/
private onParticipantJoinedOnRealtime = (participant: AblyParticipant): void => {
this.logger.log('video component @ 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 component @ on participant left on realtime', participant);
this.publish(
MeetingEvent.MEETING_PARTICIPANT_LEFT,
this.createParticipantFromAblyPresence(participant),
);
};
}