|
@@ -1,7 +1,6 @@
|
1
|
|
-import PropTypes from 'prop-types';
|
2
|
|
-import React, { Component } from 'react';
|
|
1
|
+// @flow
|
3
|
2
|
|
4
|
|
-import { Video } from '../../base/media';
|
|
3
|
+import React, { Component } from 'react';
|
5
|
4
|
|
6
|
5
|
/**
|
7
|
6
|
* Constants to describe the dimensions of the video. Landscape videos
|
|
@@ -23,66 +22,125 @@ export const ORIENTATION = {
|
23
|
22
|
* @private
|
24
|
23
|
* @type {Object}
|
25
|
24
|
*/
|
26
|
|
-export const ORIENTATION_TO_CLASS = {
|
|
25
|
+const ORIENTATION_TO_CLASS = {
|
27
|
26
|
[ORIENTATION.LANDSCAPE]: 'fit-full-width',
|
28
|
27
|
[ORIENTATION.PORTRAIT]: 'fit-full-height'
|
29
|
28
|
};
|
30
|
29
|
|
|
30
|
+/**
|
|
31
|
+ * The type of the React {@code Component} props of
|
|
32
|
+ * {@link LargeVideoBackgroundCanvas}.
|
|
33
|
+ */
|
|
34
|
+type Props = {
|
|
35
|
+
|
|
36
|
+ /**
|
|
37
|
+ * Additional CSS class names to add to the root of the component.
|
|
38
|
+ */
|
|
39
|
+ className: String,
|
|
40
|
+
|
|
41
|
+ /**
|
|
42
|
+ * Whether or not the background should have its visibility hidden.
|
|
43
|
+ */
|
|
44
|
+ hidden: boolean,
|
|
45
|
+
|
|
46
|
+ /**
|
|
47
|
+ * Whether or not the video should display flipped horizontally, so left
|
|
48
|
+ * becomes right and right becomes left.
|
|
49
|
+ */
|
|
50
|
+ mirror: boolean,
|
|
51
|
+
|
|
52
|
+ /**
|
|
53
|
+ * Whether the component should ensure full width of the video is displayed
|
|
54
|
+ * (landscape) or full height (portrait).
|
|
55
|
+ */
|
|
56
|
+ orientationFit: string,
|
|
57
|
+
|
|
58
|
+ /**
|
|
59
|
+ * Whether or not to display a filter on the video to visually indicate a
|
|
60
|
+ * problem with the video being displayed.
|
|
61
|
+ */
|
|
62
|
+ showLocalProblemFilter: boolean,
|
|
63
|
+
|
|
64
|
+ /**
|
|
65
|
+ * Whether or not to display a filter on the video to visually indicate a
|
|
66
|
+ * problem with the video being displayed.
|
|
67
|
+ */
|
|
68
|
+ showRemoteProblemFilter: boolean,
|
|
69
|
+
|
|
70
|
+ /**
|
|
71
|
+ * The video stream to display.
|
|
72
|
+ */
|
|
73
|
+ videoElement: Object
|
|
74
|
+};
|
|
75
|
+
|
|
76
|
+
|
31
|
77
|
/**
|
32
|
78
|
* Implements a React Component which shows a video element intended to be used
|
33
|
79
|
* as a background to fill the empty space of container with another video.
|
34
|
80
|
*
|
35
|
81
|
* @extends Component
|
36
|
82
|
*/
|
37
|
|
-export class LargeVideoBackground extends Component {
|
|
83
|
+export class LargeVideoBackground extends Component<Props> {
|
|
84
|
+ _canvasEl: Object;
|
|
85
|
+
|
|
86
|
+ _updateCanvasInterval: *;
|
|
87
|
+
|
|
88
|
+ /**
|
|
89
|
+ * Initializes new {@code LargeVideoBackground} instance.
|
|
90
|
+ *
|
|
91
|
+ * @param {*} props - The read-only properties with which the new instance
|
|
92
|
+ * is to be initialized.
|
|
93
|
+ */
|
|
94
|
+ constructor(props: Props) {
|
|
95
|
+ super(props);
|
|
96
|
+
|
|
97
|
+ // Bind event handlers so they are only bound once per instance.
|
|
98
|
+ this._setCanvasEl = this._setCanvasEl.bind(this);
|
|
99
|
+ this._updateCanvas = this._updateCanvas.bind(this);
|
|
100
|
+ }
|
|
101
|
+
|
|
102
|
+ /**
|
|
103
|
+ * If the canvas is not hidden, sets the initial interval to update the
|
|
104
|
+ * image displayed in the canvas.
|
|
105
|
+ *
|
|
106
|
+ * @inheritdoc
|
|
107
|
+ * @returns {void}
|
|
108
|
+ */
|
|
109
|
+ componentDidMount() {
|
|
110
|
+ if (this.props.videoElement && !this.props.hidden) {
|
|
111
|
+ this._updateCanvas();
|
|
112
|
+ this._setUpdateCanvasInterval();
|
|
113
|
+ }
|
|
114
|
+ }
|
|
115
|
+
|
38
|
116
|
/**
|
39
|
|
- * {@code LargeVideoBackground} component's property types.
|
|
117
|
+ * Starts or stops the interval to update the image displayed in the canvas.
|
40
|
118
|
*
|
41
|
|
- * @static
|
42
|
|
- */
|
43
|
|
- static propTypes = {
|
44
|
|
- /**
|
45
|
|
- * Additional CSS class names to add to the root of the component.
|
46
|
|
- */
|
47
|
|
- className: PropTypes.string,
|
48
|
|
-
|
49
|
|
- /**
|
50
|
|
- * Whether or not the background should have its visibility hidden.
|
51
|
|
- */
|
52
|
|
- hidden: PropTypes.bool,
|
53
|
|
-
|
54
|
|
- /**
|
55
|
|
- * Whether or not the video should display flipped horizontally, so
|
56
|
|
- * left becomes right and right becomes left.
|
57
|
|
- */
|
58
|
|
- mirror: PropTypes.bool,
|
59
|
|
-
|
60
|
|
- /**
|
61
|
|
- * Whether the component should ensure full width of the video is
|
62
|
|
- * displayed (landscape) or full height (portrait).
|
63
|
|
- */
|
64
|
|
- orientationFit: PropTypes.oneOf([
|
65
|
|
- ORIENTATION.LANDSCAPE,
|
66
|
|
- ORIENTATION.PORTRAIT
|
67
|
|
- ]),
|
68
|
|
-
|
69
|
|
- /**
|
70
|
|
- * Whether or not to display a filter on the video to visually indicate
|
71
|
|
- * a problem with the video being displayed.
|
72
|
|
- */
|
73
|
|
- showLocalProblemFilter: PropTypes.bool,
|
74
|
|
-
|
75
|
|
- /**
|
76
|
|
- * Whether or not to display a filter on the video to visually indicate
|
77
|
|
- * a problem with the video being displayed.
|
78
|
|
- */
|
79
|
|
- showRemoteProblemFilter: PropTypes.bool,
|
80
|
|
-
|
81
|
|
- /**
|
82
|
|
- * The video stream to display.
|
83
|
|
- */
|
84
|
|
- videoTrack: PropTypes.object
|
85
|
|
- };
|
|
119
|
+ * @inheritdoc
|
|
120
|
+ * @param {Object} nextProps - The read-only React {@code Component} props
|
|
121
|
+ * with which the new instance is to be initialized.
|
|
122
|
+ */
|
|
123
|
+ componentWillReceiveProps(nextProps: Props) {
|
|
124
|
+ if (this.props.hidden && !nextProps.hidden) {
|
|
125
|
+ this._clearCanvas();
|
|
126
|
+ this._setUpdateCanvasInterval();
|
|
127
|
+ }
|
|
128
|
+
|
|
129
|
+ if ((!this.props.hidden && nextProps.hidden)
|
|
130
|
+ || !nextProps.videoElement) {
|
|
131
|
+ this._clearCanvas();
|
|
132
|
+ this._clearUpdateCanvasInterval();
|
|
133
|
+ }
|
|
134
|
+ }
|
|
135
|
+
|
|
136
|
+ /**
|
|
137
|
+ * Clears the interval for updating the image displayed in the canvas.
|
|
138
|
+ *
|
|
139
|
+ * @inheritdoc
|
|
140
|
+ */
|
|
141
|
+ componentWillUnmount() {
|
|
142
|
+ this._clearUpdateCanvasInterval();
|
|
143
|
+ }
|
86
|
144
|
|
87
|
145
|
/**
|
88
|
146
|
* Implements React's {@link Component#render()}.
|
|
@@ -96,8 +154,7 @@ export class LargeVideoBackground extends Component {
|
96
|
154
|
mirror,
|
97
|
155
|
orientationFit,
|
98
|
156
|
showLocalProblemFilter,
|
99
|
|
- showRemoteProblemFilter,
|
100
|
|
- videoTrack
|
|
157
|
+ showRemoteProblemFilter
|
101
|
158
|
} = this.props;
|
102
|
159
|
const orientationClass = orientationFit
|
103
|
160
|
? ORIENTATION_TO_CLASS[orientationFit] : '';
|
|
@@ -105,17 +162,91 @@ export class LargeVideoBackground extends Component {
|
105
|
162
|
hidden ? 'invisible' : ''} ${orientationClass} ${
|
106
|
163
|
showLocalProblemFilter ? 'videoProblemFilter' : ''} ${
|
107
|
164
|
showRemoteProblemFilter ? 'remoteVideoProblemFilter' : ''}`;
|
108
|
|
- const videoTrackModel = {
|
109
|
|
- jitsiTrack: hidden ? null : videoTrack
|
110
|
|
- };
|
111
|
165
|
|
112
|
166
|
return (
|
113
|
167
|
<div className = { classNames }>
|
114
|
|
- <Video
|
115
|
|
- autoPlay = { true }
|
|
168
|
+ <canvas
|
116
|
169
|
id = 'largeVideoBackground'
|
117
|
|
- videoTrack = { videoTrackModel } />
|
|
170
|
+ ref = { this._setCanvasEl } />
|
118
|
171
|
</div>
|
119
|
172
|
);
|
120
|
173
|
}
|
|
174
|
+
|
|
175
|
+ /**
|
|
176
|
+ * Removes any image displayed on the canvas.
|
|
177
|
+ *
|
|
178
|
+ * @private
|
|
179
|
+ * @returns {void}
|
|
180
|
+ */
|
|
181
|
+ _clearCanvas() {
|
|
182
|
+ const cavnasContext = this._canvasEl.getContext('2d');
|
|
183
|
+
|
|
184
|
+ cavnasContext.clearRect(
|
|
185
|
+ 0, 0, this._canvasEl.width, this._canvasEl.height);
|
|
186
|
+ }
|
|
187
|
+
|
|
188
|
+ /**
|
|
189
|
+ * Clears the interval for updating the image displayed in the canvas.
|
|
190
|
+ *
|
|
191
|
+ * @private
|
|
192
|
+ * @returns {void}
|
|
193
|
+ */
|
|
194
|
+ _clearUpdateCanvasInterval() {
|
|
195
|
+ clearInterval(this._updateCanvasInterval);
|
|
196
|
+ }
|
|
197
|
+
|
|
198
|
+ _setCanvasEl: () => void;
|
|
199
|
+
|
|
200
|
+ /**
|
|
201
|
+ * Sets the instance variable for the component's canvas element so it can
|
|
202
|
+ * be accessed directly for drawing on.
|
|
203
|
+ *
|
|
204
|
+ * @param {Object} element - The DOM element for the component's canvas.
|
|
205
|
+ * @private
|
|
206
|
+ * @returns {void}
|
|
207
|
+ */
|
|
208
|
+ _setCanvasEl(element) {
|
|
209
|
+ this._canvasEl = element;
|
|
210
|
+ }
|
|
211
|
+
|
|
212
|
+ /**
|
|
213
|
+ * Starts the interval for updating the image displayed in the canvas.
|
|
214
|
+ *
|
|
215
|
+ * @private
|
|
216
|
+ * @returns {void}
|
|
217
|
+ */
|
|
218
|
+ _setUpdateCanvasInterval() {
|
|
219
|
+ this._clearUpdateCanvasInterval();
|
|
220
|
+ this._updateCanvasInterval = setInterval(this._updateCanvas, 200);
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ _updateCanvas: () => void;
|
|
224
|
+
|
|
225
|
+ /**
|
|
226
|
+ * Draws the current frame of the passed in video element onto the canvas.
|
|
227
|
+ *
|
|
228
|
+ * @private
|
|
229
|
+ * @returns {void}
|
|
230
|
+ */
|
|
231
|
+ _updateCanvas() {
|
|
232
|
+ const { videoElement } = this.props;
|
|
233
|
+ const { videoWidth, videoHeight } = videoElement;
|
|
234
|
+ const {
|
|
235
|
+ height: canvasHeight,
|
|
236
|
+ width: canvasWidth
|
|
237
|
+ } = this._canvasEl;
|
|
238
|
+ const cavnasContext = this._canvasEl.getContext('2d');
|
|
239
|
+
|
|
240
|
+ if (this.props.orientationFit === ORIENTATION.LANDSCAPE) {
|
|
241
|
+ const heightScaledToFit = (canvasWidth / videoWidth) * videoHeight;
|
|
242
|
+
|
|
243
|
+ cavnasContext.drawImage(
|
|
244
|
+ videoElement, 0, 0, canvasWidth, heightScaledToFit);
|
|
245
|
+ } else {
|
|
246
|
+ const widthScaledToFit = (canvasHeight / videoHeight) * videoWidth;
|
|
247
|
+
|
|
248
|
+ cavnasContext.drawImage(
|
|
249
|
+ videoElement, 0, 0, widthScaledToFit, canvasHeight);
|
|
250
|
+ }
|
|
251
|
+ }
|
121
|
252
|
}
|