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

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