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.

VideoManager.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import Logger from 'jitsi-meet-logger';
  2. import React from 'react';
  3. import Video from 'react-native-video';
  4. import { connect } from '../../../base/redux';
  5. import { PLAYBACK_STATUSES } from '../../constants';
  6. import AbstractVideoManager, {
  7. _mapStateToProps,
  8. Props
  9. } from './AbstractVideoManager';
  10. const logger = Logger.getLogger(__filename);
  11. /**
  12. * Manager of shared video.
  13. */
  14. class VideoManager extends AbstractVideoManager<Props> {
  15. /**
  16. * Initializes a new VideoManager instance.
  17. *
  18. * @param {Object} props - This component's props.
  19. *
  20. * @returns {void}
  21. */
  22. constructor(props) {
  23. super(props);
  24. this.state = {
  25. currentTime: 0,
  26. paused: false
  27. };
  28. this.playerRef = React.createRef();
  29. this.onPlaybackRateChange = this.onPlaybackRateChange.bind(this);
  30. this.onProgress = this.onProgress.bind(this);
  31. }
  32. /**
  33. * Retrieves the current player ref.
  34. */
  35. get player() {
  36. return this.playerRef.current;
  37. }
  38. /**
  39. * Indicates the playback state of the video.
  40. *
  41. * @returns {string}
  42. */
  43. getPlaybackStatus() {
  44. let status;
  45. if (this.state.paused) {
  46. status = PLAYBACK_STATUSES.PAUSED;
  47. } else {
  48. status = PLAYBACK_STATUSES.PLAYING;
  49. }
  50. return status;
  51. }
  52. /**
  53. * Retrieves current time.
  54. *
  55. * @returns {number}
  56. */
  57. getTime() {
  58. return this.state.currentTime;
  59. }
  60. /**
  61. * Seeks video to provided time.
  62. *
  63. * @param {number} time - The time to seek to.
  64. *
  65. * @returns {void}
  66. */
  67. seek(time) {
  68. if (this.player) {
  69. this.player.seek(time);
  70. }
  71. }
  72. /**
  73. * Plays video.
  74. *
  75. * @returns {void}
  76. */
  77. play() {
  78. this.setState({
  79. paused: false
  80. });
  81. }
  82. /**
  83. * Pauses video.
  84. *
  85. * @returns {void}
  86. */
  87. pause() {
  88. this.setState({
  89. paused: true
  90. });
  91. }
  92. /**
  93. * Handles playback rate changed event.
  94. *
  95. * @param {Object} options.playbackRate - Playback rate: 1 - playing, 0 - paused, other - slowed down / sped up.
  96. * @returns {void}
  97. */
  98. onPlaybackRateChange({ playbackRate }) {
  99. if (playbackRate === 0) {
  100. this.setState({
  101. paused: true
  102. }, () => {
  103. this.onPause();
  104. });
  105. }
  106. if (playbackRate === 1) {
  107. this.setState({
  108. paused: false
  109. }, () => {
  110. this.onPlay();
  111. });
  112. }
  113. }
  114. /**
  115. * Handles progress updarte event.
  116. *
  117. * @param {Object} options - Progress event options.
  118. * @returns {void}
  119. */
  120. onProgress(options) {
  121. this.setState({ currentTime: options.currentTime });
  122. this.throttledFireUpdateSharedVideoEvent();
  123. }
  124. /**
  125. * Retrieves video tag params.
  126. *
  127. * @returns {void}
  128. */
  129. getPlayerOptions() {
  130. const { _isOwner, videoId, width, height } = this.props;
  131. const { paused } = this.state;
  132. const options = {
  133. paused,
  134. progressUpdateInterval: 5000,
  135. resizeMode: 'cover',
  136. style: {
  137. height,
  138. width
  139. },
  140. source: { uri: videoId },
  141. controls: _isOwner,
  142. pictureInPicture: false,
  143. onProgress: this.onProgress,
  144. onError: event => {
  145. logger.error('Error in the player:', event);
  146. }
  147. };
  148. if (_isOwner) {
  149. options.onPlaybackRateChange = this.onPlaybackRateChange;
  150. }
  151. return options;
  152. }
  153. /**
  154. * Implements React Component's render.
  155. *
  156. * @inheritdoc
  157. */
  158. render() {
  159. return (<Video
  160. ref = { this.playerRef }
  161. { ...this.getPlayerOptions() } />);
  162. }
  163. }
  164. export default connect(_mapStateToProps)(VideoManager);