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

AbstractVideoTrack.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import { trackVideoStarted } from '../../tracks';
  4. import { shouldRenderVideoTrack } from '../functions';
  5. import { Video } from './_';
  6. /**
  7. * The type of the React {@code Component} props of {@link AbstractVideoTrack}.
  8. */
  9. export type Props = {
  10. /**
  11. * The Redux dispatch function.
  12. */
  13. dispatch: Dispatch<*>,
  14. /**
  15. * Callback to invoke when the {@link Video} of {@code AbstractVideoTrack}
  16. * is clicked/pressed.
  17. */
  18. onPress?: Function,
  19. /**
  20. * The Redux representation of the participant's video track.
  21. */
  22. videoTrack?: Object,
  23. /**
  24. * Whether or not video should be rendered after knowing video playback has
  25. * started.
  26. */
  27. waitForVideoStarted?: boolean,
  28. /**
  29. * The z-order of the Video of AbstractVideoTrack in the stacking space of
  30. * all Videos. For more details, refer to the zOrder property of the Video
  31. * class for React Native.
  32. */
  33. zOrder?: number,
  34. /**
  35. * Indicates whether zooming (pinch to zoom and/or drag) is enabled.
  36. */
  37. zoomEnabled?: boolean
  38. };
  39. /**
  40. * Implements a React {@link Component} that renders video element for a
  41. * specific video track.
  42. *
  43. * @abstract
  44. */
  45. export default class AbstractVideoTrack<P: Props> extends Component<P> {
  46. /**
  47. * Initializes a new AbstractVideoTrack instance.
  48. *
  49. * @param {Object} props - The read-only properties with which the new
  50. * instance is to be initialized.
  51. */
  52. constructor(props: P) {
  53. super(props);
  54. // Bind event handlers so they are only bound once for every instance.
  55. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  56. }
  57. /**
  58. * Implements React's {@link Component#render()}.
  59. *
  60. * @inheritdoc
  61. * @returns {ReactElement}
  62. */
  63. render() {
  64. const videoTrack = _falsy2null(this.props.videoTrack);
  65. let render;
  66. if (this.props.waitForVideoStarted && videoTrack) {
  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 = render && videoTrack
  88. ? videoTrack.jitsiTrack.getOriginalStream() : null;
  89. // Actual zoom is currently only enabled if the stream is a desktop
  90. // stream.
  91. const zoomEnabled
  92. = this.props.zoomEnabled
  93. && stream
  94. && videoTrack
  95. && videoTrack.videoType === 'desktop';
  96. return (
  97. <Video
  98. mirror = { videoTrack && videoTrack.mirror }
  99. onPlaying = { this._onVideoPlaying }
  100. onPress = { this.props.onPress }
  101. stream = { stream }
  102. zOrder = { this.props.zOrder }
  103. zoomEnabled = { zoomEnabled } />
  104. );
  105. }
  106. _onVideoPlaying: () => void;
  107. /**
  108. * Handler for case when video starts to play.
  109. *
  110. * @private
  111. * @returns {void}
  112. */
  113. _onVideoPlaying() {
  114. const { videoTrack } = this.props;
  115. if (videoTrack && !videoTrack.videoStarted) {
  116. this.props.dispatch(trackVideoStarted(videoTrack.jitsiTrack));
  117. }
  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. }