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 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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(() => {
  81. console.log('Animation was stopped');
  82. });
  83. }
  84. /**
  85. * Animates the display of the state videoTrack.
  86. *
  87. * @param {number} toValue - The value to which the specified animatedValue
  88. * is to be animated.
  89. * @private
  90. * @returns {Promise}
  91. */
  92. _animateVideoTrack(toValue) {
  93. return new Promise((resolve, reject) => {
  94. this._animation
  95. = Animated.timing(this.state.fade, { toValue });
  96. this._animation.start(result => {
  97. this._animation = null;
  98. result.finished ? resolve() : reject();
  99. });
  100. });
  101. }
  102. /**
  103. * Returns animation styles for Animated.View.
  104. *
  105. * @private
  106. * @returns {Object}
  107. */
  108. _getAnimationStyles() {
  109. return {
  110. opacity: this.state.fade
  111. };
  112. }
  113. // eslint-disable-next-line valid-jsdoc
  114. /**
  115. * Animate the setting of the video track to be rendered by this instance.
  116. *
  117. * @inheritdoc
  118. * @protected
  119. */
  120. _setVideoTrack(videoTrack) {
  121. // If JitsiTrack instance didn't change, that means some other track's
  122. // props were changed and we don't need to animate.
  123. const oldValue = this.state.videoTrack;
  124. const oldJitsiTrack = oldValue ? oldValue.jitsiTrack : null;
  125. const newValue = videoTrack;
  126. const newJitsiTrack = newValue ? newValue.jitsiTrack : null;
  127. if (oldJitsiTrack === newJitsiTrack) {
  128. super._setVideoTrack(newValue);
  129. } else {
  130. this._animateSetVideoTrack(oldValue, newValue);
  131. }
  132. }
  133. }
  134. export default connect()(VideoTrack);