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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /* @flow */
  2. import React, { Component } from 'react';
  3. import type { Dispatch } from 'redux';
  4. import { trackVideoStarted } from '../../tracks';
  5. import { shouldRenderVideoTrack } from '../functions';
  6. import { Video } from './_';
  7. /**
  8. * The type of the React {@code Component} props of {@link AbstractVideoTrack}.
  9. */
  10. export type Props = {
  11. /**
  12. * The Redux dispatch function.
  13. */
  14. dispatch: Dispatch<any>,
  15. /**
  16. * Callback to invoke when the {@link Video} of {@code AbstractVideoTrack}
  17. * is clicked/pressed.
  18. */
  19. onPress?: Function,
  20. /**
  21. * The Redux representation of the participant's video track.
  22. */
  23. videoTrack?: Object,
  24. /**
  25. * Whether or not video should be rendered after knowing video playback has
  26. * started.
  27. */
  28. waitForVideoStarted?: boolean,
  29. /**
  30. * The z-order of the Video of AbstractVideoTrack in the stacking space of
  31. * all Videos. For more details, refer to the zOrder property of the Video
  32. * class for React Native.
  33. */
  34. zOrder?: number,
  35. /**
  36. * Indicates whether zooming (pinch to zoom and/or drag) is enabled.
  37. */
  38. zoomEnabled?: boolean
  39. };
  40. /**
  41. * Implements a React {@link Component} that renders video element for a
  42. * specific video track.
  43. *
  44. * @abstract
  45. */
  46. export default class AbstractVideoTrack<P: Props> extends Component<P> {
  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: P) {
  54. super(props);
  55. // Bind event handlers so they are only bound once for every instance.
  56. this._onVideoPlaying = this._onVideoPlaying.bind(this);
  57. }
  58. /**
  59. * Implements React's {@link Component#render()}.
  60. *
  61. * @inheritdoc
  62. * @returns {ReactElement}
  63. */
  64. render() {
  65. const videoTrack = _falsy2null(this.props.videoTrack);
  66. let render;
  67. if (this.props.waitForVideoStarted && videoTrack) {
  68. // That's the complex case: we have to wait for onPlaying before we
  69. // render videoTrack. The complexity comes from the fact that
  70. // onPlaying will come after we render videoTrack.
  71. if (shouldRenderVideoTrack(videoTrack, true)) {
  72. // It appears that onPlaying has come for videoTrack already.
  73. // Most probably, another render has already passed through the
  74. // else clause bellow already.
  75. render = true;
  76. } else if (shouldRenderVideoTrack(videoTrack, false)
  77. && !videoTrack.videoStarted) {
  78. // XXX Unfortunately, onPlaying has not come for videoTrack yet.
  79. // We have to render in order to give onPlaying a chance to
  80. // come.
  81. render = true;
  82. }
  83. } else {
  84. // That's the simple case: we don't have to wait for onPlaying
  85. // before we render videoTrack
  86. render = shouldRenderVideoTrack(videoTrack, false);
  87. }
  88. const stream = render && videoTrack
  89. ? videoTrack.jitsiTrack.getOriginalStream() : null;
  90. // Actual zoom is currently only enabled if the stream is a desktop
  91. // stream.
  92. const zoomEnabled
  93. = this.props.zoomEnabled
  94. && stream
  95. && videoTrack
  96. && videoTrack.videoType === 'desktop';
  97. return (
  98. <Video
  99. mirror = { videoTrack && videoTrack.mirror }
  100. onPlaying = { this._onVideoPlaying }
  101. onPress = { this.props.onPress }
  102. stream = { stream }
  103. zOrder = { this.props.zOrder }
  104. zoomEnabled = { zoomEnabled } />
  105. );
  106. }
  107. _onVideoPlaying: () => void;
  108. /**
  109. * Handler for case when video starts to play.
  110. *
  111. * @private
  112. * @returns {void}
  113. */
  114. _onVideoPlaying() {
  115. const { videoTrack } = this.props;
  116. if (videoTrack && !videoTrack.videoStarted) {
  117. this.props.dispatch(trackVideoStarted(videoTrack.jitsiTrack));
  118. }
  119. }
  120. }
  121. /**
  122. * Returns null if a specific value is falsy; otherwise, returns the specified
  123. * value.
  124. *
  125. * @param {*} value - The value to return if it is not falsy.
  126. * @returns {*} If the specified value is falsy, null; otherwise, the specified
  127. * value.
  128. */
  129. function _falsy2null(value) {
  130. return value || null;
  131. }