|
@@ -0,0 +1,218 @@
|
|
1
|
+import React, { Component } from 'react';
|
|
2
|
+
|
|
3
|
+/**
|
|
4
|
+ * Component that renders a video element for a passed in video track.
|
|
5
|
+ *
|
|
6
|
+ * @extends Component
|
|
7
|
+ */
|
|
8
|
+class Video extends Component {
|
|
9
|
+ /**
|
|
10
|
+ * Default values for {@code Video} component's properties.
|
|
11
|
+ *
|
|
12
|
+ * @static
|
|
13
|
+ */
|
|
14
|
+ static defaultProps = {
|
|
15
|
+ className: '',
|
|
16
|
+
|
|
17
|
+ id: ''
|
|
18
|
+ };
|
|
19
|
+
|
|
20
|
+ /**
|
|
21
|
+ * {@code Video} component's property types.
|
|
22
|
+ *
|
|
23
|
+ * @static
|
|
24
|
+ */
|
|
25
|
+ static propTypes = {
|
|
26
|
+ /**
|
|
27
|
+ * CSS classes to add to the video element.
|
|
28
|
+ */
|
|
29
|
+ className: React.PropTypes.string,
|
|
30
|
+
|
|
31
|
+ /**
|
|
32
|
+ * The value of the id attribute of the video. Used by the torture tests
|
|
33
|
+ * to locate video elements.
|
|
34
|
+ */
|
|
35
|
+ id: React.PropTypes.string,
|
|
36
|
+
|
|
37
|
+ /**
|
|
38
|
+ * Optional callback to invoke once the video starts playing.
|
|
39
|
+ */
|
|
40
|
+ onVideoPlaying: React.PropTypes.func,
|
|
41
|
+
|
|
42
|
+ /**
|
|
43
|
+ * The JitsiLocalTrack to display.
|
|
44
|
+ */
|
|
45
|
+ videoTrack: React.PropTypes.object
|
|
46
|
+ };
|
|
47
|
+
|
|
48
|
+ /**
|
|
49
|
+ * Initializes a new {@code Video} instance.
|
|
50
|
+ *
|
|
51
|
+ * @param {Object} props - The read-only properties with which the new
|
|
52
|
+ * instance is to be initialized.
|
|
53
|
+ */
|
|
54
|
+ constructor(props) {
|
|
55
|
+ super(props);
|
|
56
|
+
|
|
57
|
+ /**
|
|
58
|
+ * The internal reference to the DOM/HTML element intended for
|
|
59
|
+ * displaying a video. This element may be an HTML video element or a
|
|
60
|
+ * temasys video object.
|
|
61
|
+ *
|
|
62
|
+ * @private
|
|
63
|
+ * @type {HTMLVideoElement|Object}
|
|
64
|
+ */
|
|
65
|
+ this._videoElement = null;
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ // Bind event handlers so they are only bound once for every instance.
|
|
69
|
+ this._onVideoPlaying = this._onVideoPlaying.bind(this);
|
|
70
|
+ this._setVideoElement = this._setVideoElement.bind(this);
|
|
71
|
+ }
|
|
72
|
+
|
|
73
|
+ /**
|
|
74
|
+ * Invokes the library for rendering the video on initial display. Sets the
|
|
75
|
+ * volume level to zero to ensure no sound plays.
|
|
76
|
+ *
|
|
77
|
+ * @inheritdoc
|
|
78
|
+ * @returns {void}
|
|
79
|
+ */
|
|
80
|
+ componentDidMount() {
|
|
81
|
+ // Add these attributes directly onto the video element so temasys can
|
|
82
|
+ // use them when converting the video to an object.
|
|
83
|
+ this._videoElement.volume = 0;
|
|
84
|
+ this._videoElement.onplaying = this._onVideoPlaying;
|
|
85
|
+
|
|
86
|
+ this._attachTrack(this.props.videoTrack);
|
|
87
|
+ }
|
|
88
|
+
|
|
89
|
+ /**
|
|
90
|
+ * Remove any existing associations between the current video track and the
|
|
91
|
+ * component's video element.
|
|
92
|
+ *
|
|
93
|
+ * @inheritdoc
|
|
94
|
+ * @returns {void}
|
|
95
|
+ */
|
|
96
|
+ componentWillUnmount() {
|
|
97
|
+ this._detachTrack(this.props.videoTrack);
|
|
98
|
+ }
|
|
99
|
+
|
|
100
|
+ /**
|
|
101
|
+ * Updates the video display only if a new track is added. This component's
|
|
102
|
+ * updating is blackboxed from React to prevent re-rendering of video
|
|
103
|
+ * element, as the lib uses track.attach(videoElement) instead. Also,
|
|
104
|
+ * re-rendering cannot be used with temasys, which replaces video elements
|
|
105
|
+ * with an object.
|
|
106
|
+ *
|
|
107
|
+ * @inheritdoc
|
|
108
|
+ * @returns {boolean} - False is always returned to blackbox this component.
|
|
109
|
+ * from React.
|
|
110
|
+ */
|
|
111
|
+ shouldComponentUpdate(nextProps) {
|
|
112
|
+ const currentJitsiTrack = this.props.videoTrack
|
|
113
|
+ && this.props.videoTrack.jitsiTrack;
|
|
114
|
+ const nextJitsiTrack = nextProps.videoTrack
|
|
115
|
+ && nextProps.videoTrack.jitsiTrack;
|
|
116
|
+
|
|
117
|
+ if (currentJitsiTrack !== nextJitsiTrack) {
|
|
118
|
+ this._detachTrack(this.props.videoTrack);
|
|
119
|
+ this._attachTrack(nextProps.videoTrack);
|
|
120
|
+ }
|
|
121
|
+
|
|
122
|
+ return false;
|
|
123
|
+ }
|
|
124
|
+
|
|
125
|
+ /**
|
|
126
|
+ * Renders the video element.
|
|
127
|
+ *
|
|
128
|
+ * @override
|
|
129
|
+ * @returns {ReactElement}
|
|
130
|
+ */
|
|
131
|
+ render() {
|
|
132
|
+ // The wrapping div is necessary because temasys will replace the video
|
|
133
|
+ // with an object but react will keep expecting the video element. The
|
|
134
|
+ // div gives a constant element for react to keep track of.
|
|
135
|
+ return (
|
|
136
|
+ <div>
|
|
137
|
+ <video
|
|
138
|
+ autoPlay = { true }
|
|
139
|
+ className = { this.props.className }
|
|
140
|
+ id = { this.props.id }
|
|
141
|
+ ref = { this._setVideoElement } />
|
|
142
|
+ </div>
|
|
143
|
+ );
|
|
144
|
+ }
|
|
145
|
+
|
|
146
|
+ /**
|
|
147
|
+ * Calls into the passed in track to associate the track with the
|
|
148
|
+ * component's video element and render video.
|
|
149
|
+ *
|
|
150
|
+ * @param {Object} videoTrack - The redux representation of the
|
|
151
|
+ * {@code JitsiLocalTrack}.
|
|
152
|
+ * @private
|
|
153
|
+ * @returns {void}
|
|
154
|
+ */
|
|
155
|
+ _attachTrack(videoTrack) {
|
|
156
|
+ if (!videoTrack || !videoTrack.jitsiTrack) {
|
|
157
|
+ return;
|
|
158
|
+ }
|
|
159
|
+
|
|
160
|
+ const updatedVideoElement
|
|
161
|
+ = videoTrack.jitsiTrack.attach(this._videoElement);
|
|
162
|
+
|
|
163
|
+ // Sets the instance variable for the video element again as the element
|
|
164
|
+ // maybe have been replaced with a new object by temasys.
|
|
165
|
+ this._setVideoElement(updatedVideoElement);
|
|
166
|
+ }
|
|
167
|
+
|
|
168
|
+ /**
|
|
169
|
+ * Removes the association to the component's video element from the passed
|
|
170
|
+ * in redux representation of jitsi video track to stop the track from
|
|
171
|
+ * rendering. With temasys, the video element must still be visible for
|
|
172
|
+ * detaching to complete.
|
|
173
|
+ *
|
|
174
|
+ * @param {Object} videoTrack - The redux representation of the
|
|
175
|
+ * {@code JitsiLocalTrack}.
|
|
176
|
+ * @private
|
|
177
|
+ * @returns {void}
|
|
178
|
+ */
|
|
179
|
+ _detachTrack(videoTrack) {
|
|
180
|
+ // Detach the video element from the track only if it has already
|
|
181
|
+ // been attached. This accounts for a special case with temasys
|
|
182
|
+ // where if detach is being called before attach, the video
|
|
183
|
+ // element is converted to Object without updating this
|
|
184
|
+ // component's reference to the video element.
|
|
185
|
+ if (this._videoElement
|
|
186
|
+ && videoTrack
|
|
187
|
+ && videoTrack.jitsiTrack
|
|
188
|
+ && videoTrack.jitsiTrack.containers.includes(this._videoElement)) {
|
|
189
|
+ videoTrack.jitsiTrack.detach(this._videoElement);
|
|
190
|
+ }
|
|
191
|
+ }
|
|
192
|
+
|
|
193
|
+ /**
|
|
194
|
+ * Invokes the onvideoplaying callback if defined.
|
|
195
|
+ *
|
|
196
|
+ * @private
|
|
197
|
+ * @returns {void}
|
|
198
|
+ */
|
|
199
|
+ _onVideoPlaying() {
|
|
200
|
+ if (this.props.onVideoPlaying) {
|
|
201
|
+ this.props.onVideoPlaying();
|
|
202
|
+ }
|
|
203
|
+ }
|
|
204
|
+
|
|
205
|
+ /**
|
|
206
|
+ * Sets an instance variable for the component's video element so it can be
|
|
207
|
+ * referenced later for attaching and detaching a JitsiLocalTrack.
|
|
208
|
+ *
|
|
209
|
+ * @param {Object} element - DOM element for the component's video display.
|
|
210
|
+ * @private
|
|
211
|
+ * @returns {void}
|
|
212
|
+ */
|
|
213
|
+ _setVideoElement(element) {
|
|
214
|
+ this._videoElement = element;
|
|
215
|
+ }
|
|
216
|
+}
|
|
217
|
+
|
|
218
|
+export default Video;
|