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 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. _mounted: boolean;
  120. /**
  121. * Default values for {@code Video} component's properties.
  122. *
  123. * @static
  124. */
  125. static defaultProps = {
  126. className: '',
  127. autoPlay: true,
  128. id: '',
  129. playsinline: true
  130. };
  131. /**
  132. * Initializes a new {@code Video} instance.
  133. *
  134. * @param {Object} props - The read-only properties with which the new
  135. * instance is to be initialized.
  136. */
  137. constructor(props: Props) {
  138. super(props);
  139. /**
  140. * The internal reference to the DOM/HTML element intended for
  141. * displaying a video.
  142. *
  143. * @private
  144. * @type {HTMLVideoElement}
  145. */
  146. this._videoElement = null;
  147. // Bind event handlers so they are only bound once for every instance.
  148. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  149. this._setVideoElement = this._setVideoElement.bind(this);
  150. }
  151. /**
  152. * Invokes the library for rendering the video on initial display. Sets the
  153. * volume level to zero to ensure no sound plays.
  154. *
  155. * @inheritdoc
  156. * @returns {void}
  157. */
  158. componentDidMount() {
  159. this._mounted = true;
  160. if (this._videoElement) {
  161. this._videoElement.volume = 0;
  162. this._videoElement.onplaying = this._onVideoPlaying;
  163. }
  164. this._attachTrack(this.props.videoTrack);
  165. if (this._videoElement && this.props.autoPlay) {
  166. // Ensure the video gets play() called on it. This may be necessary in the
  167. // case where the local video container was moved and re-attached, in which
  168. // case video does not autoplay.
  169. this._videoElement.play()
  170. .catch(error => {
  171. // Prevent uncaught "DOMException: The play() request was interrupted by a new load request"
  172. // when video playback takes long to start and it starts after the component was unmounted.
  173. if (this._mounted) {
  174. throw error;
  175. }
  176. });
  177. }
  178. }
  179. /**
  180. * Remove any existing associations between the current video track and the
  181. * component's video element.
  182. *
  183. * @inheritdoc
  184. * @returns {void}
  185. */
  186. componentWillUnmount() {
  187. this._mounted = false;
  188. this._detachTrack(this.props.videoTrack);
  189. }
  190. /**
  191. * Updates the video display only if a new track is added. This component's
  192. * updating is blackboxed from React to prevent re-rendering of video
  193. * element, as the lib uses {@code track.attach(videoElement)} instead.
  194. *
  195. * @inheritdoc
  196. * @returns {boolean} - False is always returned to blackbox this component
  197. * from React.
  198. */
  199. shouldComponentUpdate(nextProps: Props) {
  200. const currentJitsiTrack = this.props.videoTrack
  201. && this.props.videoTrack.jitsiTrack;
  202. const nextJitsiTrack = nextProps.videoTrack
  203. && nextProps.videoTrack.jitsiTrack;
  204. if (currentJitsiTrack !== nextJitsiTrack) {
  205. this._detachTrack(this.props.videoTrack);
  206. this._attachTrack(nextProps.videoTrack);
  207. }
  208. if (this.props.style !== nextProps.style || this.props.className !== nextProps.className) {
  209. return true;
  210. }
  211. return false;
  212. }
  213. /**
  214. * Renders the video element.
  215. *
  216. * @override
  217. * @returns {ReactElement}
  218. */
  219. render() {
  220. const {
  221. autoPlay,
  222. className,
  223. id,
  224. muted,
  225. playsinline,
  226. style,
  227. eventHandlers
  228. } = this.props;
  229. return (
  230. <video
  231. autoPlay = { autoPlay }
  232. className = { className }
  233. id = { id }
  234. muted = { muted }
  235. playsInline = { playsinline }
  236. ref = { this._setVideoElement }
  237. style = { style }
  238. { ...eventHandlers } />
  239. );
  240. }
  241. /**
  242. * Calls into the passed in track to associate the track with the
  243. * component's video element and render video.
  244. *
  245. * @param {Object} videoTrack - The redux representation of the
  246. * {@code JitsiLocalTrack}.
  247. * @private
  248. * @returns {void}
  249. */
  250. _attachTrack(videoTrack) {
  251. if (!videoTrack || !videoTrack.jitsiTrack) {
  252. return;
  253. }
  254. videoTrack.jitsiTrack.attach(this._videoElement);
  255. }
  256. /**
  257. * Removes the association to the component's video element from the passed
  258. * in redux representation of jitsi video track to stop the track from
  259. * rendering.
  260. *
  261. * @param {Object} videoTrack - The redux representation of the
  262. * {@code JitsiLocalTrack}.
  263. * @private
  264. * @returns {void}
  265. */
  266. _detachTrack(videoTrack) {
  267. if (this._videoElement && videoTrack && videoTrack.jitsiTrack) {
  268. videoTrack.jitsiTrack.detach(this._videoElement);
  269. }
  270. }
  271. _onVideoPlaying: () => void;
  272. /**
  273. * Invokes the onvideoplaying callback if defined.
  274. *
  275. * @private
  276. * @returns {void}
  277. */
  278. _onVideoPlaying() {
  279. if (this.props.onVideoPlaying) {
  280. this.props.onVideoPlaying();
  281. }
  282. }
  283. _setVideoElement: () => void;
  284. /**
  285. * Sets an instance variable for the component's video element so it can be
  286. * referenced later for attaching and detaching a JitsiLocalTrack.
  287. *
  288. * @param {Object} element - DOM element for the component's video display.
  289. * @private
  290. * @returns {void}
  291. */
  292. _setVideoElement(element) {
  293. this._videoElement = element;
  294. }
  295. }
  296. export default Video;