mediasfu-shared 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (141) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +228 -0
  3. package/dist/index.cjs +7707 -0
  4. package/dist/index.cjs.map +1 -0
  5. package/dist/index.d.ts +7285 -0
  6. package/dist/index.js +7690 -0
  7. package/dist/index.js.map +1 -0
  8. package/package.json +78 -0
  9. package/src/ProducerClient/producerClientEmits/createDeviceClient.ts +68 -0
  10. package/src/consumers/addVideosGrid.ts +18 -0
  11. package/src/consumers/autoAdjust.ts +100 -0
  12. package/src/consumers/calculateRowsAndColumns.ts +51 -0
  13. package/src/consumers/changeVids.ts +753 -0
  14. package/src/consumers/checkGrid.ts +100 -0
  15. package/src/consumers/checkPermission.ts +89 -0
  16. package/src/consumers/checkScreenShare.ts +114 -0
  17. package/src/consumers/closeAndResize.ts +401 -0
  18. package/src/consumers/compareActiveNames.ts +122 -0
  19. package/src/consumers/compareScreenStates.ts +117 -0
  20. package/src/consumers/connectIps.ts +175 -0
  21. package/src/consumers/connectLocalIps.ts +103 -0
  22. package/src/consumers/connectRecvTransport.ts +158 -0
  23. package/src/consumers/connectSendTransport.ts +150 -0
  24. package/src/consumers/connectSendTransportAudio.ts +161 -0
  25. package/src/consumers/connectSendTransportScreen.ts +169 -0
  26. package/src/consumers/connectSendTransportVideo.ts +149 -0
  27. package/src/consumers/consumerResume.ts +25 -0
  28. package/src/consumers/controlMedia.ts +118 -0
  29. package/src/consumers/createSendTransport.ts +312 -0
  30. package/src/consumers/disconnectSendTransportAudio.ts +170 -0
  31. package/src/consumers/disconnectSendTransportScreen.ts +130 -0
  32. package/src/consumers/disconnectSendTransportVideo.ts +161 -0
  33. package/src/consumers/dispStreams.ts +694 -0
  34. package/src/consumers/generatePageContent.ts +118 -0
  35. package/src/consumers/getEstimate.ts +124 -0
  36. package/src/consumers/getPipedProducersAlt.ts +96 -0
  37. package/src/consumers/getProducersPiped.ts +89 -0
  38. package/src/consumers/getVideos.ts +107 -0
  39. package/src/consumers/mixStreams.ts +97 -0
  40. package/src/consumers/onScreenChanges.ts +106 -0
  41. package/src/consumers/prepopulateUserMedia.ts +18 -0
  42. package/src/consumers/processConsumerTransports.ts +157 -0
  43. package/src/consumers/processConsumerTransportsAudio.ts +121 -0
  44. package/src/consumers/rePort.ts +123 -0
  45. package/src/consumers/reUpdateInter.ts +289 -0
  46. package/src/consumers/readjust.ts +170 -0
  47. package/src/consumers/receiveAllPipedTransports.ts +77 -0
  48. package/src/consumers/receiveRoomMessages.ts +55 -0
  49. package/src/consumers/reorderStreams.ts +246 -0
  50. package/src/consumers/requestScreenShare.ts +103 -0
  51. package/src/consumers/resumePauseAudioStreams.ts +174 -0
  52. package/src/consumers/resumePauseStreams.ts +110 -0
  53. package/src/consumers/resumeSendTransportAudio.ts +143 -0
  54. package/src/consumers/signalNewConsumerTransport.ts +179 -0
  55. package/src/consumers/socketReceiveMethods/joinConsumeRoom.ts +130 -0
  56. package/src/consumers/socketReceiveMethods/newPipeProducer.ts +138 -0
  57. package/src/consumers/socketReceiveMethods/producerClosed.ts +102 -0
  58. package/src/consumers/startShareScreen.ts +124 -0
  59. package/src/consumers/stopShareScreen.ts +241 -0
  60. package/src/consumers/streamSuccessAudio.ts +297 -0
  61. package/src/consumers/streamSuccessAudioSwitch.ts +315 -0
  62. package/src/consumers/streamSuccessScreen.ts +255 -0
  63. package/src/consumers/streamSuccessVideo.ts +373 -0
  64. package/src/consumers/switchUserAudio.ts +140 -0
  65. package/src/consumers/switchUserVideo.ts +201 -0
  66. package/src/consumers/switchUserVideoAlt.ts +331 -0
  67. package/src/consumers/trigger.ts +250 -0
  68. package/src/consumers/updateMiniCardsGrid.ts +150 -0
  69. package/src/consumers/updateParticipantAudioDecibels.ts +56 -0
  70. package/src/index.ts +119 -0
  71. package/src/methods/background/launchBackground.ts +16 -0
  72. package/src/methods/breakoutRooms/breakoutRoomUpdated.ts +161 -0
  73. package/src/methods/breakoutRooms/handleStartBreakout.ts +96 -0
  74. package/src/methods/breakoutRooms/handleStopBreakout.ts +72 -0
  75. package/src/methods/breakoutRooms/index.ts +4 -0
  76. package/src/methods/breakoutRooms/launchBreakoutRooms.ts +31 -0
  77. package/src/methods/coHost/launchCoHost.ts +28 -0
  78. package/src/methods/coHostMethods/index.ts +2 -0
  79. package/src/methods/coHostMethods/modifyCoHostSettings.ts +94 -0
  80. package/src/methods/displaySettings/index.ts +1 -0
  81. package/src/methods/displaySettings/launchDisplaySettings.ts +31 -0
  82. package/src/methods/displaySettings/modifyDisplaySettings.ts +242 -0
  83. package/src/methods/exit/confirmExit.ts +60 -0
  84. package/src/methods/exit/index.ts +2 -0
  85. package/src/methods/exit/launchConfirmExit.ts +29 -0
  86. package/src/methods/index.ts +5 -0
  87. package/src/methods/mediaSettings/launchMediaSettings.ts +61 -0
  88. package/src/methods/menu/launchMenuModal.ts +28 -0
  89. package/src/methods/message/index.ts +1 -0
  90. package/src/methods/message/launchMessages.ts +27 -0
  91. package/src/methods/message/sendMessage.ts +175 -0
  92. package/src/methods/participants/index.ts +3 -0
  93. package/src/methods/participants/launchParticipants.ts +25 -0
  94. package/src/methods/participants/messageParticipants.ts +78 -0
  95. package/src/methods/participants/muteParticipants.ts +79 -0
  96. package/src/methods/participants/removeParticipants.ts +97 -0
  97. package/src/methods/polls/handleCreatePoll.ts +66 -0
  98. package/src/methods/polls/handleEndPoll.ts +64 -0
  99. package/src/methods/polls/handleVotePoll.ts +76 -0
  100. package/src/methods/polls/index.ts +3 -0
  101. package/src/methods/polls/launchPoll.ts +25 -0
  102. package/src/methods/prejoin/handleCreateRoom.ts +441 -0
  103. package/src/methods/prejoin/handleJoinRoom.ts +153 -0
  104. package/src/methods/prejoin/index.ts +14 -0
  105. package/src/methods/recording/checkPauseState.ts +57 -0
  106. package/src/methods/recording/checkResumeState.ts +42 -0
  107. package/src/methods/recording/confirmRecording.ts +241 -0
  108. package/src/methods/recording/launchRecording.ts +114 -0
  109. package/src/methods/recording/recordPauseTimer.ts +52 -0
  110. package/src/methods/recording/recordResumeTimer.ts +92 -0
  111. package/src/methods/recording/recordStartTimer.ts +106 -0
  112. package/src/methods/recording/recordUpdateTimer.ts +49 -0
  113. package/src/methods/recording/startRecording.ts +268 -0
  114. package/src/methods/recording/stopRecording.ts +124 -0
  115. package/src/methods/recording/updateRecording.ts +245 -0
  116. package/src/methods/requests/index.ts +1 -0
  117. package/src/methods/requests/launchRequests.ts +25 -0
  118. package/src/methods/requests/respondToRequests.ts +108 -0
  119. package/src/methods/settings/index.ts +2 -0
  120. package/src/methods/settings/launchSettings.ts +25 -0
  121. package/src/methods/settings/modifySettings.ts +99 -0
  122. package/src/methods/stream/clickVideo.ts +337 -0
  123. package/src/methods/stream/index.ts +3 -0
  124. package/src/methods/stream/switchAudio.ts +73 -0
  125. package/src/methods/stream/switchVideo.ts +148 -0
  126. package/src/methods/stream/switchVideoAlt.ts +152 -0
  127. package/src/methods/utils/checkLimitsAndMakeRequest.ts +103 -0
  128. package/src/methods/utils/validateAlphanumeric.ts +41 -0
  129. package/src/methods/waiting/index.ts +1 -0
  130. package/src/methods/waiting/launchWaiting.ts +26 -0
  131. package/src/methods/waiting/respondToWaiting.ts +82 -0
  132. package/src/methods/welcome/handleWelcomeRequest.ts +189 -0
  133. package/src/methods/welcome/index.ts +5 -0
  134. package/src/methods/whiteboard/handleStartWhiteboard.ts +65 -0
  135. package/src/methods/whiteboard/handleStopWhiteboard.ts +48 -0
  136. package/src/methods/whiteboard/index.ts +4 -0
  137. package/src/methods/whiteboard/launchConfigureWhiteboard.ts +29 -0
  138. package/src/producers/producerEmits/joinConRoom.ts +153 -0
  139. package/src/sockets/SocketManager.ts +232 -0
  140. package/src/types/shared-base-types.ts +752 -0
  141. package/src/types/types.ts +84 -0
