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.

AbstractVideoTrack.js 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { trackVideoStarted } from '../../tracks';
  4. import { shouldRenderVideoTrack } from '../functions';
  5. import { Video } from './_';
  6. /**
  7. * The type of the React {@code Component} props of {@link AbstractVideoTrack}.
  8. */
  9. export type Props = {
  10. /**
  11. * The Redux dispatch function.
  12. */
  13. dispatch: Dispatch<*>,
  14. /**
  15. * Callback to invoke when the {@link Video} of {@code AbstractVideoTrack}
  16. * is clicked/pressed.
  17. */
  18. onPress?: Function,
  19. /**
  20. * The Redux representation of the participant's video track.
  21. */
  22. videoTrack?: Object,
  23. /**
  24. * Whether or not video should be rendered after knowing video playback has
  25. * started.
  26. */
  27. waitForVideoStarted?: boolean,
  28. /**
  29. * The z-order of the Video of AbstractVideoTrack in the stacking space of
  30. * all Videos. For more details, refer to the zOrder property of the Video
  31. * class for React Native.
  32. */
  33. zOrder?: number,
  34. /**
  35. * Indicates whether zooming (pinch to zoom and/or drag) is enabled.
  36. */
  37. zoomEnabled?: boolean
  38. };
  39. /**
  40. * The type of the React {@code Component} state of {@link AbstractVideoTrack}.
  41. */
  42. type State = {
  43. /**
  44. * The Redux representation of the participant's video track.
  45. */
  46. videoTrack: Object | null
  47. };
  48. /**
  49. * Implements a React {@link Component} that renders video element for a
  50. * specific video track.
  51. *
  52. * @abstract
  53. */
  54. export default class AbstractVideoTrack<P: Props> extends Component<P, State> {
  55. /**
  56. * Initializes a new AbstractVideoTrack instance.
  57. *
  58. * @param {Object} props - The read-only properties with which the new
  59. * instance is to be initialized.
  60. */
  61. constructor(props: P) {
  62. super(props);
  63. this.state = {
  64. videoTrack: _falsy2null(props.videoTrack)
  65. };
  66. // Bind event handlers so they are only bound once for every instance.
  67. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  68. }
  69. /**
  70. * Implements React's {@link Component#componentWillReceiveProps()}.
  71. *
  72. * @inheritdoc
  73. * @param {Object} nextProps - The read-only props which this Component will
  74. * receive.
  75. * @returns {void}
  76. */
  77. componentWillReceiveProps(nextProps: P) {
  78. const oldValue = this.state.videoTrack;
  79. const newValue = _falsy2null(nextProps.videoTrack);
  80. if (oldValue !== newValue) {
  81. this._setVideoTrack(newValue);
  82. }
  83. }
  84. /**
  85. * Implements React's {@link Component#render()}.
  86. *
  87. * @inheritdoc
  88. * @returns {ReactElement}
  89. */
  90. render() {
  91. const { videoTrack } = this.state;
  92. let render;
  93. if (this.props.waitForVideoStarted && videoTrack) {
  94. // That's the complex case: we have to wait for onPlaying before we
  95. // render videoTrack. The complexity comes from the fact that
  96. // onPlaying will come after we render videoTrack.
  97. if (shouldRenderVideoTrack(videoTrack, true)) {
  98. // It appears that onPlaying has come for videoTrack already.
  99. // Most probably, another render has already passed through the
  100. // else clause bellow already.
  101. render = true;
  102. } else if (shouldRenderVideoTrack(videoTrack, false)
  103. && !videoTrack.videoStarted) {
  104. // XXX Unfortunately, onPlaying has not come for videoTrack yet.
  105. // We have to render in order to give onPlaying a chance to
  106. // come.
  107. render = true;
  108. }
  109. } else {
  110. // That's the simple case: we don't have to wait for onPlaying
  111. // before we render videoTrack
  112. render = shouldRenderVideoTrack(videoTrack, false);
  113. }
  114. const stream = render && videoTrack
  115. ? videoTrack.jitsiTrack.getOriginalStream() : null;
  116. // Actual zoom is currently only enabled if the stream is a desktop
  117. // stream.
  118. const zoomEnabled
  119. = this.props.zoomEnabled
  120. && stream
  121. && videoTrack
  122. && videoTrack.videoType === 'desktop';
  123. return (
  124. <Video
  125. mirror = { videoTrack && videoTrack.mirror }
  126. onPlaying = { this._onVideoPlaying }
  127. onPress = { this.props.onPress }
  128. stream = { stream }
  129. zOrder = { this.props.zOrder }
  130. zoomEnabled = { zoomEnabled } />
  131. );
  132. }
  133. _onVideoPlaying: () => void;
  134. /**
  135. * Handler for case when video starts to play.
  136. *
  137. * @private
  138. * @returns {void}
  139. */
  140. _onVideoPlaying() {
  141. const { videoTrack } = this.props;
  142. if (videoTrack && !videoTrack.videoStarted) {
  143. this.props.dispatch(trackVideoStarted(videoTrack.jitsiTrack));
  144. }
  145. }
  146. /**
  147. * Sets a specific video track to be rendered by this instance.
  148. *
  149. * @param {Track} videoTrack - The video track to be rendered by this
  150. * instance.
  151. * @protected
  152. * @returns {void}
  153. */
  154. _setVideoTrack(videoTrack) {
  155. this.setState({ videoTrack });
  156. }
  157. }
  158. /**
  159. * Returns null if a specific value is falsy; otherwise, returns the specified
  160. * value.
  161. *
  162. * @param {*} value - The value to return if it is not falsy.
  163. * @returns {*} If the specified value is falsy, null; otherwise, the specified
  164. * value.
  165. */
  166. function _falsy2null(value) {
  167. return value || null;
  168. }