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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. // @ts-expect-error
  2. import Logger from '@jitsi/logger';
  3. import throttle from 'lodash/throttle';
  4. import { PureComponent } from 'react';
  5. import { createSharedVideoEvent as createEvent } from '../../../analytics/AnalyticsEvents';
  6. import { sendAnalytics } from '../../../analytics/functions';
  7. import { IReduxState, IStore } from '../../../app/types';
  8. import { getCurrentConference } from '../../../base/conference/functions';
  9. import { IJitsiConference } from '../../../base/conference/reducer';
  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?: IJitsiConference;
  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. window._sharedVideoPlayer = this;
  111. }
  112. /**
  113. * Implements React Component's componentDidMount.
  114. *
  115. * @inheritdoc
  116. */
  117. componentDidMount() {
  118. this.props._dockToolbox(true);
  119. this.processUpdatedProps();
  120. }
  121. /**
  122. * Implements React Component's componentDidUpdate.
  123. *
  124. * @inheritdoc
  125. */
  126. componentDidUpdate(prevProps: IProps) {
  127. const { _videoUrl } = this.props;
  128. if (prevProps._videoUrl !== _videoUrl) {
  129. sendAnalytics(createEvent('started'));
  130. }
  131. this.processUpdatedProps();
  132. }
  133. /**
  134. * Implements React Component's componentWillUnmount.
  135. *
  136. * @inheritdoc
  137. */
  138. componentWillUnmount() {
  139. sendAnalytics(createEvent('stopped'));
  140. if (this.dispose) {
  141. this.dispose();
  142. }
  143. this.props._dockToolbox(false);
  144. }
  145. /**
  146. * Processes new properties.
  147. *
  148. * @returns {void}
  149. */
  150. processUpdatedProps() {
  151. const { _status, _time, _isOwner, _muted } = this.props;
  152. if (_isOwner) {
  153. return;
  154. }
  155. const playerTime = this.getTime();
  156. if (shouldSeekToPosition(Number(_time), Number(playerTime))) {
  157. this.seek(Number(_time));
  158. }
  159. if (this.getPlaybackStatus() !== _status) {
  160. if (_status === PLAYBACK_STATUSES.PLAYING) {
  161. this.play();
  162. }
  163. if (_status === PLAYBACK_STATUSES.PAUSED) {
  164. this.pause();
  165. }
  166. }
  167. if (this.isMuted() !== _muted) {
  168. if (_muted) {
  169. this.mute();
  170. } else {
  171. this.unMute();
  172. }
  173. }
  174. }
  175. /**
  176. * Handle video error.
  177. *
  178. * @param {Object|undefined} e - The error returned by the API or none.
  179. * @returns {void}
  180. */
  181. onError(e?: any) {
  182. logger.error('Error in the video player', e?.data,
  183. e?.data ? 'Check error code at https://developers.google.com/youtube/iframe_api_reference#onError' : '');
  184. this.props._stopSharedVideo();
  185. this.props._displayWarning();
  186. }
  187. /**
  188. * Handle video playing.
  189. *
  190. * @returns {void}
  191. */
  192. onPlay() {
  193. this.smartAudioMute();
  194. sendAnalytics(createEvent('play'));
  195. this.fireUpdateSharedVideoEvent();
  196. }
  197. /**
  198. * Handle video paused.
  199. *
  200. * @returns {void}
  201. */
  202. onPause() {
  203. sendAnalytics(createEvent('paused'));
  204. this.fireUpdateSharedVideoEvent();
  205. }
  206. /**
  207. * Handle volume changed.
  208. *
  209. * @returns {void}
  210. */
  211. onVolumeChange() {
  212. const volume = this.getVolume();
  213. const muted = this.isMuted();
  214. if (Number(volume) > 0 && !muted) {
  215. this.smartAudioMute();
  216. }
  217. sendAnalytics(createEvent(
  218. 'volume.changed',
  219. {
  220. volume,
  221. muted
  222. }));
  223. this.fireUpdatePlayingVideoEvent();
  224. }
  225. /**
  226. * Handle changes to the shared playing video.
  227. *
  228. * @returns {void}
  229. */
  230. fireUpdatePlayingVideoEvent() {
  231. if (this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING) {
  232. this.fireUpdateSharedVideoEvent();
  233. }
  234. }
  235. /**
  236. * Dispatches an update action for the shared video.
  237. *
  238. * @returns {void}
  239. */
  240. fireUpdateSharedVideoEvent() {
  241. const { _isOwner } = this.props;
  242. if (!_isOwner) {
  243. return;
  244. }
  245. const status = this.getPlaybackStatus();
  246. if (!Object.values(PLAYBACK_STATUSES).includes(status ?? '')) {
  247. return;
  248. }
  249. const {
  250. _ownerId,
  251. _setSharedVideoStatus,
  252. _videoUrl
  253. } = this.props;
  254. _setSharedVideoStatus({
  255. videoUrl: _videoUrl,
  256. status,
  257. time: this.getTime(),
  258. ownerId: _ownerId,
  259. muted: this.isMuted()
  260. });
  261. }
  262. /**
  263. * Indicates if the player volume is currently on. This will return true if
  264. * we have an available player, which is currently in a PLAYING state,
  265. * which isn't muted and has it's volume greater than 0.
  266. *
  267. * @returns {boolean} Indicating if the volume of the shared video is
  268. * currently on.
  269. */
  270. isSharedVideoVolumeOn() {
  271. return this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING
  272. && !this.isMuted()
  273. && Number(this.getVolume()) > 0;
  274. }
  275. /**
  276. * Smart mike mute. If the mike isn't currently muted and the shared video
  277. * volume is on we mute the mike.
  278. *
  279. * @returns {void}
  280. */
  281. smartAudioMute() {
  282. const { _isLocalAudioMuted, _muteLocal } = this.props;
  283. if (!_isLocalAudioMuted
  284. && this.isSharedVideoVolumeOn()) {
  285. sendAnalytics(createEvent('audio.muted'));
  286. _muteLocal(true);
  287. }
  288. }
  289. /**
  290. * Seeks video to provided time.
  291. *
  292. * @param {number} _time - Time to seek to.
  293. * @returns {void}
  294. */
  295. seek(_time: number) {
  296. // to be implemented by subclass
  297. }
  298. /**
  299. * Indicates the playback state of the video.
  300. *
  301. * @returns {string}
  302. */
  303. getPlaybackStatus(): string | undefined {
  304. return;
  305. }
  306. /**
  307. * Indicates whether the video is muted.
  308. *
  309. * @returns {boolean}
  310. */
  311. isMuted(): boolean | undefined {
  312. return;
  313. }
  314. /**
  315. * Retrieves current volume.
  316. *
  317. * @returns {number}
  318. */
  319. getVolume() {
  320. return 1;
  321. }
  322. /**
  323. * Plays video.
  324. *
  325. * @returns {void}
  326. */
  327. play() {
  328. // to be implemented by subclass
  329. }
  330. /**
  331. * Pauses video.
  332. *
  333. * @returns {void}
  334. */
  335. pause() {
  336. // to be implemented by subclass
  337. }
  338. /**
  339. * Mutes video.
  340. *
  341. * @returns {void}
  342. */
  343. mute() {
  344. // to be implemented by subclass
  345. }
  346. /**
  347. * Unmutes video.
  348. *
  349. * @returns {void}
  350. */
  351. unMute() {
  352. // to be implemented by subclass
  353. }
  354. /**
  355. * Retrieves current time.
  356. *
  357. * @returns {number}
  358. */
  359. getTime() {
  360. return 0;
  361. }
  362. /**
  363. * Disposes current video player.
  364. *
  365. * @returns {void}
  366. */
  367. dispose() {
  368. // to be implemented by subclass
  369. }
  370. }
  371. export default AbstractVideoManager;
  372. /**
  373. * Maps part of the Redux store to the props of this component.
  374. *
  375. * @param {Object} state - The Redux state.
  376. * @returns {IProps}
  377. */
  378. export function _mapStateToProps(state: IReduxState) {
  379. const { ownerId, status, time, videoUrl, muted } = state['features/shared-video'];
  380. const localParticipant = getLocalParticipant(state);
  381. const _isLocalAudioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  382. return {
  383. _conference: getCurrentConference(state),
  384. _isLocalAudioMuted,
  385. _isOwner: ownerId === localParticipant?.id,
  386. _muted: muted,
  387. _ownerId: ownerId,
  388. _status: status,
  389. _time: time,
  390. _videoUrl: videoUrl
  391. };
  392. }
  393. /**
  394. * Maps part of the props of this component to Redux actions.
  395. *
  396. * @param {Function} dispatch - The Redux dispatch function.
  397. * @returns {IProps}
  398. */
  399. export function _mapDispatchToProps(dispatch: IStore['dispatch']) {
  400. return {
  401. _displayWarning: () => {
  402. dispatch(showWarningNotification({
  403. titleKey: 'dialog.shareVideoLinkError'
  404. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  405. },
  406. _dockToolbox: (value: boolean) => {
  407. dispatch(dockToolbox(value));
  408. },
  409. _stopSharedVideo: () => {
  410. dispatch(stopSharedVideo());
  411. },
  412. _muteLocal: (value: boolean) => {
  413. dispatch(muteLocal(value, MEDIA_TYPE.AUDIO));
  414. },
  415. _setSharedVideoStatus: ({ videoUrl, status, time, ownerId, muted }: any) => {
  416. dispatch(setSharedVideoStatus({
  417. videoUrl,
  418. status,
  419. time,
  420. ownerId,
  421. muted
  422. }));
  423. }
  424. };
  425. }