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.0KB

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