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