@@ -0,0 +1,100 @@
1
+ export interface CheckGridOptions {
2
+ rows: number;
3
+ cols: number;
4
+ actives: number;
5
+ }
6
+
7
+ export type CheckGridType = (options: CheckGridOptions) => Promise<[boolean, number, number, number, number, number, number] | void>;
8
+
9
+ /**
10
+ * Checks the grid configuration and calculates various parameters based on the number of rows, columns, and active elements.
11
+ *
12
+ * @param {CheckGridOptions} options - The options for checking the grid.
13
+ * @param {number} options.rows - The number of rows in the grid.
14
+ * @param {number} options.cols - The number of columns in the grid.
15
+ * @param {number} options.actives - The number of active elements in the grid.
16
+ * @returns {Promise<[boolean, number, number, number, number, number, number] | void>} A promise that resolves to a tuple containing:
17
+ * - `removeAltGrid` (boolean): Indicates whether to remove the alternate grid.
18
+ * - `numtoadd` (number): The number of elements to add.
19
+ * - `numRows` (number): The number of rows.
20
+ * - `numCols` (number): The number of columns.
21
+ * - `remainingVideos` (number): The number of remaining videos.
22
+ * - `actualRows` (number): The actual number of rows.
23
+ * - `lastrowcols` (number): The number of columns in the last row.
24
+ *
25
+ * If an error occurs, it logs the error to the console.
26
+ *
27
+ * @example
28
+ * const options = {
29
+ * rows: 3,
30
+ * cols: 4,
31
+ * actives: 10,
32
+ * };
33
+ *
34
+ * checkGrid(options)
35
+ * .then(result => {
36
+ * console.log('Grid check result:', result);
37
+ * // Example output: [true, 2, 3, 4, 2, 3, 4]
38
+ * })
39
+ * .catch(error => {
40
+ * console.error('Error checking grid:', error);
41
+ * });
42
+ */
43
+
44
+ export async function checkGrid({ rows, cols, actives }: CheckGridOptions): Promise<[boolean, number, number, number, number, number, number] | void> {
45
+ try {
46
+ let numRows = 0;
47
+ let numCols = 0;
48
+ let lastrow = 0;
49
+ let lastrowcols = 0;
50
+ let remainingVideos = 0;
51
+ let numtoadd = 0;
52
+ let actualRows = 0;
53
+ let removeAltGrid = false;
54
+
55
+ if (rows * cols !== actives) {
56
+ if (rows * cols > actives) {
57
+ const res = actives - (rows - 1) * cols;
58
+ if (cols * 0.5 < res) {
59
+ lastrow = rows;
60
+ lastrowcols = res;
61
+ remainingVideos = lastrowcols;
62
+ } else {
63
+ lastrowcols = res + cols;
64
+ lastrow = rows - 1;
65
+ remainingVideos = lastrowcols;
66
+ }
67
+
68
+ numRows = lastrow - 1;
69
+ numCols = cols;
70
+ numtoadd = (lastrow - 1) * numCols;
71
+ actualRows = lastrow;
72
+
73
+ removeAltGrid = false;
74
+ }
75
+ } else {
76
+ // Perfect fit
77
+ numCols = cols;
78
+ numRows = rows;
79
+ lastrow = rows;
80
+ lastrowcols = cols;
81
+ remainingVideos = 0;
82
+ numtoadd = lastrow * numCols;
83
+ actualRows = lastrow;
84
+ removeAltGrid = true;
85
+ }
86
+
87
+ return [
88
+ removeAltGrid,
89
+ numtoadd,
90
+ numRows,
91
+ numCols,
92
+ remainingVideos,
93
+ actualRows,
94
+ lastrowcols,
95
+ ];
96
+ } catch (error) {
97
+ console.log("checkGrid error", error);
98
+ // throw error;
99
+ }
100
+ }
@@ -0,0 +1,89 @@
1
+ export interface CheckPermissionOptions {
2
+ audioSetting: string;
3
+ videoSetting: string;
4
+ screenshareSetting: string;
5
+ chatSetting: string;
6
+ permissionType: 'audioSetting' | 'videoSetting' | 'screenshareSetting' | 'chatSetting';
7
+ }
8
+
9
+ // Export the type definition for the function
10
+ export type CheckPermissionType = (options: CheckPermissionOptions) => Promise<number>;
11
+
12
+ /**
13
+ * Checks the permission based on the provided settings.
14
+ *
15
+ * @param {CheckPermissionOptions} options - The options for checking permissions.
16
+ * @param {string} options.permissionType - The type of permission to check. Can be "audioSetting", "videoSetting", "screenshareSetting", or "chatSetting".
17
+ * @param {string} options.audioSetting - The setting for audio permission. Can be "allow", "approval", or other.
18
+ * @param {string} options.videoSetting - The setting for video permission. Can be "allow", "approval", or other.
19
+ * @param {string} options.screenshareSetting - The setting for screenshare permission. Can be "allow", "approval", or other.
20
+ * @param {string} options.chatSetting - The setting for chat permission. Can be "allow", "approval", or other.
21
+ * @returns {Promise<number>} - Returns 0 if the setting is "allow", 1 if the setting is "approval", and 2 for other settings or invalid permission types.
22
+ * @throws Will throw an error if an unexpected error occurs during the permission check.
23
+ *
24
+ * @example
25
+ * const options = {
26
+ * permissionType: 'audioSetting',
27
+ * audioSetting: 'allow',
28
+ * videoSetting: 'approval',
29
+ * screenshareSetting: 'approval',
30
+ * chatSetting: 'allow',
31
+ * };
32
+ *
33
+ * checkPermission(options)
34
+ * .then(result => {
35
+ * console.log('Permission result:', result);
36
+ * })
37
+ * .catch(error => {
38
+ * console.error('Error checking permission:', error);
39
+ * });
40
+ */
41
+
42
+ export async function checkPermission({ permissionType, audioSetting, videoSetting, screenshareSetting, chatSetting }: CheckPermissionOptions) {
43
+ try {
44
+
45
+ // PermissionType is audioSetting, videoSetting, screenshareSetting, chatSetting
46
+ // Perform a switch case to check for the permissionType and return the response
47
+ switch (permissionType) {
48
+ case "audioSetting":
49
+ if (audioSetting === "allow") {
50
+ return 0;
51
+ } else if (audioSetting === "approval") {
52
+ return 1;
53
+ } else {
54
+ return 2;
55
+ }
56
+ case "videoSetting":
57
+ if (videoSetting === "allow") {
58
+ return 0;
59
+ } else if (videoSetting === "approval") {
60
+ return 1;
61
+ } else {
62
+ return 2;
63
+ }
64
+ case "screenshareSetting":
65
+ if (screenshareSetting === "allow") {
66
+ return 0;
67
+ } else if (screenshareSetting === "approval") {
68
+ return 1;
69
+ } else {
70
+ return 2;
71
+ }
72
+ case "chatSetting":
73
+ if (chatSetting === "allow") {
74
+ return 0;
75
+ } else if (chatSetting === "approval") {
76
+ return 1;
77
+ } else {
78
+ return 2;
79
+ }
80
+ default:
81
+ // throw new Error(`Invalid permissionType: ${permissionType}`);
82
+ return 2;
83
+ }
84
+ } catch {
85
+ // console.log('checkPermission error', error);
86
+ // throw error;
87
+ return 2;
88
+ }
89
+ }
@@ -0,0 +1,114 @@
1
+ import { StopShareScreenType, StopShareScreenParameters, RequestScreenShareType, RequestScreenShareParameters, ShowAlert } from "../types/types";
2
+ export interface CheckScreenShareParameters extends StopShareScreenParameters, RequestScreenShareParameters {
3
+ shared: boolean;
4
+ whiteboardStarted: boolean;
5
+ whiteboardEnded: boolean;
6
+ breakOutRoomStarted: boolean;
7
+ breakOutRoomEnded: boolean;
8
+ showAlert?: ShowAlert;
9
+
10
+ // Mediasfu functions
11
+ stopShareScreen: StopShareScreenType;
12
+ requestScreenShare: RequestScreenShareType;
13
+
14
+ getUpdatedAllParams : () => CheckScreenShareParameters;
15
+ [key: string]: any;
16
+ }
17
+
18
+ export interface CheckScreenShareOptions {
19
+ parameters: CheckScreenShareParameters;
20
+ }
21
+
22
+ // Export the type definition for the function
23
+ export type CheckScreenShareType = (options: CheckScreenShareOptions) => Promise<void>;
24
+
25
+ /**
26
+ * Checks the current screen sharing status and either stops or requests screen sharing based on the provided parameters.
27
+ *
28
+ * @param {CheckScreenShareOptions} options - The options for checking screen share.
29
+ * @param {Object} options.parameters - The parameters for screen sharing.
30
+ * @param {boolean} options.parameters.shared - Indicates if the screen is currently being shared.
31
+ * @param {Function} [options.parameters.showAlert] - Function to show alerts.
32
+ * @param {boolean} options.parameters.whiteboardStarted - Indicates if the whiteboard session has started.
33
+ * @param {boolean} options.parameters.whiteboardEnded - Indicates if the whiteboard session has ended.
34
+ * @param {boolean} options.parameters.breakOutRoomStarted - Indicates if the breakout room session has started.
35
+ * @param {boolean} options.parameters.breakOutRoomEnded - Indicates if the breakout room session has ended.
36
+ * @param {Function} options.parameters.stopShareScreen - Function to stop screen sharing.
37
+ * @param {Function} options.parameters.requestScreenShare - Function to request screen sharing.
38
+ *
39
+ * @returns {Promise<void>} A promise that resolves when the screen sharing status has been checked and the appropriate action has been taken.
40
+ *
41
+ * @throws Will log an error message if an error occurs during the process.
42
+ *
43
+ * @example
44
+ * const options = {
45
+ * parameters: {
46
+ * shared: false,
47
+ * showAlert: showAlertFunction,
48
+ * whiteboardStarted: false,
49
+ * whiteboardEnded: false,
50
+ * breakOutRoomStarted: false,
51
+ * breakOutRoomEnded: false,
52
+ * stopShareScreen: stopShareScreenFunction,
53
+ * requestScreenShare: requestScreenShareFunction,
54
+ * },
55
+ * };
56
+ *
57
+ * checkScreenShare(options)
58
+ * .then(() => {
59
+ * console.log('Screen share checked successfully');
60
+ * })
61
+ * .catch((error) => {
62
+ * console.error('Error checking screen share:', error);
63
+ * });
64
+ */
65
+
66
+ export async function checkScreenShare({ parameters }: CheckScreenShareOptions): Promise<void> {
67
+ try {
68
+ const {
69
+ shared,
70
+ showAlert,
71
+ whiteboardStarted,
72
+ whiteboardEnded,
73
+ breakOutRoomStarted,
74
+ breakOutRoomEnded,
75
+
76
+ //mediasfu functions
77
+ stopShareScreen,
78
+ requestScreenShare,
79
+ } = parameters;
80
+
81
+ // Stop screen share if already shared or request screen share if not shared
82
+ if (shared) {
83
+ if (whiteboardStarted && !whiteboardEnded) {
84
+ showAlert?.({
85
+ message: "Screen share is not allowed when whiteboard is active",
86
+ type: "danger",
87
+ });
88
+ return;
89
+ }
90
+ await stopShareScreen({ parameters });
91
+ } else {
92
+ // Can't share if breakout room is active
93
+ if (breakOutRoomStarted && !breakOutRoomEnded) {
94
+ showAlert?.({
95
+ message: "Screen share is not allowed when breakout room is active",
96
+ type: "danger",
97
+ });
98
+ return;
99
+ }
100
+
101
+ if (whiteboardStarted && !whiteboardEnded) {
102
+ showAlert?.({
103
+ message: "Screen share is not allowed when whiteboard is active",
104
+ type: "danger",
105
+ });
106
+ return;
107
+ }
108
+ await requestScreenShare({ parameters });
109
+ }
110
+ } catch (error) {
111
+ console.log("checkScreenShare error", error);
112
+ // throw error;
113
+ }
114
+ }
@@ -0,0 +1,401 @@
1
+ import {
2
+ Stream, Participant, ReorderStreamsType, ReorderStreamsParameters, PrepopulateUserMediaParameters,
3
+ PrepopulateUserMediaType, GetVideosType, RePortType, RePortParameters, EventType
4
+ } from '../types/types';
5
+
6
+
7
+
8
+
9
+ export interface CloseAndResizeParameters extends ReorderStreamsParameters, PrepopulateUserMediaParameters, RePortParameters {
10
+ allAudioStreams: (Stream | Participant)[];
11
+ allVideoStreams: (Stream | Participant)[];
12
+ activeNames: string[];
13
+ participants: Participant[];
14
+ streamNames: Stream[];
15
+ recordingDisplayType: "video" | "media" | "all";
16
+ recordingVideoOptimized: boolean;
17
+ adminIDStream?: string;
18
+ newLimitedStreams: (Stream | Participant)[];
19
+ newLimitedStreamsIDs: string[];
20
+ oldAllStreams: (Stream | Participant)[];
21
+ shareScreenStarted: boolean;
22
+ shared: boolean;
23
+ meetingDisplayType: string;
24
+ defer_receive: boolean;
25
+ lock_screen: boolean;
26
+ firstAll: boolean;
27
+ first_round: boolean;
28
+ gotAllVids: boolean;
29
+ eventType: EventType;
30
+ hostLabel: string;
31
+ shareEnded: boolean;
32
+ updateMainWindow: boolean;
33
+ updateActiveNames: (activeNames: string[]) => void;
34
+ updateAllAudioStreams: (allAudioStreams: (Stream | Participant)[]) => void;
35
+ updateShareScreenStarted: (shareScreenStarted: boolean) => void;
36
+ updateUpdateMainWindow: (updateMainWindow: boolean) => void;
37
+ updateNewLimitedStreams: (newLimitedStreams: (Stream | Participant)[]) => void;
38
+ updateOldAllStreams: (oldAllStreams: (Stream | Participant)[]) => void;
39
+ updateDefer_receive: (defer_receive: boolean) => void;
40
+ updateMainHeightWidth: (heightWidth: number) => void;
41
+ updateShareEnded: (shareEnded: boolean) => void;
42
+ updateLock_screen: (lock_screen: boolean) => void;
43
+ updateFirstAll: (firstAll: boolean) => void;
44
+ updateFirst_round: (first_round: boolean) => void;
45
+
46
+ // mediasfu functions
47
+ reorderStreams: ReorderStreamsType;
48
+ prepopulateUserMedia: PrepopulateUserMediaType;
49
+ getVideos: GetVideosType;
50
+ rePort: RePortType;
51
+ getUpdatedAllParams: () => CloseAndResizeParameters;
52
+ [key: string]: any;
53
+ }
54
+
55
+ export interface CloseAndResizeOptions {
56
+ producerId: string;
57
+ kind: string;
58
+ parameters: CloseAndResizeParameters;
59
+ }
60
+
61
+ // Export the type definition for the function
62
+ export type CloseAndResizeType = (options: CloseAndResizeOptions) => Promise<void>;
63
+
64
+
65
+ /**
66
+ * Closes and resizes the video and audio elements based on the provided options.
67
+ *
68
+ * @param {CloseAndResizeOptions} options - The options for closing and resizing.
69
+ * @param {string} options.producerId - The ID of the producer.
70
+ * @param {string} options.kind - The kind of media (audio, video, screenshare, or screen).
71
+ * @param {object} options.parameters - The parameters for the operation.
72
+ * @param {function} options.parameters.getUpdatedAllParams - Function to get updated parameters.
73
+ * @param {Array} options.parameters.allAudioStreams - Array of all audio streams.
74
+ * @param {Array} options.parameters.allVideoStreams - Array of all video streams.
75
+ * @param {Array} options.parameters.activeNames - Array of active participant names.
76
+ * @param {Array} options.parameters.participants - Array of participants.
77
+ * @param {Array} options.parameters.streamNames - Array of stream names.
78
+ * @param {string} options.parameters.recordingDisplayType - Type of recording display.
79
+ * @param {boolean} options.parameters.recordingVideoOptimized - Whether recording is video optimized.
80
+ * @param {string} options.parameters.adminIDStream - ID of the admin stream.
81
+ * @param {Array} options.parameters.newLimitedStreams - Array of new limited streams.
82
+ * @param {Array} options.parameters.newLimitedStreamsIDs - Array of new limited stream IDs.
83
+ * @param {Array} options.parameters.oldAllStreams - Array of old all streams.
84
+ * @param {boolean} options.parameters.shareScreenStarted - Whether screen sharing has started.
85
+ * @param {boolean} options.parameters.shared - Whether sharing is active.
86
+ * @param {string} options.parameters.meetingDisplayType - Type of meeting display.
87
+ * @param {boolean} options.parameters.defer_receive - Whether to defer receiving.
88
+ * @param {boolean} options.parameters.lock_screen - Whether the screen is locked.
89
+ * @param {boolean} options.parameters.firstAll - Whether it is the first all.
90
+ * @param {boolean} options.parameters.first_round - Whether it is the first round.
91
+ * @param {boolean} options.parameters.gotAllVids - Whether all videos are received.
92
+ * @param {string} options.parameters.eventType - Type of event.
93
+ * @param {string} options.parameters.hostLabel - Label of the host.
94
+ * @param {boolean} options.parameters.shareEnded - Whether sharing has ended.
95
+ * @param {boolean} options.parameters.updateMainWindow - Whether to update the main window.
96
+ * @param {function} options.parameters.updateActiveNames - Function to update active names.
97
+ * @param {function} options.parameters.updateAllAudioStreams - Function to update all audio streams.
98
+ * @param {function} options.parameters.updateAllVideoStreams - Function to update all video streams.
99
+ * @param {function} options.parameters.updateShareScreenStarted - Function to update share screen started status.
100
+ * @param {function} options.parameters.updateUpdateMainWindow - Function to update main window status.
101
+ * @param {function} options.parameters.updateNewLimitedStreams - Function to update new limited streams.
102
+ * @param {function} options.parameters.updateOldAllStreams - Function to update old all streams.
103
+ * @param {function} options.parameters.updateDefer_receive - Function to update defer receive status.
104
+ * @param {function} options.parameters.updateMainHeightWidth - Function to update main height and width.
105
+ * @param {function} options.parameters.updateShareEnded - Function to update share ended status.
106
+ * @param {function} options.parameters.updateLock_screen - Function to update lock screen status.
107
+ * @param {function} options.parameters.updateFirstAll - Function to update first all status.
108
+ * @param {function} options.parameters.updateFirst_round - Function to update first round status.
109
+ * @param {function} options.parameters.reorderStreams - Function to reorder streams.
110
+ * @param {function} options.parameters.prepopulateUserMedia - Function to prepopulate user media.
111
+ * @param {function} options.parameters.getVideos - Function to get videos.
112
+ * @param {function} options.parameters.rePort - Function to report.
113
+ *
114
+ * @returns {Promise<void>} A promise that resolves when the operation is complete.
115
+ *
116
+ * @example
117
+ * const options = {
118
+ * producerId: 'producerId',
119
+ * kind: 'video',
120
+ * parameters: {
121
+ * getUpdatedAllParams: getUpdatedAllParamsFunction,
122
+ * allAudioStreams: [],
123
+ * allVideoStreams: [],
124
+ * activeNames: [],
125
+ * participants: [],
126
+ * streamNames: [],
127
+ * recordingDisplayType: 'video',
128
+ * recordingVideoOptimized: true,
129
+ * adminIDStream: 'adminId',
130
+ * newLimitedStreams: [],
131
+ * newLimitedStreamsIDs: [],
132
+ * oldAllStreams: [],
133
+ * shareScreenStarted: false,
134
+ * shared: false,
135
+ * meetingDisplayType: 'video',
136
+ * defer_receive: false,
137
+ * lock_screen: false,
138
+ * firstAll: false,
139
+ * first_round: false,
140
+ * gotAllVids: false,
141
+ * eventType: 'conference',
142
+ * hostLabel: 'host',
143
+ * shareEnded: false,
144
+ * updateMainWindow: true,
145
+ * updateActiveNames: updateActiveNamesFunction,
146
+ * updateAllAudioStreams: updateAllAudioStreamsFunction,
147
+ * updateAllVideoStreams: updateAllVideoStreamsFunction,
148
+ * updateShareScreenStarted: updateShareScreenStartedFunction,
149
+ * updateUpdateMainWindow: updateUpdateMainWindowFunction,
150
+ * updateNewLimitedStreams: updateNewLimitedStreamsFunction,
151
+ * updateOldAllStreams: updateOldAllStreamsFunction,
152
+ * updateDefer_receive: updateDefer_receiveFunction,
153
+ * updateMainHeightWidth: updateMainHeightWidthFunction,
154
+ * updateShareEnded: updateShareEndedFunction,
155
+ * updateLock_screen: updateLock_screenFunction,
156
+ * updateFirstAll: updateFirstAllFunction,
157
+ * updateFirst_round: updateFirst_roundFunction,
158
+ * reorderStreams: reorderStreamsFunction,
159
+ * prepopulateUserMedia: prepopulateUserMediaFunction,
160
+ * getVideos: getVideosFunction,
161
+ * rePort: rePortFunction,
162
+ * },
163
+ * };
164
+ *
165
+ * closeAndResize(options)
166
+ * .then(() => {
167
+ * console.log('Closed and resized successfully');
168
+ * });
169
+ */
170
+
171
+
172
+ export const closeAndResize = async ({ producerId, kind, parameters }: CloseAndResizeOptions): Promise<void> => {
173
+
174
+ let { getUpdatedAllParams } = parameters;
175
+ parameters = getUpdatedAllParams();
176
+
177
+ let {
178
+ allAudioStreams,
179
+ allVideoStreams,
180
+ activeNames,
181
+ participants,
182
+ streamNames,
183
+ recordingDisplayType,
184
+ recordingVideoOptimized,
185
+ adminIDStream,
186
+ newLimitedStreams,
187
+ newLimitedStreamsIDs,
188
+ oldAllStreams,
189
+ shareScreenStarted,
190
+ shared,
191
+ meetingDisplayType,
192
+ defer_receive,
193
+ lock_screen,
194
+ firstAll,
195
+ first_round,
196
+ gotAllVids,
197
+ eventType,
198
+
199
+ hostLabel,
200
+ shareEnded,
201
+ updateMainWindow,
202
+ updateActiveNames,
203
+ updateAllAudioStreams,
204
+ updateAllVideoStreams,
205
+
206
+ updateShareScreenStarted,
207
+ updateUpdateMainWindow,
208
+ updateNewLimitedStreams,
209
+ updateOldAllStreams,
210
+ updateDefer_receive,
211
+ updateMainHeightWidth,
212
+ updateShareEnded,
213
+ updateLock_screen,
214
+ updateFirstAll,
215
+ updateFirst_round,
216
+
217
+ //mediasfu functions
218
+ reorderStreams,
219
+ prepopulateUserMedia,
220
+ getVideos,
221
+ rePort,
222
+ } = parameters;
223
+
224
+ //function to close and resize the video and audio elements
225
+
226
+ let participant: Participant | undefined;
227
+
228
+ if (kind === "audio") {
229
+ //stop the audio by removing the miniAudio with id = producerId
230
+
231
+ //remove the audio from the allAudioStreams array
232
+ allAudioStreams = allAudioStreams.filter(function (audioStream) {
233
+ return audioStream.producerId !== producerId;
234
+ });
235
+
236
+ updateAllAudioStreams(allAudioStreams);
237
+
238
+ if (recordingDisplayType == "video" && recordingVideoOptimized == true) {
239
+ // optimize the video display
240
+ } else {
241
+ //get the name of the participant with the producerId
242
+ participant = participants.find(
243
+ (obj) => obj.audioID === producerId
244
+ );
245
+
246
+ if (participant) {
247
+ //check if the participants videoID is not null or ""
248
+ if (participant.videoID !== null && participant.videoID !== "") {
249
+ // found a participant with the producerId
250
+ } else {
251
+ //remove the participant from the activeNames array
252
+ activeNames = activeNames.filter(function (name) {
253
+ return name !== participant!.name;
254
+ });
255
+ updateActiveNames(activeNames);
256
+ }
257
+ }
258
+ }
259
+
260
+ let checker = false;
261
+ let alt_checker = false;
262
+
263
+ if (meetingDisplayType == "video") {
264
+ checker =
265
+ participant![0].videoID != null &&
266
+ participant![0].videoID != "" &&
267
+ participant![0].videoID != undefined;
268
+ } else {
269
+ checker = true;
270
+ alt_checker = true;
271
+ }
272
+
273
+ if (checker) {
274
+ if (shareScreenStarted || shared) {
275
+ if (!alt_checker) {
276
+ await reorderStreams({ parameters });
277
+ }
278
+ } else {
279
+ if (alt_checker && meetingDisplayType != "video") {
280
+ await reorderStreams({ add: false, screenChanged: true, parameters });
281
+ }
282
+ }
283
+ }
284
+ } else if (kind === "video") {
285
+ //update the video elements by removing the miniVideo with id = producerId
286
+ //remove the video from the allVideoStreams array
287
+
288
+ //check if producerId == adminidstream
289
+ if (producerId == adminIDStream) {
290
+ updateMainWindow = true;
291
+ updateUpdateMainWindow(updateMainWindow);
292
+ }
293
+
294
+ try {
295
+ allVideoStreams = allVideoStreams.filter(function (videoStream) {
296
+ return videoStream.producerId !== producerId;
297
+ });
298
+
299
+ updateAllVideoStreams(allVideoStreams);
300
+
301
+ try {
302
+ //try remove it from oldVideoStreams
303
+ oldAllStreams = oldAllStreams.filter(function (videoStream) {
304
+ return videoStream.producerId !== producerId;
305
+ });
306
+
307
+ updateOldAllStreams(oldAllStreams);
308
+ } catch { // Handle error
309
+ }
310
+
311
+ try {
312
+ //try remove it from newLimitedStreams
313
+ newLimitedStreams = newLimitedStreams.filter(function (
314
+ videoStream
315
+ ) {
316
+ return videoStream.producerId !== producerId;
317
+ });
318
+
319
+ updateNewLimitedStreams(newLimitedStreams);
320
+ } catch {
321
+ // Handle error
322
+ }
323
+ } catch {
324
+ try {
325
+ //try remove it from oldVideoStreams
326
+ oldAllStreams = oldAllStreams.filter(function (videoStream) {
327
+ return videoStream.producerId !== producerId;
328
+ });
329
+ updateOldAllStreams(oldAllStreams);
330
+ } catch {
331
+ // Handle error
332
+ }
333
+ }
334
+
335
+ try {
336
+ //remove the participant from activeNames
337
+ activeNames = activeNames.filter(function (name) {
338
+ //get the participant with the producerId
339
+ let participant = streamNames.find(
340
+ (obj) => obj.producerId === producerId
341
+ );
342
+
343
+ return name !== participant!['name'];
344
+ });
345
+
346
+ updateActiveNames(activeNames);
347
+ } catch {
348
+ // Handle error
349
+ }
350
+
351
+ if (lock_screen) {
352
+ defer_receive = true;
353
+ // check if the video is the one being displayed (i.e. (newLimitedStreamsIDs))
354
+ if (newLimitedStreamsIDs.includes(producerId)) {
355
+ await prepopulateUserMedia({ name: hostLabel, parameters });
356
+ await reorderStreams({ add: false, screenChanged: true, parameters });
357
+ }
358
+ } else {
359
+ await prepopulateUserMedia({ name: hostLabel, parameters });
360
+ await reorderStreams({ add: false, screenChanged: true, parameters });
361
+ }
362
+ } else if (kind === "screenshare" || kind === "screen") {
363
+ //update the video elements by removing the mainVideo with id = producerId
364
+ updateMainWindow = true;
365
+
366
+ //screenshare stuff
367
+ shareScreenStarted = false;
368
+ shareEnded = true;
369
+
370
+ lock_screen = false;
371
+ firstAll = false;
372
+ first_round = false;
373
+
374
+ updateUpdateMainWindow(updateMainWindow);
375
+ updateShareScreenStarted(shareScreenStarted);
376
+ updateShareEnded(shareEnded);
377
+ updateLock_screen(lock_screen);
378
+ updateFirstAll(firstAll);
379
+ updateFirst_round(first_round);
380
+
381
+ if (!gotAllVids || defer_receive) {
382
+ defer_receive = false;
383
+ updateDefer_receive(defer_receive);
384
+ await getVideos({
385
+ participants,
386
+ allVideoStreams,
387
+ oldAllStreams,
388
+ updateAllVideoStreams,
389
+ updateOldAllStreams,
390
+ });
391
+ await rePort({ parameters });
392
+ }
393
+
394
+ if (eventType == "conference") {
395
+ updateMainHeightWidth(0);
396
+ }
397
+
398
+ await prepopulateUserMedia({ name: hostLabel, parameters });
399
+ await reorderStreams({ add: false, screenChanged: true, parameters });
400
+ }
401
+ }