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

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