選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

AbstractVideoManager.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 { 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 { 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. * @returns {void}
  176. */
  177. onError() {
  178. logger.error('Error in the video player');
  179. this.props._stopSharedVideo();
  180. this.props._displayWarning();
  181. }
  182. /**
  183. * Handle video playing.
  184. *
  185. * @returns {void}
  186. */
  187. onPlay() {
  188. this.smartAudioMute();
  189. sendAnalytics(createEvent('play'));
  190. this.fireUpdateSharedVideoEvent();
  191. }
  192. /**
  193. * Handle video paused.
  194. *
  195. * @returns {void}
  196. */
  197. onPause() {
  198. sendAnalytics(createEvent('paused'));
  199. this.fireUpdateSharedVideoEvent();
  200. }
  201. /**
  202. * Handle volume changed.
  203. *
  204. * @returns {void}
  205. */
  206. onVolumeChange() {
  207. const volume = this.getVolume();
  208. const muted = this.isMuted();
  209. if (volume > 0 && !muted) {
  210. this.smartAudioMute();
  211. }
  212. sendAnalytics(createEvent(
  213. 'volume.changed',
  214. {
  215. volume,
  216. muted
  217. }));
  218. this.fireUpdatePlayingVideoEvent();
  219. }
  220. /**
  221. * Handle changes to the shared playing video.
  222. *
  223. * @returns {void}
  224. */
  225. fireUpdatePlayingVideoEvent() {
  226. if (this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING) {
  227. this.fireUpdateSharedVideoEvent();
  228. }
  229. }
  230. /**
  231. * Dispatches an update action for the shared video.
  232. *
  233. * @returns {void}
  234. */
  235. fireUpdateSharedVideoEvent() {
  236. const { _isOwner } = this.props;
  237. if (!_isOwner) {
  238. return;
  239. }
  240. const status = this.getPlaybackStatus();
  241. if (!Object.values(PLAYBACK_STATUSES).includes(status)) {
  242. return;
  243. }
  244. const {
  245. _ownerId,
  246. _setSharedVideoStatus,
  247. _videoUrl
  248. } = this.props;
  249. _setSharedVideoStatus({
  250. videoUrl: _videoUrl,
  251. status,
  252. time: this.getTime(),
  253. ownerId: _ownerId,
  254. muted: this.isMuted()
  255. });
  256. }
  257. /**
  258. * Indicates if the player volume is currently on. This will return true if
  259. * we have an available player, which is currently in a PLAYING state,
  260. * which isn't muted and has it's volume greater than 0.
  261. *
  262. * @returns {boolean} Indicating if the volume of the shared video is
  263. * currently on.
  264. */
  265. isSharedVideoVolumeOn() {
  266. return this.getPlaybackStatus() === PLAYBACK_STATUSES.PLAYING
  267. && !this.isMuted()
  268. && this.getVolume() > 0;
  269. }
  270. /**
  271. * Smart mike mute. If the mike isn't currently muted and the shared video
  272. * volume is on we mute the mike.
  273. *
  274. * @returns {void}
  275. */
  276. smartAudioMute() {
  277. const { _isLocalAudioMuted, _muteLocal } = this.props;
  278. if (!_isLocalAudioMuted
  279. && this.isSharedVideoVolumeOn()) {
  280. sendAnalytics(createEvent('audio.muted'));
  281. _muteLocal(true);
  282. }
  283. }
  284. /**
  285. * Seeks video to provided time.
  286. *
  287. * @param {number} time
  288. */
  289. seek: (time: number) => void;
  290. /**
  291. * Indicates the playback state of the video.
  292. */
  293. getPlaybackStatus: () => boolean;
  294. /**
  295. * Indicates whether the video is muted.
  296. */
  297. isMuted: () => boolean;
  298. /**
  299. * Retrieves current volume.
  300. */
  301. getVolume: () => number;
  302. /**
  303. * Plays video.
  304. */
  305. play: () => void;
  306. /**
  307. * Pauses video.
  308. */
  309. pause: () => void;
  310. /**
  311. * Mutes video.
  312. */
  313. mute: () => void;
  314. /**
  315. * Unmutes video.
  316. */
  317. unMute: () => void;
  318. /**
  319. * Retrieves current time.
  320. */
  321. getTime: () => number;
  322. /**
  323. * Disposes current video player.
  324. */
  325. dispose: () => void;
  326. }
  327. export default AbstractVideoManager;
  328. /**
  329. * Maps part of the Redux store to the props of this component.
  330. *
  331. * @param {Object} state - The Redux state.
  332. * @returns {Props}
  333. */
  334. export function _mapStateToProps(state: Object): $Shape<Props> {
  335. const { ownerId, status, time, videoUrl, muted } = state['features/shared-video'];
  336. const localParticipant = getLocalParticipant(state);
  337. const _isLocalAudioMuted = isLocalTrackMuted(state['features/base/tracks'], MEDIA_TYPE.AUDIO);
  338. return {
  339. _conference: getCurrentConference(state),
  340. _isLocalAudioMuted,
  341. _isOwner: ownerId === localParticipant.id,
  342. _muted: muted,
  343. _ownerId: ownerId,
  344. _status: status,
  345. _time: time,
  346. _videoUrl: videoUrl
  347. };
  348. }
  349. /**
  350. * Maps part of the props of this component to Redux actions.
  351. *
  352. * @param {Function} dispatch - The Redux dispatch function.
  353. * @returns {Props}
  354. */
  355. export function _mapDispatchToProps(dispatch: Function): $Shape<Props> {
  356. return {
  357. _displayWarning: () => {
  358. dispatch(showWarningNotification({
  359. titleKey: 'dialog.shareVideoLinkError'
  360. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  361. },
  362. _dockToolbox: value => {
  363. dispatch(dockToolbox(value));
  364. },
  365. _stopSharedVideo: () => {
  366. dispatch(stopSharedVideo());
  367. },
  368. _muteLocal: value => {
  369. dispatch(muteLocal(value, MEDIA_TYPE.AUDIO));
  370. },
  371. _setSharedVideoStatus: ({ videoUrl, status, time, ownerId, muted }) => {
  372. dispatch(setSharedVideoStatus({
  373. videoUrl,
  374. status,
  375. time,
  376. ownerId,
  377. muted
  378. }));
  379. }
  380. };
  381. }