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.

AbstractVideoManager.js 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* @flow */
  2. /* eslint-disable no-invalid-this */
  3. import throttle from 'lodash/throttle';
  4. import { PureComponent } from 'react';
  5. import { getCurrentConference } from '../../../base/conference';
  6. import { getLocalParticipant } from '../../../base/participants';
  7. import { setSharedVideoStatus } from '../../actions.any';
  8. import { PLAYBACK_STATUSES } from '../../constants';
  9. /**
  10. * Return true if the diffenrece between the two timees is larger than 5.
  11. *
  12. * @param {number} newTime - The current time.
  13. * @param {number} previousTime - The previous time.
  14. * @private
  15. * @returns {boolean}
  16. */
  17. function shouldSeekToPosition(newTime, previousTime) {
  18. return Math.abs(newTime - previousTime) > 5;
  19. }
  20. /**
  21. * The type of the React {@link Component} props of {@link AbstractVideoManager}.
  22. */
  23. export type Props = {
  24. /**
  25. * The current coference
  26. */
  27. _conference: Object,
  28. /**
  29. * Is the video shared by the local user.
  30. *
  31. * @private
  32. */
  33. _isOwner: boolean,
  34. /**
  35. * The shared video owner id
  36. */
  37. _ownerId: string,
  38. /**
  39. * The shared video status
  40. */
  41. _status: string,
  42. /**
  43. * Seek time in seconds.
  44. *
  45. */
  46. _time: number,
  47. /**
  48. * The video url
  49. */
  50. _videoUrl: string,
  51. /**
  52. * The Redux dispatch function.
  53. */
  54. dispatch: Function,
  55. /**
  56. * The player's height
  57. */
  58. height: number,
  59. /**
  60. * The video id
  61. */
  62. videoId: string,
  63. /**
  64. * The player's width
  65. */
  66. width: number
  67. }
  68. /**
  69. * Manager of shared video.
  70. */
  71. class AbstractVideoManager extends PureComponent<Props> {
  72. throttledFireUpdateSharedVideoEvent: Function;
  73. /**
  74. * Initializes a new instance of AbstractVideoManager.
  75. *
  76. * @returns {void}
  77. */
  78. constructor() {
  79. super();
  80. this.throttledFireUpdateSharedVideoEvent = throttle(this.fireUpdateSharedVideoEvent.bind(this), 5000);
  81. }
  82. /**
  83. * Implements React Component's componentDidMount.
  84. *
  85. * @inheritdoc
  86. */
  87. componentDidMount() {
  88. this.processUpdatedProps();
  89. }
  90. /**
  91. * Implements React Component's componentDidUpdate.
  92. *
  93. * @inheritdoc
  94. */
  95. componentDidUpdate() {
  96. this.processUpdatedProps();
  97. }
  98. /**
  99. * Implements React Component's componentWillUnmount.
  100. *
  101. * @inheritdoc
  102. */
  103. componentWillUnmount() {
  104. if (this.dispose) {
  105. this.dispose();
  106. }
  107. }
  108. /**
  109. * Processes new properties.
  110. *
  111. * @returns {void}
  112. */
  113. async processUpdatedProps() {
  114. const { _status, _time, _isOwner } = this.props;
  115. if (_isOwner) {
  116. return;
  117. }
  118. const playerTime = await this.getTime();
  119. if (shouldSeekToPosition(_time, playerTime)) {
  120. this.seek(_time);
  121. }
  122. if (this.getPlaybackStatus() !== _status) {
  123. if (_status === PLAYBACK_STATUSES.PLAYING) {
  124. this.play();
  125. } else if (_status === PLAYBACK_STATUSES.PAUSED) {
  126. this.pause();
  127. }
  128. }
  129. }
  130. /**
  131. * Handle video playing.
  132. *
  133. * @returns {void}
  134. */
  135. onPlay() {
  136. this.fireUpdateSharedVideoEvent();
  137. }
  138. /**
  139. * Handle video paused.
  140. *
  141. * @returns {void}
  142. */
  143. onPause() {
  144. this.fireUpdateSharedVideoEvent();
  145. }
  146. /**
  147. * Dispatches an update action for the shared video.
  148. *
  149. * @returns {void}
  150. */
  151. async fireUpdateSharedVideoEvent() {
  152. const { _isOwner } = this.props;
  153. if (!_isOwner) {
  154. return;
  155. }
  156. const status = this.getPlaybackStatus();
  157. if (!Object.values(PLAYBACK_STATUSES).includes(status)) {
  158. return;
  159. }
  160. const time = await this.getTime();
  161. const {
  162. _ownerId,
  163. _videoUrl,
  164. dispatch
  165. } = this.props;
  166. dispatch(setSharedVideoStatus({
  167. videoUrl: _videoUrl,
  168. status,
  169. time,
  170. ownerId: _ownerId
  171. }));
  172. }
  173. /**
  174. * Seeks video to provided time
  175. * @param {number} time
  176. */
  177. seek: (time: number) => void;
  178. /**
  179. * Indicates the playback state of the video
  180. */
  181. getPlaybackStatus: () => boolean;
  182. /**
  183. * Plays video
  184. */
  185. play: () => void;
  186. /**
  187. * Pauses video
  188. */
  189. pause: () => void;
  190. /**
  191. * Retrieves current time
  192. */
  193. getTime: () => number;
  194. /**
  195. * Disposes current video player
  196. */
  197. dispose: () => void;
  198. }
  199. export default AbstractVideoManager;
  200. /**
  201. * Maps part of the Redux store to the props of this component.
  202. *
  203. * @param {Object} state - The Redux state.
  204. * @returns {Props}
  205. */
  206. export function _mapStateToProps(state: Object): $Shape<Props> {
  207. const { ownerId, status, time, videoUrl } = state['features/shared-video'];
  208. const localParticipant = getLocalParticipant(state);
  209. return {
  210. _conference: getCurrentConference(state),
  211. _isOwner: ownerId === localParticipant.id,
  212. _ownerId: ownerId,
  213. _status: status,
  214. _time: time,
  215. _videoUrl: videoUrl
  216. };
  217. }