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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. videoTrack: PropTypes.object,
  21. waitForVideoStarted: PropTypes.bool,
  22. /**
  23. * The z-order of the Video of AbstractVideoTrack in the stacking space
  24. * of all Videos. For more details, refer to the zOrder property of the
  25. * Video class for React Native.
  26. */
  27. zOrder: PropTypes.number
  28. };
  29. /**
  30. * Initializes a new AbstractVideoTrack instance.
  31. *
  32. * @param {Object} props - The read-only properties with which the new
  33. * instance is to be initialized.
  34. */
  35. constructor(props) {
  36. super(props);
  37. this.state = {
  38. videoTrack: _falsy2null(props.videoTrack)
  39. };
  40. // Bind event handlers so they are only bound once for every instance.
  41. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  42. }
  43. /**
  44. * Implements React's {@link Component#componentWillReceiveProps()}.
  45. *
  46. * @inheritdoc
  47. * @param {Object} nextProps - The read-only props which this Component will
  48. * receive.
  49. * @returns {void}
  50. */
  51. componentWillReceiveProps(nextProps) {
  52. const oldValue = this.state.videoTrack;
  53. const newValue = _falsy2null(nextProps.videoTrack);
  54. if (oldValue !== newValue) {
  55. this._setVideoTrack(newValue);
  56. }
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. const videoTrack = this.state.videoTrack;
  66. let render;
  67. if (this.props.waitForVideoStarted) {
  68. // That's the complex case: we have to wait for onPlaying before we
  69. // render videoTrack. The complexity comes from the fact that
  70. // onPlaying will come after we render videoTrack.
  71. if (shouldRenderVideoTrack(videoTrack, true)) {
  72. // It appears that onPlaying has come for videoTrack already.
  73. // Most probably, another render has already passed through the
  74. // else clause bellow already.
  75. render = true;
  76. } else if (shouldRenderVideoTrack(videoTrack, false)
  77. && !videoTrack.videoStarted) {
  78. // XXX Unfortunately, onPlaying has not come for videoTrack yet.
  79. // We have to render in order to give onPlaying a chance to
  80. // come.
  81. render = true;
  82. }
  83. } else {
  84. // That's the simple case: we don't have to wait for onPlaying
  85. // before we render videoTrack
  86. render = shouldRenderVideoTrack(videoTrack, false);
  87. }
  88. const stream
  89. = render ? videoTrack.jitsiTrack.getOriginalStream() : null;
  90. return (
  91. <Video
  92. mirror = { videoTrack && videoTrack.mirror }
  93. onPlaying = { this._onVideoPlaying }
  94. stream = { stream }
  95. zOrder = { this.props.zOrder } />
  96. );
  97. }
  98. /**
  99. * Handler for case when video starts to play.
  100. *
  101. * @private
  102. * @returns {void}
  103. */
  104. _onVideoPlaying() {
  105. const videoTrack = this.props.videoTrack;
  106. if (videoTrack
  107. && !videoTrack.videoStarted) {
  108. this.props.dispatch(trackVideoStarted(videoTrack.jitsiTrack));
  109. }
  110. }
  111. /**
  112. * Sets a specific video track to be rendered by this instance.
  113. *
  114. * @param {Track} videoTrack - The video track to be rendered by this
  115. * instance.
  116. * @protected
  117. * @returns {void}
  118. */
  119. _setVideoTrack(videoTrack) {
  120. this.setState({ videoTrack });
  121. }
  122. }
  123. /**
  124. * Returns null if a specific value is falsy; otherwise, returns the specified
  125. * value.
  126. *
  127. * @param {*} value - The value to return if it is not falsy.
  128. * @returns {*} If the specified value is falsy, null; otherwise, the specified
  129. * value.
  130. */
  131. function _falsy2null(value) {
  132. return value || null;
  133. }