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

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