You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Video.js 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. /**
  4. * The type of the React {@code Component} props of {@link Video}.
  5. */
  6. type Props = {
  7. /**
  8. * CSS classes to add to the video element.
  9. */
  10. className: string,
  11. /**
  12. * The value of the id attribute of the video. Used by the torture tests to
  13. * locate video elements.
  14. */
  15. id: string,
  16. /**
  17. * Optional callback to invoke once the video starts playing.
  18. */
  19. onVideoPlaying?: Function,
  20. /**
  21. * The JitsiLocalTrack to display.
  22. */
  23. videoTrack: ?Object,
  24. /**
  25. * Used to determine the value of the autoplay attribute of the underlying
  26. * video element.
  27. */
  28. autoPlay: boolean,
  29. /**
  30. * Used to determine the value of the autoplay attribute of the underlying
  31. * video element.
  32. */
  33. playsinline: boolean,
  34. /**
  35. * A map of the event handlers for the video HTML element.
  36. */
  37. eventHandlers?: {|
  38. /**
  39. * onAbort event handler.
  40. */
  41. onAbort?: ?Function,
  42. /**
  43. * onCanPlay event handler.
  44. */
  45. onCanPlay?: ?Function,
  46. /**
  47. * onCanPlayThrough event handler.
  48. */
  49. onCanPlayThrough?: ?Function,
  50. /**
  51. * onEmptied event handler.
  52. */
  53. onEmptied?: ?Function,
  54. /**
  55. * onEnded event handler.
  56. */
  57. onEnded?: ?Function,
  58. /**
  59. * onError event handler.
  60. */
  61. onError?: ?Function,
  62. /**
  63. * onLoadedData event handler.
  64. */
  65. onLoadedData?: ?Function,
  66. /**
  67. * onLoadedMetadata event handler.
  68. */
  69. onLoadedMetadata?: ?Function,
  70. /**
  71. * onLoadStart event handler.
  72. */
  73. onLoadStart?: ?Function,
  74. /**
  75. * onPause event handler.
  76. */
  77. onPause?: ?Function,
  78. /**
  79. * onPlay event handler.
  80. */
  81. onPlay?: ?Function,
  82. /**
  83. * onPlaying event handler.
  84. */
  85. onPlaying?: ?Function,
  86. /**
  87. * onRateChange event handler.
  88. */
  89. onRateChange?: ?Function,
  90. /**
  91. * onStalled event handler.
  92. */
  93. onStalled?: ?Function,
  94. /**
  95. * onSuspend event handler.
  96. */
  97. onSuspend?: ?Function,
  98. /**
  99. * onWaiting event handler.
  100. */
  101. onWaiting?: ?Function
  102. |},
  103. /**
  104. * A styles that will be applied on the video element.
  105. */
  106. style?: Object,
  107. /**
  108. * The value of the muted attribute for the underlying video element.
  109. */
  110. muted?: boolean
  111. };
  112. /**
  113. * Component that renders a video element for a passed in video track.
  114. *
  115. * @extends Component
  116. */
  117. class Video extends Component<Props> {
  118. _videoElement: ?Object;
  119. /**
  120. * Default values for {@code Video} component's properties.
  121. *
  122. * @static
  123. */
  124. static defaultProps = {
  125. className: '',
  126. autoPlay: true,
  127. id: '',
  128. playsinline: true
  129. };
  130. /**
  131. * Initializes a new {@code Video} instance.
  132. *
  133. * @param {Object} props - The read-only properties with which the new
  134. * instance is to be initialized.
  135. */
  136. constructor(props: Props) {
  137. super(props);
  138. /**
  139. * The internal reference to the DOM/HTML element intended for
  140. * displaying a video.
  141. *
  142. * @private
  143. * @type {HTMLVideoElement}
  144. */
  145. this._videoElement = null;
  146. // Bind event handlers so they are only bound once for every instance.
  147. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  148. this._setVideoElement = this._setVideoElement.bind(this);
  149. }
  150. /**
  151. * Invokes the library for rendering the video on initial display. Sets the
  152. * volume level to zero to ensure no sound plays.
  153. *
  154. * @inheritdoc
  155. * @returns {void}
  156. */
  157. componentDidMount() {
  158. if (this._videoElement) {
  159. this._videoElement.volume = 0;
  160. this._videoElement.onplaying = this._onVideoPlaying;
  161. }
  162. this._attachTrack(this.props.videoTrack);
  163. if (this._videoElement && this.props.autoPlay) {
  164. // Ensure the video gets play() called on it. This may be necessary in the
  165. // case where the local video container was moved and re-attached, in which
  166. // case video does not autoplay.
  167. this._videoElement.play();
  168. }
  169. }
  170. /**
  171. * Remove any existing associations between the current video track and the
  172. * component's video element.
  173. *
  174. * @inheritdoc
  175. * @returns {void}
  176. */
  177. componentWillUnmount() {
  178. this._detachTrack(this.props.videoTrack);
  179. }
  180. /**
  181. * Updates the video display only if a new track is added. This component's
  182. * updating is blackboxed from React to prevent re-rendering of video
  183. * element, as the lib uses {@code track.attach(videoElement)} instead.
  184. *
  185. * @inheritdoc
  186. * @returns {boolean} - False is always returned to blackbox this component
  187. * from React.
  188. */
  189. shouldComponentUpdate(nextProps: Props) {
  190. const currentJitsiTrack = this.props.videoTrack
  191. && this.props.videoTrack.jitsiTrack;
  192. const nextJitsiTrack = nextProps.videoTrack
  193. && nextProps.videoTrack.jitsiTrack;
  194. if (currentJitsiTrack !== nextJitsiTrack) {
  195. this._detachTrack(this.props.videoTrack);
  196. this._attachTrack(nextProps.videoTrack);
  197. }
  198. if (this.props.style !== nextProps.style || this.props.className !== nextProps.className) {
  199. return true;
  200. }
  201. return false;
  202. }
  203. /**
  204. * Renders the video element.
  205. *
  206. * @override
  207. * @returns {ReactElement}
  208. */
  209. render() {
  210. const {
  211. autoPlay,
  212. className,
  213. id,
  214. muted,
  215. playsinline,
  216. style,
  217. eventHandlers
  218. } = this.props;
  219. return (
  220. <video
  221. autoPlay = { autoPlay }
  222. className = { className }
  223. id = { id }
  224. muted = { muted }
  225. playsInline = { playsinline }
  226. ref = { this._setVideoElement }
  227. style = { style }
  228. { ...eventHandlers } />
  229. );
  230. }
  231. /**
  232. * Calls into the passed in track to associate the track with the
  233. * component's video element and render video.
  234. *
  235. * @param {Object} videoTrack - The redux representation of the
  236. * {@code JitsiLocalTrack}.
  237. * @private
  238. * @returns {void}
  239. */
  240. _attachTrack(videoTrack) {
  241. if (!videoTrack || !videoTrack.jitsiTrack) {
  242. return;
  243. }
  244. videoTrack.jitsiTrack.attach(this._videoElement);
  245. }
  246. /**
  247. * Removes the association to the component's video element from the passed
  248. * in redux representation of jitsi video track to stop the track from
  249. * rendering.
  250. *
  251. * @param {Object} videoTrack - The redux representation of the
  252. * {@code JitsiLocalTrack}.
  253. * @private
  254. * @returns {void}
  255. */
  256. _detachTrack(videoTrack) {
  257. if (this._videoElement && videoTrack && videoTrack.jitsiTrack) {
  258. videoTrack.jitsiTrack.detach(this._videoElement);
  259. }
  260. }
  261. _onVideoPlaying: () => void;
  262. /**
  263. * Invokes the onvideoplaying callback if defined.
  264. *
  265. * @private
  266. * @returns {void}
  267. */
  268. _onVideoPlaying() {
  269. if (this.props.onVideoPlaying) {
  270. this.props.onVideoPlaying();
  271. }
  272. }
  273. _setVideoElement: () => void;
  274. /**
  275. * Sets an instance variable for the component's video element so it can be
  276. * referenced later for attaching and detaching a JitsiLocalTrack.
  277. *
  278. * @param {Object} element - DOM element for the component's video display.
  279. * @private
  280. * @returns {void}
  281. */
  282. _setVideoElement(element) {
  283. this._videoElement = element;
  284. }
  285. }
  286. export default Video;