-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathSpeakerVideoLayout.tsx
More file actions
249 lines (230 loc) · 8.78 KB
/
SpeakerVideoLayout.tsx
File metadata and controls
249 lines (230 loc) · 8.78 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { LayerHost, Stack, mergeStyles, useTheme } from '@fluentui/react';
/* @conditional-compile-remove(click-to-call) */
import { LocalVideoTileSize } from '../VideoGallery';
import { LayoutProps } from './Layout';
import { isNarrowWidth } from '../utils/responsive';
/* @conditional-compile-remove(vertical-gallery) */
import { isShortHeight } from '../utils/responsive';
import React, { useMemo, useRef, useState } from 'react';
import { OverflowGallery } from './OverflowGallery';
import {
SMALL_FLOATING_MODAL_SIZE_REM,
LARGE_FLOATING_MODAL_SIZE_REM,
localVideoTileContainerStyle
} from './styles/FloatingLocalVideo.styles';
/* @conditional-compile-remove(vertical-gallery) */
import {
VERTICAL_GALLERY_FLOATING_MODAL_SIZE_REM,
SHORT_VERTICAL_GALLERY_FLOATING_MODAL_SIZE_REM
} from './styles/FloatingLocalVideo.styles';
import { useOrganizedParticipants } from './utils/videoGalleryLayoutUtils';
import { GridLayout } from '../GridLayout';
import { rootLayoutStyle } from './styles/FloatingLocalVideoLayout.styles';
import { layerHostStyle, innerLayoutStyle } from './styles/FloatingLocalVideoLayout.styles';
import { videoGalleryLayoutGap } from './styles/Layout.styles';
import { useId } from '@fluentui/react-hooks';
/**
* Props for {@link FloatingLocalVideoLayout}.
*
* @private
*/
export interface SpeakerVideoLayoutProps extends LayoutProps {
/**
* Whether to display the local video camera switcher button
*/
showCameraSwitcherInLocalPreview?: boolean;
/**
* Height of parent element
*/
parentHeight?: number;
/* @conditional-compile-remove(click-to-call) */
/**
* Local video tile mode
*/
localVideoTileSize?: LocalVideoTileSize;
}
/**
* Layout for the gallery mode to highlight the current dominant speaker
*
* @private
*/
export const SpeakerVideoLayout = (props: SpeakerVideoLayoutProps): JSX.Element => {
const {
remoteParticipants = [],
dominantSpeakers,
localVideoComponent,
screenShareComponent,
onRenderRemoteParticipant,
styles,
maxRemoteVideoStreams,
parentWidth,
/* @conditional-compile-remove(vertical-gallery) */ parentHeight,
/* @conditional-compile-remove(vertical-gallery) */ overflowGalleryPosition = 'HorizontalBottom',
pinnedParticipantUserIds = [],
/* @conditional-compile-remove(click-to-call) */ localVideoTileSize
} = props;
const theme = useTheme();
const isNarrow = parentWidth ? isNarrowWidth(parentWidth) : false;
/* @conditional-compile-remove(vertical-gallery) */
const isShort = parentHeight ? isShortHeight(parentHeight) : false;
// This is for tracking the number of children in the first page of overflow gallery.
// This number will be used for the maxOverflowGalleryDominantSpeakers when organizing the remote participants.
const childrenPerPage = useRef(4);
const { gridParticipants, overflowGalleryParticipants } = useOrganizedParticipants({
remoteParticipants,
dominantSpeakers,
maxRemoteVideoStreams,
isScreenShareActive: !!screenShareComponent,
maxOverflowGalleryDominantSpeakers: screenShareComponent
? childrenPerPage.current - (pinnedParticipantUserIds.length % childrenPerPage.current)
: childrenPerPage.current,
/* @conditional-compile-remove(pinned-participants) */ pinnedParticipantUserIds,
/* @conditional-compile-remove(gallery-layouts) */ layout: 'speaker'
});
let activeVideoStreams = 0;
const gridTiles = gridParticipants.map((p) => {
return onRenderRemoteParticipant(
p,
maxRemoteVideoStreams && maxRemoteVideoStreams >= 0
? p.videoStream?.isAvailable && activeVideoStreams++ < maxRemoteVideoStreams
: p.videoStream?.isAvailable
);
});
const shouldFloatLocalVideo = remoteParticipants.length > 0;
if (!shouldFloatLocalVideo && localVideoComponent) {
gridTiles.push(localVideoComponent);
}
/**
* instantiate indexes available to render with indexes available that would be on first page
*
* For some components which do not strictly follow the order of the array, we might
* re-render the initial tiles -> dispose them -> create new tiles, we need to take care of
* this case when those components are here
*/
const [indexesToRender, setIndexesToRender] = useState<number[]>([]);
const overflowGalleryTiles = overflowGalleryParticipants.map((p, i) => {
return onRenderRemoteParticipant(
p,
maxRemoteVideoStreams && maxRemoteVideoStreams >= 0
? p.videoStream?.isAvailable &&
indexesToRender &&
indexesToRender.includes(i) &&
activeVideoStreams++ < maxRemoteVideoStreams
: p.videoStream?.isAvailable
);
});
const layerHostId = useId('layerhost');
const localVideoSizeRem = useMemo(() => {
if (isNarrow || /*@conditional-compile-remove(click-to-call) */ localVideoTileSize === '9:16') {
return SMALL_FLOATING_MODAL_SIZE_REM;
}
/* @conditional-compile-remove(vertical-gallery) */
if ((overflowGalleryTiles.length > 0 || screenShareComponent) && overflowGalleryPosition === 'VerticalRight') {
return isNarrow
? SMALL_FLOATING_MODAL_SIZE_REM
: isShort
? SHORT_VERTICAL_GALLERY_FLOATING_MODAL_SIZE_REM
: VERTICAL_GALLERY_FLOATING_MODAL_SIZE_REM;
}
/*@conditional-compile-remove(click-to-call) */
if ((overflowGalleryTiles.length > 0 || screenShareComponent) && overflowGalleryPosition === 'HorizontalBottom') {
return localVideoTileSize === '16:9' || !isNarrow ? LARGE_FLOATING_MODAL_SIZE_REM : SMALL_FLOATING_MODAL_SIZE_REM;
}
return LARGE_FLOATING_MODAL_SIZE_REM;
}, [
overflowGalleryTiles.length,
isNarrow,
screenShareComponent,
/* @conditional-compile-remove(vertical-gallery) */ isShort,
/* @conditional-compile-remove(vertical-gallery) */ overflowGalleryPosition,
/* @conditional-compile-remove(click-to-call) */ localVideoTileSize
]);
const wrappedLocalVideoComponent =
localVideoComponent || (screenShareComponent && localVideoComponent) ? (
<Stack
className={mergeStyles(
localVideoTileContainerStyle(
theme,
localVideoSizeRem,
!!screenShareComponent,
/* @conditional-compile-remove(gallery-layouts) */ overflowGalleryPosition
)
)}
>
{localVideoComponent}
</Stack>
) : undefined;
const overflowGallery = useMemo(() => {
if (overflowGalleryTiles.length === 0 && !screenShareComponent) {
return null;
}
return (
<OverflowGallery
/* @conditional-compile-remove(vertical-gallery) */
isShort={isShort}
onFetchTilesToRender={setIndexesToRender}
isNarrow={isNarrow}
shouldFloatLocalVideo={true}
overflowGalleryElements={overflowGalleryTiles}
horizontalGalleryStyles={styles?.horizontalGallery}
/* @conditional-compile-remove(vertical-gallery) */
verticalGalleryStyles={styles?.verticalGallery}
/* @conditional-compile-remove(vertical-gallery) */
overflowGalleryPosition={overflowGalleryPosition}
onChildrenPerPageChange={(n: number) => {
childrenPerPage.current = n;
}}
/>
);
}, [
isNarrow,
/* @conditional-compile-remove(vertical-gallery) */ isShort,
screenShareComponent,
overflowGalleryTiles,
styles?.horizontalGallery,
/* @conditional-compile-remove(vertical-gallery) */ overflowGalleryPosition,
setIndexesToRender,
/* @conditional-compile-remove(vertical-gallery) */ styles?.verticalGallery
]);
return (
<Stack styles={rootLayoutStyle}>
{wrappedLocalVideoComponent}
<LayerHost id={layerHostId} className={mergeStyles(layerHostStyle)} />
<Stack
/* @conditional-compile-remove(vertical-gallery) */
horizontal={overflowGalleryPosition === 'VerticalRight'}
styles={innerLayoutStyle}
tokens={videoGalleryLayoutGap}
>
{
/* @conditional-compile-remove(gallery-layouts) */ props.overflowGalleryPosition === 'HorizontalTop' ? (
overflowGallery
) : (
<></>
)
}
{screenShareComponent ? (
screenShareComponent
) : (
<GridLayout key="grid-layout" styles={styles?.gridLayout}>
{gridTiles}
</GridLayout>
)}
{overflowGalleryTrampoline(
overflowGallery,
/* @conditional-compile-remove(gallery-layouts) */ props.overflowGalleryPosition
)}
</Stack>
</Stack>
);
};
const overflowGalleryTrampoline = (
gallery: JSX.Element | null,
galleryPosition?: 'HorizontalBottom' | 'VerticalRight' | 'HorizontalTop'
): JSX.Element | null => {
/* @conditional-compile-remove(gallery-layouts) */
return galleryPosition !== 'HorizontalTop' ? gallery : <></>;
return gallery;
};