您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

AbstractVideoTrack.js 5.1KB

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