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.

middleware.any.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // @flow
  2. import { batch } from 'react-redux';
  3. import { CONFERENCE_JOIN_IN_PROGRESS, CONFERENCE_LEFT } from '../base/conference/actionTypes';
  4. import { getCurrentConference } from '../base/conference/functions';
  5. import { MEDIA_TYPE } from '../base/media';
  6. import {
  7. PARTICIPANT_LEFT,
  8. getLocalParticipant,
  9. participantJoined,
  10. participantLeft,
  11. pinParticipant,
  12. getParticipantById
  13. } from '../base/participants';
  14. import { MiddlewareRegistry } from '../base/redux';
  15. import { SET_SHARED_VIDEO_STATUS, RESET_SHARED_VIDEO_STATUS } from './actionTypes';
  16. import {
  17. resetSharedVideoStatus,
  18. setSharedVideoStatus
  19. } from './actions.any';
  20. import { SHARED_VIDEO, VIDEO_PLAYER_PARTICIPANT_NAME, PLAYBACK_STATUSES } from './constants';
  21. import { isSharingStatus } from './functions';
  22. import logger from './logger';
  23. declare var APP: Object;
  24. /**
  25. * Middleware that captures actions related to video sharing and updates
  26. * components not hooked into redux.
  27. *
  28. * @param {Store} store - The redux store.
  29. * @returns {Function}
  30. */
  31. MiddlewareRegistry.register(store => next => action => {
  32. const { dispatch, getState } = store;
  33. const state = getState();
  34. switch (action.type) {
  35. case CONFERENCE_JOIN_IN_PROGRESS: {
  36. const { conference } = action;
  37. const localParticipantId = getLocalParticipant(state)?.id;
  38. conference.addCommandListener(SHARED_VIDEO,
  39. ({ value, attributes }) => {
  40. const { from } = attributes;
  41. const sharedVideoStatus = attributes.state;
  42. if (isSharingStatus(sharedVideoStatus)) {
  43. handleSharingVideoStatus(store, value, attributes, conference);
  44. } else if (sharedVideoStatus === 'stop') {
  45. const videoParticipant = getParticipantById(state, value);
  46. dispatch(participantLeft(value, conference, {
  47. isFakeParticipant: videoParticipant?.isFakeParticipant
  48. }));
  49. if (localParticipantId !== from) {
  50. dispatch(resetSharedVideoStatus());
  51. }
  52. }
  53. }
  54. );
  55. break;
  56. }
  57. case CONFERENCE_LEFT:
  58. dispatch(resetSharedVideoStatus());
  59. break;
  60. case PARTICIPANT_LEFT: {
  61. const conference = getCurrentConference(state);
  62. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  63. if (action.participant.id === stateOwnerId) {
  64. batch(() => {
  65. dispatch(resetSharedVideoStatus());
  66. dispatch(participantLeft(statevideoUrl, conference));
  67. });
  68. }
  69. break;
  70. }
  71. case SET_SHARED_VIDEO_STATUS: {
  72. const conference = getCurrentConference(state);
  73. const localParticipantId = getLocalParticipant(state)?.id;
  74. const { videoUrl, status, ownerId, time, muted, volume } = action;
  75. const operator = status === PLAYBACK_STATUSES.PLAYING ? 'is' : '';
  76. logger.debug(`User with id: ${ownerId} ${operator} ${status} video sharing.`);
  77. if (typeof APP !== 'undefined') {
  78. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, status, ownerId);
  79. }
  80. if (localParticipantId === ownerId) {
  81. sendShareVideoCommand({
  82. conference,
  83. localParticipantId,
  84. muted,
  85. status,
  86. time,
  87. id: videoUrl,
  88. volume
  89. });
  90. }
  91. break;
  92. }
  93. case RESET_SHARED_VIDEO_STATUS: {
  94. const localParticipantId = getLocalParticipant(state)?.id;
  95. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  96. if (!stateOwnerId) {
  97. break;
  98. }
  99. logger.debug(`User with id: ${stateOwnerId} stop video sharing.`);
  100. if (typeof APP !== 'undefined') {
  101. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, 'stop', stateOwnerId);
  102. }
  103. if (localParticipantId === stateOwnerId) {
  104. const conference = getCurrentConference(state);
  105. sendShareVideoCommand({
  106. conference,
  107. id: statevideoUrl,
  108. localParticipantId,
  109. muted: true,
  110. status: 'stop',
  111. time: 0,
  112. volume: 0
  113. });
  114. }
  115. break;
  116. }
  117. }
  118. return next(action);
  119. });
  120. /**
  121. * Handles the playing, pause and start statuses for the shared video.
  122. * Dispatches participantJoined event and, if necessary, pins it.
  123. * Sets the SharedVideoStatus if the event was triggered by the local user.
  124. *
  125. * @param {Store} store - The redux store.
  126. * @param {string} videoUrl - The id of the video to the shared.
  127. * @param {Object} attributes - The attributes received from the share video command.
  128. * @param {JitsiConference} conference - The current conference.
  129. * @returns {void}
  130. */
  131. function handleSharingVideoStatus(store, videoUrl, { state, time, from, muted }, conference) {
  132. const { dispatch, getState } = store;
  133. const localParticipantId = getLocalParticipant(getState()).id;
  134. const oldStatus = getState()['features/shared-video']?.status;
  135. if (state === 'start' || ![ 'playing', 'pause', 'start' ].includes(oldStatus)) {
  136. const youtubeId = videoUrl.match(/http/) ? false : videoUrl;
  137. const avatarURL = youtubeId ? `https://img.youtube.com/vi/${youtubeId}/0.jpg` : '';
  138. dispatch(participantJoined({
  139. conference,
  140. id: videoUrl,
  141. isFakeParticipant: true,
  142. avatarURL,
  143. name: VIDEO_PLAYER_PARTICIPANT_NAME
  144. }));
  145. dispatch(pinParticipant(videoUrl));
  146. }
  147. if (localParticipantId !== from) {
  148. dispatch(setSharedVideoStatus({
  149. muted: muted === 'true',
  150. ownerId: from,
  151. status: state,
  152. time: Number(time),
  153. videoUrl
  154. }));
  155. }
  156. }
  157. /* eslint-disable max-params */
  158. /**
  159. * Sends SHARED_VIDEO command.
  160. *
  161. * @param {string} id - The id of the video.
  162. * @param {string} status - The status of the shared video.
  163. * @param {JitsiConference} conference - The current conference.
  164. * @param {string} localParticipantId - The id of the local participant.
  165. * @param {string} time - The seek position of the video.
  166. * @returns {void}
  167. */
  168. function sendShareVideoCommand({ id, status, conference, localParticipantId, time, muted, volume }) {
  169. conference.sendCommandOnce(SHARED_VIDEO, {
  170. value: id,
  171. attributes: {
  172. from: localParticipantId,
  173. muted,
  174. state: status,
  175. time,
  176. volume
  177. }
  178. });
  179. }