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.

VideoTrack.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React from 'react';
  2. import { Animated, View } from 'react-native';
  3. import { connect } from 'react-redux';
  4. import AbstractVideoTrack from '../AbstractVideoTrack';
  5. import styles from './styles';
  6. /**
  7. * Component that renders video element for a specified video track.
  8. *
  9. * @extends AbstractVideoTrack
  10. */
  11. class VideoTrack extends AbstractVideoTrack {
  12. /**
  13. * VideoTrack component's property types.
  14. *
  15. * @static
  16. */
  17. static propTypes = AbstractVideoTrack.propTypes
  18. /**
  19. * Initializes a new VideoTrack instance.
  20. *
  21. * @param {Object} props - The read-only properties with which the new
  22. * instance is to be initialized.
  23. */
  24. constructor(props) {
  25. super(props);
  26. /**
  27. * Reference to currently running animation if any.
  28. *
  29. * @private
  30. */
  31. this._animation = null;
  32. /**
  33. * Extend Component's state with additional animation-related vars.
  34. *
  35. * @type {Object}
  36. */
  37. this.state = {
  38. ...this.state,
  39. fade: new Animated.Value(0)
  40. };
  41. }
  42. /**
  43. * Renders video element with animation.
  44. *
  45. * @override
  46. * @returns {ReactElement}
  47. */
  48. render() {
  49. const animatedStyles
  50. = [ styles.videoCover, this._getAnimationStyles() ];
  51. return (
  52. <View style = { styles.video } >
  53. { super.render() }
  54. <Animated.View style = { animatedStyles } />
  55. </View>
  56. );
  57. }
  58. /**
  59. * Animates setting a new video track to be rendered by this instance.
  60. *
  61. * @param {Track} oldValue - The old video track rendered by this instance.
  62. * @param {Track} newValue - The new video track to be rendered by this
  63. * instance.
  64. * @private
  65. * @returns {Promise}
  66. */
  67. _animateSetVideoTrack(oldValue, newValue) {
  68. // If we're in the middle of an animation and a new animation is about
  69. // to start, stop the previous one first.
  70. if (this._animation) {
  71. this._animation.stop();
  72. this._animation = null;
  73. this.state.fade.setValue(0);
  74. }
  75. return this._animateVideoTrack(1)
  76. .then(() => {
  77. super._setVideoTrack(newValue);
  78. return this._animateVideoTrack(0);
  79. })
  80. .catch(() => console.log('Animation was stopped'));
  81. }
  82. /**
  83. * Animates the display of the state videoTrack.
  84. *
  85. * @param {number} toValue - The value to which the specified animatedValue
  86. * is to be animated.
  87. * @private
  88. * @returns {Promise}
  89. */
  90. _animateVideoTrack(toValue) {
  91. return new Promise((resolve, reject) => {
  92. this._animation
  93. = Animated.timing(this.state.fade, { toValue });
  94. this._animation.start(result => {
  95. this._animation = null;
  96. result.finished ? resolve() : reject();
  97. });
  98. });
  99. }
  100. /**
  101. * Returns animation styles for Animated.View.
  102. *
  103. * @private
  104. * @returns {Object}
  105. */
  106. _getAnimationStyles() {
  107. return {
  108. opacity: this.state.fade
  109. };
  110. }
  111. // eslint-disable-next-line valid-jsdoc
  112. /**
  113. * Animate the setting of the video track to be rendered by this instance.
  114. *
  115. * @inheritdoc
  116. * @protected
  117. */
  118. _setVideoTrack(videoTrack) {
  119. // If JitsiTrack instance didn't change, that means some other track's
  120. // props were changed and we don't need to animate.
  121. const oldValue = this.state.videoTrack;
  122. const oldJitsiTrack = oldValue ? oldValue.jitsiTrack : null;
  123. const newValue = videoTrack;
  124. const newJitsiTrack = newValue ? newValue.jitsiTrack : null;
  125. if (oldJitsiTrack === newJitsiTrack) {
  126. super._setVideoTrack(newValue);
  127. } else {
  128. this._animateSetVideoTrack(oldValue, newValue);
  129. }
  130. }
  131. }
  132. export default connect()(VideoTrack);