Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

AbstractVideoTrack.js 4.6KB

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