-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathvideoGalleryUtils.ts
269 lines (251 loc) · 9.37 KB
/
videoGalleryUtils.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { DominantSpeakersInfo } from '@azure/communication-calling';
import { SpotlightedParticipant } from '@azure/communication-calling';
import { ParticipantRole } from '@azure/communication-calling';
import { memoizeFnAll, toFlatCommunicationIdentifier } from '@internal/acs-ui-common';
import { RemoteParticipantState, RemoteVideoStreamState } from '@internal/calling-stateful-client';
import { VideoGalleryRemoteParticipant, VideoGalleryStream, MediaAccess } from '@internal/react-components';
import memoizeOne from 'memoize-one';
import { _convertParticipantState, ParticipantConnectionState } from './callUtils';
import { maskDisplayNameWithRole } from './callUtils';
import { checkIsSpeaking } from './SelectorUtils';
import { isPhoneNumberIdentifier } from '@azure/communication-common';
import { RaisedHandState } from '@internal/calling-stateful-client';
import { Reaction } from '@internal/react-components';
import { memoizedConvertToVideoTileReaction } from './participantListSelectorUtils';
import { Spotlight } from '@internal/react-components';
/** @internal */
export const _dominantSpeakersWithFlatId = (dominantSpeakers?: DominantSpeakersInfo): undefined | string[] => {
return dominantSpeakers?.speakersList?.map(toFlatCommunicationIdentifier);
};
/** @internal */
export type _VideoGalleryRemoteParticipantsMemoFn = (
remoteParticipants: RemoteParticipantState[] | undefined,
isHideAttendeeNamesEnabled?: boolean,
localUserRole?: ParticipantRole
) => VideoGalleryRemoteParticipant[];
/** @internal */
export const _videoGalleryRemoteParticipantsMemo: _VideoGalleryRemoteParticipantsMemoFn = (
remoteParticipants: RemoteParticipantState[] | undefined,
isHideAttendeeNamesEnabled?: boolean,
localUserRole?
): VideoGalleryRemoteParticipant[] => {
if (!remoteParticipants) {
return [];
}
return memoizedAllConvertRemoteParticipant((memoizedFn) => {
return (
Object.values(remoteParticipants)
/**
* hiding participants who are inLobby, idle, or connecting in ACS clients till we can admit users through ACS clients.
* phone users will be in the connecting state until they are connected to the call.
*/
.filter((participant: RemoteParticipantState) => {
return (
!['InLobby', 'Idle', 'Connecting', 'Disconnected'].includes(participant.state) ||
isPhoneNumberIdentifier(participant.identifier)
);
})
.map((participant: RemoteParticipantState) => {
const state = _convertParticipantState(participant);
const displayName = maskDisplayNameWithRole(
participant.displayName,
localUserRole,
participant.role,
isHideAttendeeNamesEnabled
);
const remoteParticipantReaction = memoizedConvertToVideoTileReaction(participant.reactionState);
const spotlight = participant.spotlight;
return memoizedFn(
toFlatCommunicationIdentifier(participant.identifier),
participant.isMuted,
checkIsSpeaking(participant),
participant.videoStreams,
state,
displayName,
participant.raisedHand,
participant.contentSharingStream,
remoteParticipantReaction,
spotlight,
participant.mediaAccess,
participant.role,
/* @conditional-compile-remove(remote-ufd) */
Math.max(
(participant.diagnostics?.networkReceiveQuality?.value ?? 0) as number,
(participant.diagnostics?.networkSendQuality?.value ?? 0) as number
)
);
})
);
});
};
const memoizedAllConvertRemoteParticipant = memoizeFnAll(
(
userId: string,
isMuted: boolean,
isSpeaking: boolean,
videoStreams: { [key: number]: RemoteVideoStreamState },
state: ParticipantConnectionState,
displayName?: string,
raisedHand?: RaisedHandState,
contentSharingStream?: HTMLElement,
reaction?: Reaction,
spotlight?: Spotlight,
mediaAccess?: MediaAccess,
role?: ParticipantRole,
signalStrength?: undefined | /* @conditional-compile-remove(remote-ufd) */ number
): VideoGalleryRemoteParticipant => {
return convertRemoteParticipantToVideoGalleryRemoteParticipant(
userId,
isMuted,
isSpeaking,
videoStreams,
state,
displayName,
raisedHand,
contentSharingStream,
reaction,
spotlight,
signalStrength,
mediaAccess,
role
);
}
);
/** @private */
export const convertRemoteParticipantToVideoGalleryRemoteParticipant = (
userId: string,
isMuted: boolean,
isSpeaking: boolean,
videoStreams: { [key: number]: RemoteVideoStreamState },
state: ParticipantConnectionState,
displayName?: string,
raisedHand?: RaisedHandState,
contentSharingStream?: HTMLElement,
reaction?: Reaction,
spotlight?: Spotlight,
signalStrength?: undefined | /* @conditional-compile-remove(remote-ufd) */ number,
mediaAccess?: MediaAccess,
role?: ParticipantRole
): VideoGalleryRemoteParticipant => {
const rawVideoStreamsArray = Object.values(videoStreams);
let videoStream: VideoGalleryStream | undefined = undefined;
let screenShareStream: VideoGalleryStream | undefined = undefined;
/**
* There is a bug from the calling sdk where if a user leaves and rejoins immediately
* it adds 2 more potential streams this remote participant can use. The old 2 streams
* still show as available and that is how we got a frozen stream in this case. The stopgap
* until streams accurately reflect their availability is to always prioritize the latest streams of a certain type
* e.g findLast instead of find
*/
const sdkRemoteVideoStream =
Object.values(rawVideoStreamsArray).findLast((i) => i.mediaStreamType === 'Video' && i.isAvailable) ||
Object.values(rawVideoStreamsArray).findLast((i) => i.mediaStreamType === 'Video');
const sdkScreenShareStream =
Object.values(rawVideoStreamsArray).findLast((i) => i.mediaStreamType === 'ScreenSharing' && i.isAvailable) ||
Object.values(rawVideoStreamsArray).findLast((i) => i.mediaStreamType === 'ScreenSharing');
if (sdkRemoteVideoStream) {
videoStream = convertRemoteVideoStreamToVideoGalleryStream(sdkRemoteVideoStream);
}
if (sdkScreenShareStream) {
screenShareStream = convertRemoteVideoStreamToVideoGalleryStream(sdkScreenShareStream);
}
if (contentSharingStream) {
screenShareStream = convertRemoteContentSharingStreamToVideoGalleryStream(contentSharingStream);
}
return {
userId,
displayName,
isMuted,
isSpeaking,
videoStream,
screenShareStream,
isScreenSharingOn: screenShareStream !== undefined && screenShareStream.isAvailable,
state,
raisedHand,
reaction,
spotlight,
mediaAccess,
canAudioBeForbidden: role === 'Attendee',
canVideoBeForbidden: role === 'Attendee',
/* @conditional-compile-remove(remote-ufd) */
signalStrength
};
};
const convertRemoteVideoStreamToVideoGalleryStream = (stream: RemoteVideoStreamState): VideoGalleryStream => {
return {
id: stream.id,
isAvailable: stream.isAvailable,
isReceiving: stream.isReceiving,
isMirrored: stream.view?.isMirrored,
renderElement: stream.view?.target,
scalingMode: stream.view?.scalingMode,
streamSize: stream.streamSize
};
};
const convertRemoteContentSharingStreamToVideoGalleryStream = (stream: HTMLElement): VideoGalleryStream => {
return {
isAvailable: !!stream,
isReceiving: true,
isMirrored: false,
renderElement: stream
};
};
/** @private */
export const memoizeLocalParticipant = memoizeOne(
(
identifier,
displayName,
isMuted,
isScreenSharingOn,
localVideoStream,
localScreenSharingStream,
role,
raisedHand,
reaction,
localSpotlight,
capabilities
) => ({
userId: identifier,
displayName: displayName ?? '',
isMuted: isMuted,
isScreenSharingOn: isScreenSharingOn,
videoStream: {
isAvailable: !!localVideoStream,
isMirrored: localVideoStream?.view?.isMirrored,
renderElement: localVideoStream?.view?.target
},
screenShareStream: {
isAvailable: !!localScreenSharingStream,
renderElement: localScreenSharingStream?.view?.target
},
role,
raisedHand: raisedHand,
reaction,
spotlight: localSpotlight,
capabilities,
mediaAccess: {
isAudioPermitted: capabilities?.unmuteMic ? capabilities.unmuteMic.isPresent : true,
isVideoPermitted: capabilities?.turnVideoOn ? capabilities.turnVideoOn.isPresent : true
}
})
);
/** @private */
export const memoizeSpotlightedParticipantIds = memoizeOne((spotlightedParticipants) =>
spotlightedParticipants?.map((p: SpotlightedParticipant) => toFlatCommunicationIdentifier(p.identifier))
);
/** @private */
export const memoizeTogetherModeStreams = memoizeOne((togetherModeStreams) => ({
mainVideoStream: {
id: togetherModeStreams?.mainVideoStream?.id,
isReceiving: togetherModeStreams?.mainVideoStream?.isReceiving,
isAvailable: togetherModeStreams?.mainVideoStream?.isAvailable,
renderElement: togetherModeStreams?.mainVideoStream?.view?.target,
streamSize: togetherModeStreams?.mainVideoStream?.streamSize
}
}));
/** @private */
export const memoizeTogetherModeSeatingPositions = memoizeOne(
(togetherModeSeatingCoordinates) => togetherModeSeatingCoordinates
);