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

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