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.ts 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. import throttle from 'lodash/throttle';
  2. import { PureComponent } from 'react';
  3. import { IReduxState, IStore } from '../../../app/types';
  4. import { getCurrentConference } from '../../../base/conference/functions';
  5. import { IJitsiConference } from '../../../base/conference/reducer';
  6. import { getLocalParticipant } from '../../../base/participants/functions';
  7. import { setSharedVideoStatus } from '../../actions.any';
  8. import { PLAYBACK_STATUSES } from '../../constants';
  9. /**
  10. * Return true if the difference between the two times 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: number, previousTime: number) {
  18. return Math.abs(newTime - previousTime) > 5;
  19. }
  20. /**
  21. * The type of the React {@link Component} props of {@link AbstractVideoManager}.
  22. */
  23. export interface IProps {
  24. /**
  25. * The current conference.
  26. */
  27. _conference?: IJitsiConference;
  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: IStore['dispatch'];
  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. abstract class AbstractVideoManager<S=void> extends PureComponent<IProps, S> {
  72. throttledFireUpdateSharedVideoEvent: Function;
  73. /**
  74. * Initializes a new instance of AbstractVideoManager.
  75. *
  76. * @param {IProps} props - Component props.
  77. * @returns {void}
  78. */
  79. constructor(props: IProps) {
  80. super(props);
  81. this.throttledFireUpdateSharedVideoEvent = throttle(this.fireUpdateSharedVideoEvent.bind(this), 5000);
  82. }
  83. /**
  84. * Implements React Component's componentDidMount.
  85. *
  86. * @inheritdoc
  87. */
  88. componentDidMount() {
  89. this.processUpdatedProps();
  90. }
  91. /**
  92. * Implements React Component's componentDidUpdate.
  93. *
  94. * @inheritdoc
  95. */
  96. componentDidUpdate() {
  97. this.processUpdatedProps();
  98. }
  99. /**
  100. * Implements React Component's componentWillUnmount.
  101. *
  102. * @inheritdoc
  103. */
  104. componentWillUnmount() {
  105. if (this.dispose) {
  106. this.dispose();
  107. }
  108. }
  109. /**
  110. * Processes new properties.
  111. *
  112. * @returns {void}
  113. */
  114. async processUpdatedProps() {
  115. const { _status, _time, _isOwner } = this.props;
  116. if (_isOwner) {
  117. return;
  118. }
  119. const playerTime = await this.getTime();
  120. if (shouldSeekToPosition(_time, playerTime)) {
  121. this.seek(_time);
  122. }
  123. if (this.getPlaybackStatus() !== _status) {
  124. if (_status === PLAYBACK_STATUSES.PLAYING) {
  125. this.play();
  126. } else if (_status === PLAYBACK_STATUSES.PAUSED) {
  127. this.pause();
  128. }
  129. }
  130. }
  131. /**
  132. * Handle video playing.
  133. *
  134. * @returns {void}
  135. */
  136. onPlay() {
  137. this.fireUpdateSharedVideoEvent();
  138. }
  139. /**
  140. * Handle video paused.
  141. *
  142. * @returns {void}
  143. */
  144. onPause() {
  145. this.fireUpdateSharedVideoEvent();
  146. }
  147. /**
  148. * Dispatches an update action for the shared video.
  149. *
  150. * @returns {void}
  151. */
  152. async fireUpdateSharedVideoEvent() {
  153. const { _isOwner } = this.props;
  154. if (!_isOwner) {
  155. return;
  156. }
  157. const status = this.getPlaybackStatus();
  158. if (!Object.values(PLAYBACK_STATUSES).includes(status)) {
  159. return;
  160. }
  161. const time = await this.getTime();
  162. const {
  163. _ownerId,
  164. _videoUrl,
  165. dispatch
  166. } = this.props;
  167. dispatch(setSharedVideoStatus({
  168. videoUrl: _videoUrl ?? '',
  169. status,
  170. time,
  171. ownerId: _ownerId
  172. }));
  173. }
  174. /**
  175. * Seeks video to provided time.
  176. */
  177. abstract seek(time: number): void;
  178. /**
  179. * Indicates the playback state of the video.
  180. */
  181. abstract getPlaybackStatus(): string;
  182. /**
  183. * Plays video.
  184. */
  185. abstract play(): void;
  186. /**
  187. * Pauses video.
  188. *
  189. * @returns {void}
  190. */
  191. abstract pause(): void;
  192. /**
  193. * Retrieves current time.
  194. */
  195. abstract getTime(): number;
  196. /**
  197. * Disposes current video player.
  198. *
  199. * @returns {void}
  200. */
  201. dispose() {
  202. // optional abstract method to be implemented by sub-class
  203. }
  204. }
  205. export default AbstractVideoManager;
  206. /**
  207. * Maps part of the Redux store to the props of this component.
  208. *
  209. * @param {Object} state - The Redux state.
  210. * @returns {IProps}
  211. */
  212. export function _mapStateToProps(state: IReduxState) {
  213. const { ownerId, status, time, videoUrl } = state['features/shared-video'];
  214. const localParticipant = getLocalParticipant(state);
  215. return {
  216. _conference: getCurrentConference(state),
  217. _isOwner: ownerId === localParticipant?.id,
  218. _ownerId: ownerId,
  219. _status: status,
  220. _time: Number(time),
  221. _videoUrl: videoUrl
  222. };
  223. }