Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

AbstractVideoManager.ts 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /* eslint-disable no-invalid-this */
  2. // @ts-ignore
  3. import Logger from '@jitsi/logger';
  4. import throttle from 'lodash/throttle';
  5. import { PureComponent } from 'react';
  6. import { createSharedVideoEvent as createEvent } from '../../../analytics/AnalyticsEvents';
  7. import { sendAnalytics } from '../../../analytics/functions';
  8. import { IReduxState } from '../../../app/types';
  9. import { getCurrentConference } from '../../../base/conference/functions';
  10. import { MEDIA_TYPE } from '../../../base/media/constants';
  11. import { getLocalParticipant } from '../../../base/participants/functions';
  12. import { isLocalTrackMuted } from '../../../base/tracks/functions';
  13. import { showWarningNotification } from '../../../notifications/actions';
  14. import { NOTIFICATION_TIMEOUT_TYPE } from '../../../notifications/constants';
  15. import { dockToolbox } from '../../../toolbox/actions';
  16. import { muteLocal } from '../../../video-menu/actions.any';
  17. import { setSharedVideoStatus, stopSharedVideo } from '../../actions.any';
  18. import { PLAYBACK_STATUSES } from '../../constants';
  19. const logger = Logger.getLogger(__filename);
  20. /**
  21. * Return true if the difference between the two times is larger than 5.
  22. *
  23. * @param {number} newTime - The current time.
  24. * @param {number} previousTime - The previous time.
  25. * @private
  26. * @returns {boolean}
  27. */
  28. function shouldSeekToPosition(newTime: number, previousTime: number) {
  29. return Math.abs(newTime - previousTime) > 5;
  30. }
  31. /**
  32. * The type of the React {@link PureComponent} props of {@link AbstractVideoManager}.
  33. */
  34. export interface IProps {
  35. /**
  36. * The current conference.
  37. */
  38. _conference: Object;
  39. /**
  40. * Warning that indicates an incorrect video url.
  41. */
  42. _displayWarning: Function;
  43. /**
  44. * Docks the toolbox.
  45. */
  46. _dockToolbox: Function;
  47. /**
  48. * Indicates whether the local audio is muted.
  49. */
  50. _isLocalAudioMuted: boolean;
  51. /**
  52. * Is the video shared by the local user.
  53. *
  54. * @private
  55. */
  56. _isOwner: boolean;
  57. /**
  58. * Mutes local audio track.
  59. */
  60. _muteLocal: Function;
  61. /**
  62. * Store flag for muted state.
  63. */
  64. _muted: boolean;
  65. /**
  66. * The shared video owner id.
  67. */
  68. _ownerId: string;
  69. /**
  70. * Updates the shared video status.
  71. */
  72. _setSharedVideoStatus: Function;
  73. /**
  74. * The shared video status.
  75. */
  76. _status: string;
  77. /**
  78. * Action to stop video sharing.
  79. */
  80. _stopSharedVideo: Function;
  81. /**
  82. * Seek time in seconds.
  83. *
  84. */
  85. _time: number;
  86. /**
  87. * The video url.
  88. */
  89. _videoUrl: string;
  90. /**
  91. * The video id.
  92. */
  93. videoId: string;
  94. }
  95. /**
  96. * Manager of shared video.
  97. */
  98. class AbstractVideoManager extends PureComponent<IProps> {
  99. throttledFireUpdateSharedVideoEvent: Function;
  100. /**
  101. * Initializes a new instance of AbstractVideoManager.
  102. *
  103. * @param {IProps} props - Component props.
  104. * @returns {void}
  105. */
  106. constructor(props: IProps) {
  107. super(props);
  108. this.throttledFireUpdateSharedVideoEvent = throttle(this.fireUpdateSharedVideoEvent.bind(this), 5000);
  109. // selenium tests handler
  110. // @ts-ignore
  111. window._sharedVideoPlayer = this;
  112. }
  113. /**
  114. * Implements React Component's componentDidMount.
  115. *
  116. * @inheritdoc
  117. */
  118. componentDidMount() {
  119. this.props._dockToolbox(true);
  120. this.processUpdatedProps();
  121. }
  122. /**
  123. * Implements React Component's componentDidUpdate.
  124. *
  125. * @inheritdoc
  126. */
  127. componentDidUpdate(prevProps: IProps) {
  128. const { _videoUrl } = this.props;
  129. if (prevProps._videoUrl !== _videoUrl) {
  130. sendAnalytics(createEvent('started'));
  131. }
  132. this.processUpdatedProps();
  133. }
  134. /**
  135. * Implements React Component's componentWillUnmount.
  136. *
  137. * @inheritdoc
  138. */
  139. componentWillUnmount() {
  140. sendAnalytics(createEvent('stopped'));
  141. if (this.dispose) {
  142. this.dispose();
  143. }
  144. this.props._dockToolbox(false);
  145. }
  146. /**
  147. * Processes new properties.
  148. *
  149. * @returns {void}
  150. */
  151. processUpdatedProps() {
  152. const { _status, _time, _isOwner, _muted } = this.props;
  153. if (_isOwner) {
  154. return;
  155. }
  156. const playerTime = this.getTime();
  157. if (shouldSeekToPosition(_time, playerTime)) {
  158. this.seek(_time);
  159. }
  160. if (this.getPlaybackStatus() !== _status) {
  161. if (_status === PLAYBACK_STATUSES.PLAYING) {
  162. this.play();
  163. }
  164. if (_status === PLAYBACK_STATUSES.PAUSED) {
  165. this.pause();
  166. }
  167. }
  168. if (this.isMuted() !== _muted) {
  169. if (_muted) {
  170. this.mute();
  171. } else {
  172. this.unMute();
  173. }
  174. }
  175. }
  176. /**
  177. * Handle video error.
  178. *
  179. * @param {Object|undefined} e - The error returned by the API or none.
  180. * @returns {void}
  181. */
  182. onError(e: any) {
  183. logger.error('Error in the video player', e?.data,
  184. e?.data ? 'Check error code at https://developers.google.com/youtube/iframe_api_reference#onError' : '');
  185. this.props._stopSharedVideo();
  186. this.props._displayWarning();
  187. }
  188. /**
  189. * Handle video playing.
  190. *
  191. * @returns {void}
  192. */
  193. onPlay() {
  194. this.smartAudioMute();
  195. sendAnalytics(createEvent('play'));
  196. this.fireUpdateSharedVideoEvent();
  197. }
  198. /**
  199. * Handle video paused.
  200. *
  201. * @returns {void}
  202. */
  203. onPause() {
  204. sendAnalytics(createEvent('paused'));
  205. this.fireUpdateSharedVideoEvent();
  206. }
  207. /**
  208. * Handle volume changed.
  209. *
  210. * @returns {void}
  211. */
  212. onVolumeChange() {
  213. const volume = this.getVolume();
  214. const muted = this.isMuted();
  215. if (volume > 0 && !muted) {
  216. this.smartAudioMute();
  217. }
  218. sendAnalytics(createEvent(
  219. 'volume.changed',
  220. {
  221. volume,
  222. muted
  223. }));
  224. this.fireUpdatePlayingVideoEvent();
  225. }
  226. /**
  227. * Handle changes to the shared playing video.
  228. *
  229. * @returns {void}
  230. */
  231. fireUpdatePlayingVideoEvent() {
  232. if (this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING) {
  233. this.fireUpdateSharedVideoEvent();
  234. }
  235. }
  236. /**
  237. * Dispatches an update action for the shared video.
  238. *
  239. * @returns {void}
  240. */
  241. fireUpdateSharedVideoEvent() {
  242. const { _isOwner } = this.props;
  243. if (!_isOwner) {
  244. return;
  245. }
  246. const status = this.getPlaybackStatus();
  247. if (!Object.values(PLAYBACK_STATUSES).includes(status)) {
  248. return;
  249. }
  250. const {
  251. _ownerId,
  252. _setSharedVideoStatus,
  253. _videoUrl
  254. } = this.props;
  255. _setSharedVideoStatus({
  256. videoUrl: _videoUrl,
  257. status,
  258. time: this.getTime(),
  259. ownerId: _ownerId,
  260. muted: this.isMuted()
  261. });
  262. }
  263. /**
  264. * Indicates if the player volume is currently on. This will return true if
  265. * we have an available player, which is currently in a PLAYING state,
  266. * which isn't muted and has it's volume greater than 0.
  267. *
  268. * @returns {boolean} Indicating if the volume of the shared video is
  269. * currently on.
  270. */
  271. isSharedVideoVolumeOn() {
  272. return this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING
  273. && !this.isMuted()
  274. && this.getVolume() > 0;
  275. }
  276. /**
  277. * Smart mike mute. If the mike isn't currently muted and the shared video
  278. * volume is on we mute the mike.
  279. *
  280. * @returns {void}
  281. */
  282. smartAudioMute() {
  283. const { _isLocalAudioMuted, _muteLocal } = this.props;
  284. if (!_isLocalAudioMuted
  285. && this.isSharedVideoVolumeOn()) {
  286. sendAnalytics(createEvent('audio.muted'));
  287. _muteLocal(true);
  288. }
  289. }
  290. /**
  291. * Seeks video to provided time.
  292. *
  293. * @param {number} time
  294. */
  295. seek: (time: number) => void;
  296. /**
  297. * Indicates the playback state of the video.
  298. */
  299. getPlaybackStatus: () => string;
  300. /**
  301. * Indicates whether the video is muted.
  302. */
  303. isMuted: () => boolean;
  304. /**
  305. * Retrieves current volume.
  306. */
  307. getVolume: () => number;
  308. /**
  309. * Plays video.
  310. */
  311. play: () => void;
  312. /**
  313. * Pauses video.
  314. */
  315. pause: () => void;
  316. /**
  317. * Mutes video.
  318. */
  319. mute: () => void;
  320. /**
  321. * Unmutes video.
  322. */
  323. unMute: () => void;
  324. /**
  325. * Retrieves current time.
  326. */
  327. getTime: () => number;
  328. /**
  329. * Disposes current video player.
  330. */
  331. dispose: () => void;
  332. }
  333. export default AbstractVideoManager;
  334. /**
  335. * Maps part of the Redux store to the props of this component.
  336. *
  337. * @param {Object} state - The Redux state.
  338. * @returns {IProps}
  339. */
  340. export function _mapStateToProps(state: IReduxState) {
  341. const { ownerId, status, time, videoUrl, muted } = state['features/shared-video'];
  342. const localParticipant = getLocalParticipant(state);
  343. const _isLocalAudioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  344. return {
  345. _conference: getCurrentConference(state),
  346. _isLocalAudioMuted,
  347. _isOwner: ownerId === localParticipant?.id,
  348. _muted: muted,
  349. _ownerId: ownerId,
  350. _status: status,
  351. _time: time,
  352. _videoUrl: videoUrl
  353. };
  354. }
  355. /**
  356. * Maps part of the props of this component to Redux actions.
  357. *
  358. * @param {Function} dispatch - The Redux dispatch function.
  359. * @returns {IProps}
  360. */
  361. export function _mapDispatchToProps(dispatch: Function) {
  362. return {
  363. _displayWarning: () => {
  364. dispatch(showWarningNotification({
  365. titleKey: 'dialog.shareVideoLinkError'
  366. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  367. },
  368. _dockToolbox: (value: boolean) => {
  369. dispatch(dockToolbox(value));
  370. },
  371. _stopSharedVideo: () => {
  372. dispatch(stopSharedVideo());
  373. },
  374. _muteLocal: (value: boolean) => {
  375. dispatch(muteLocal(value, MEDIA_TYPE.AUDIO));
  376. },
  377. _setSharedVideoStatus: ({ videoUrl, status, time, ownerId, muted }: any) => {
  378. dispatch(setSharedVideoStatus({
  379. videoUrl,
  380. status,
  381. time,
  382. ownerId,
  383. muted
  384. }));
  385. }
  386. };
  387. }