123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- /* @flow */
- /* eslint-disable no-invalid-this */
-
- import throttle from 'lodash/throttle';
- import { PureComponent } from 'react';
-
- import { getCurrentConference } from '../../../base/conference';
- import { getLocalParticipant } from '../../../base/participants';
- import { setSharedVideoStatus } from '../../actions.any';
- import { PLAYBACK_STATUSES } from '../../constants';
-
- /**
- * Return true if the diffenrece between the two timees is larger than 5.
- *
- * @param {number} newTime - The current time.
- * @param {number} previousTime - The previous time.
- * @private
- * @returns {boolean}
- */
- function shouldSeekToPosition(newTime, previousTime) {
- return Math.abs(newTime - previousTime) > 5;
- }
-
- /**
- * The type of the React {@link Component} props of {@link AbstractVideoManager}.
- */
- export type Props = {
-
- /**
- * The current coference
- */
- _conference: Object,
-
- /**
- * Is the video shared by the local user.
- *
- * @private
- */
- _isOwner: boolean,
-
- /**
- * The shared video owner id
- */
- _ownerId: string,
-
- /**
- * The shared video status
- */
- _status: string,
-
- /**
- * Seek time in seconds.
- *
- */
- _time: number,
-
- /**
- * The video url
- */
- _videoUrl: string,
-
- /**
- * The Redux dispatch function.
- */
- dispatch: Function,
-
- /**
- * The player's height
- */
- height: number,
-
- /**
- * The video id
- */
- videoId: string,
-
- /**
- * The player's width
- */
- width: number
- }
-
- /**
- * Manager of shared video.
- */
- class AbstractVideoManager extends PureComponent<Props> {
- throttledFireUpdateSharedVideoEvent: Function;
-
- /**
- * Initializes a new instance of AbstractVideoManager.
- *
- * @returns {void}
- */
- constructor() {
- super();
-
- this.throttledFireUpdateSharedVideoEvent = throttle(this.fireUpdateSharedVideoEvent.bind(this), 5000);
- }
-
- /**
- * Implements React Component's componentDidMount.
- *
- * @inheritdoc
- */
- componentDidMount() {
- this.processUpdatedProps();
- }
-
- /**
- * Implements React Component's componentDidUpdate.
- *
- * @inheritdoc
- */
- componentDidUpdate() {
- this.processUpdatedProps();
- }
-
- /**
- * Implements React Component's componentWillUnmount.
- *
- * @inheritdoc
- */
- componentWillUnmount() {
- if (this.dispose) {
- this.dispose();
- }
- }
-
- /**
- * Processes new properties.
- *
- * @returns {void}
- */
- async processUpdatedProps() {
- const { _status, _time, _isOwner } = this.props;
-
- if (_isOwner) {
- return;
- }
-
- const playerTime = await this.getTime();
-
- if (shouldSeekToPosition(_time, playerTime)) {
- this.seek(_time);
- }
-
- if (this.getPlaybackStatus() !== _status) {
- if (_status === PLAYBACK_STATUSES.PLAYING) {
- this.play();
- } else if (_status === PLAYBACK_STATUSES.PAUSED) {
- this.pause();
- }
- }
- }
-
- /**
- * Handle video playing.
- *
- * @returns {void}
- */
- onPlay() {
- this.fireUpdateSharedVideoEvent();
- }
-
- /**
- * Handle video paused.
- *
- * @returns {void}
- */
- onPause() {
- this.fireUpdateSharedVideoEvent();
- }
-
- /**
- * Dispatches an update action for the shared video.
- *
- * @returns {void}
- */
- async fireUpdateSharedVideoEvent() {
- const { _isOwner } = this.props;
-
- if (!_isOwner) {
- return;
- }
-
- const status = this.getPlaybackStatus();
-
- if (!Object.values(PLAYBACK_STATUSES).includes(status)) {
- return;
- }
-
- const time = await this.getTime();
-
- const {
- _ownerId,
- _videoUrl,
- dispatch
- } = this.props;
-
- dispatch(setSharedVideoStatus({
- videoUrl: _videoUrl,
- status,
- time,
- ownerId: _ownerId
- }));
- }
-
- /**
- * Seeks video to provided time
- * @param {number} time
- */
- seek: (time: number) => void;
-
- /**
- * Indicates the playback state of the video
- */
- getPlaybackStatus: () => boolean;
-
- /**
- * Plays video
- */
- play: () => void;
-
- /**
- * Pauses video
- */
- pause: () => void;
-
- /**
- * Retrieves current time
- */
- getTime: () => number;
-
- /**
- * Disposes current video player
- */
- dispose: () => void;
- }
-
-
- export default AbstractVideoManager;
-
- /**
- * Maps part of the Redux store to the props of this component.
- *
- * @param {Object} state - The Redux state.
- * @returns {Props}
- */
- export function _mapStateToProps(state: Object): $Shape<Props> {
- const { ownerId, status, time, videoUrl } = state['features/shared-video'];
- const localParticipant = getLocalParticipant(state);
-
- return {
- _conference: getCurrentConference(state),
- _isOwner: ownerId === localParticipant.id,
- _ownerId: ownerId,
- _status: status,
- _time: time,
- _videoUrl: videoUrl
- };
- }
|