forked from bigbluebutton/bigbluebutton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.jsx
executable file
·275 lines (249 loc) · 8.74 KB
/
component.jsx
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
/* eslint-disable no-nested-ternary */
import React, { useEffect, useRef, useState } from 'react';
import { injectIntl } from 'react-intl';
import PropTypes from 'prop-types';
import UserActions from '/imports/ui/components/video-provider/video-list/video-list-item/user-actions/component';
import UserStatus from '/imports/ui/components/video-provider/video-list/video-list-item/user-status/component';
import PinArea from '/imports/ui/components/video-provider/video-list/video-list-item/pin-area/component';
import UserAvatarVideo from '/imports/ui/components/video-provider/video-list/video-list-item/user-avatar/component';
import ViewActions from '/imports/ui/components/video-provider/video-list/video-list-item/view-actions/component';
import {
isStreamStateUnhealthy,
subscribeToStreamStateChange,
unsubscribeFromStreamStateChange,
} from '/imports/ui/services/bbb-webrtc-sfu/stream-state-service';
import Settings from '/imports/ui/services/settings';
import VideoService from '/imports/ui/components/video-provider/service';
import Styled from './styles';
import { withDragAndDrop } from './drag-and-drop/component';
const VIDEO_CONTAINER_WIDTH_BOUND = 125;
const VideoListItem = (props) => {
const {
name, voiceUser, isFullscreenContext, layoutContextDispatch, user, onHandleVideoFocus,
cameraId, numOfStreams, focused, onVideoItemMount, onVideoItemUnmount, onVirtualBgDrop,
makeDragOperations, dragging, draggingOver, isRTL,
} = props;
const [videoDataLoaded, setVideoDataLoaded] = useState(false);
const [isStreamHealthy, setIsStreamHealthy] = useState(false);
const [isMirrored, setIsMirrored] = useState(VideoService.mirrorOwnWebcam(user?.userId));
const [isVideoSqueezed, setIsVideoSqueezed] = useState(false);
const [isCamDisabled, setIsCamDisabled] = useState(false);
const resizeObserver = new ResizeObserver((entry) => {
if (entry && entry[0]?.contentRect?.width < VIDEO_CONTAINER_WIDTH_BOUND) {
return setIsVideoSqueezed(true);
}
return setIsVideoSqueezed(false);
});
const videoTag = useRef();
const videoContainer = useRef();
const videoIsReady = isStreamHealthy && videoDataLoaded && !isCamDisabled;
const { animations } = Settings.application;
const talking = voiceUser?.talking;
const onStreamStateChange = (e) => {
const { streamState } = e.detail;
const newHealthState = !isStreamStateUnhealthy(streamState);
e.stopPropagation();
setIsStreamHealthy(newHealthState);
};
const onLoadedData = () => {
setVideoDataLoaded(true);
window.dispatchEvent(new Event('resize'));
/* used when re-sharing cameras after leaving a breakout room.
it is needed in cases where the user has more than one active camera
so we only share the second camera after the first
has finished loading (can't share more than one at the same time) */
Session.set('canConnect', true);
};
// component did mount
useEffect(() => {
subscribeToStreamStateChange(cameraId, onStreamStateChange);
onVideoItemMount(videoTag.current);
resizeObserver.observe(videoContainer.current);
videoTag?.current?.addEventListener('loadeddata', onLoadedData);
return () => {
videoTag?.current?.removeEventListener('loadeddata', onLoadedData);
resizeObserver.disconnect();
};
}, []);
// component will mount
useEffect(() => {
const playElement = (elem) => {
if (elem.paused) {
elem.play().catch((error) => {
// NotAllowedError equals autoplay issues, fire autoplay handling event
if (error.name === 'NotAllowedError') {
const tagFailedEvent = new CustomEvent('videoPlayFailed', { detail: { mediaTag: elem } });
window.dispatchEvent(tagFailedEvent);
}
});
}
};
if (!isCamDisabled && videoDataLoaded) {
playElement(videoTag.current);
}
if (isCamDisabled) {
videoTag.current.pause();
}
}, [isCamDisabled, videoDataLoaded]);
// component will unmount
useEffect(() => () => {
unsubscribeFromStreamStateChange(cameraId, onStreamStateChange);
onVideoItemUnmount(cameraId);
}, []);
useEffect(() => {
setIsCamDisabled(Settings.application.disableCam);
}, [Settings.application.disableCam]);
const renderSqueezedButton = () => (
<UserActions
name={name}
user={user}
videoContainer={videoContainer}
isVideoSqueezed={isVideoSqueezed}
cameraId={cameraId}
numOfStreams={numOfStreams}
onHandleVideoFocus={onHandleVideoFocus}
focused={focused}
onHandleMirror={() => setIsMirrored((value) => !value)}
isRTL={isRTL}
onHandleDisableCam={() => setIsCamDisabled((value) => !value)}
/>
);
const renderWebcamConnecting = () => (
<Styled.WebcamConnecting
data-test="webcamConnecting"
animations={animations}
>
<UserAvatarVideo
user={user}
voiceUser={voiceUser}
unhealthyStream={videoDataLoaded && !isStreamHealthy}
squeezed={false}
/>
<Styled.BottomBar>
<UserActions
name={name}
user={user}
cameraId={cameraId}
numOfStreams={numOfStreams}
onHandleVideoFocus={onHandleVideoFocus}
focused={focused}
onHandleMirror={() => setIsMirrored((value) => !value)}
isRTL={isRTL}
onHandleDisableCam={() => setIsCamDisabled((value) => !value)}
/>
<UserStatus
voiceUser={voiceUser}
/>
</Styled.BottomBar>
</Styled.WebcamConnecting>
);
const renderWebcamConnectingSqueezed = () => (
<Styled.WebcamConnecting
data-test="webcamConnectingSqueezed"
animations={animations}
>
<UserAvatarVideo
user={user}
unhealthyStream={videoDataLoaded && !isStreamHealthy}
squeezed
/>
{renderSqueezedButton()}
</Styled.WebcamConnecting>
);
const renderDefaultButtons = () => (
<>
<Styled.TopBar>
<PinArea
user={user}
/>
<ViewActions
videoContainer={videoContainer}
name={name}
cameraId={cameraId}
isFullscreenContext={isFullscreenContext}
layoutContextDispatch={layoutContextDispatch}
/>
</Styled.TopBar>
<Styled.BottomBar>
<UserActions
name={name}
user={user}
cameraId={cameraId}
numOfStreams={numOfStreams}
onHandleVideoFocus={onHandleVideoFocus}
focused={focused}
onHandleMirror={() => setIsMirrored((value) => !value)}
isRTL={isRTL}
onHandleDisableCam={() => setIsCamDisabled((value) => !value)}
/>
<UserStatus
voiceUser={voiceUser}
/>
</Styled.BottomBar>
</>
);
return (
<Styled.Content
ref={videoContainer}
talking={talking}
fullscreen={isFullscreenContext}
data-test={talking ? 'webcamItemTalkingUser' : 'webcamItem'}
animations={animations}
{...{
...makeDragOperations(user?.userId),
dragging,
draggingOver,
}}
>
<Styled.VideoContainer>
{isCamDisabled && (
<Styled.VideoDisabled> Self cam disabled </Styled.VideoDisabled>
)}
<Styled.Video
mirrored={isMirrored}
unhealthyStream={videoDataLoaded && !isStreamHealthy}
data-test={isMirrored ? 'mirroredVideoContainer' : 'videoContainer'}
ref={videoTag}
muted="muted"
autoPlay
playsInline
/>
</Styled.VideoContainer>
{/* eslint-disable-next-line no-nested-ternary */}
{(videoIsReady || isCamDisabled) && (
isVideoSqueezed ? renderSqueezedButton() : renderDefaultButtons()
)}
{!videoIsReady && !isCamDisabled && (
isVideoSqueezed ? renderWebcamConnectingSqueezed() : renderWebcamConnecting()
)}
</Styled.Content>
);
};
export default withDragAndDrop(injectIntl(VideoListItem));
VideoListItem.defaultProps = {
numOfStreams: 0,
};
VideoListItem.propTypes = {
cameraId: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
numOfStreams: PropTypes.number,
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
onHandleVideoFocus: PropTypes.func.isRequired,
onVideoItemMount: PropTypes.func.isRequired,
onVideoItemUnmount: PropTypes.func.isRequired,
isFullscreenContext: PropTypes.bool.isRequired,
layoutContextDispatch: PropTypes.func.isRequired,
user: PropTypes.shape({
pin: PropTypes.bool.isRequired,
userId: PropTypes.string.isRequired,
}).isRequired,
voiceUser: PropTypes.shape({
muted: PropTypes.bool.isRequired,
listenOnly: PropTypes.bool.isRequired,
talking: PropTypes.bool.isRequired,
joined: PropTypes.bool.isRequired,
}).isRequired,
focused: PropTypes.bool.isRequired,
};