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.ts 8.6KB

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