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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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, getParticipantDisplayName } 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 { showWarningNotification } from '../notifications/actions';
  15. import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
  16. import { RESET_SHARED_VIDEO_STATUS, SET_SHARED_VIDEO_STATUS } from './actionTypes';
  17. import {
  18. hideConfirmPlayingDialog,
  19. resetSharedVideoStatus,
  20. setAllowedUrlDomians,
  21. setSharedVideoStatus,
  22. showConfirmPlayingDialog
  23. } from './actions.any';
  24. import {
  25. DEFAULT_ALLOWED_URL_DOMAINS,
  26. PLAYBACK_START,
  27. PLAYBACK_STATUSES,
  28. SHARED_VIDEO,
  29. VIDEO_PLAYER_PARTICIPANT_NAME
  30. } from './constants';
  31. import { isSharedVideoEnabled, isSharingStatus, isURLAllowedForSharedVideo, sendShareVideoCommand } from './functions';
  32. import logger from './logger';
  33. /**
  34. * Middleware that captures actions related to video sharing and updates
  35. * components not hooked into redux.
  36. *
  37. * @param {Store} store - The redux store.
  38. * @returns {Function}
  39. */
  40. MiddlewareRegistry.register(store => next => action => {
  41. const { dispatch, getState } = store;
  42. if (!isSharedVideoEnabled(getState())) {
  43. return next(action);
  44. }
  45. switch (action.type) {
  46. case CONFERENCE_JOIN_IN_PROGRESS: {
  47. const { conference } = action;
  48. const localParticipantId = getLocalParticipant(getState())?.id;
  49. conference.addCommandListener(SHARED_VIDEO,
  50. ({ value, attributes }: { attributes: {
  51. from: string; muted: string; state: string; time: string; }; value: string; }) => {
  52. const state = getState();
  53. const { from } = attributes;
  54. const sharedVideoStatus = attributes.state;
  55. if (isSharingStatus(sharedVideoStatus)) {
  56. // confirmShowVideo is undefined the first time we receive
  57. // when confirmShowVideo is false we ignore everything except stop that resets it
  58. if (getState()['features/shared-video'].confirmShowVideo === false) {
  59. return;
  60. }
  61. if (isURLAllowedForSharedVideo(value, getState()['features/shared-video'].allowedUrlDomains, true)
  62. || localParticipantId === from
  63. || getState()['features/shared-video'].confirmShowVideo) { // if confirmed skip asking again
  64. handleSharingVideoStatus(store, value, attributes, conference);
  65. } else {
  66. dispatch(showConfirmPlayingDialog(getParticipantDisplayName(getState(), from), () => {
  67. handleSharingVideoStatus(store, value, attributes, conference);
  68. return true; // on mobile this is used to close the dialog
  69. }));
  70. }
  71. return;
  72. }
  73. if (sharedVideoStatus === 'stop') {
  74. const videoParticipant = getParticipantById(state, value);
  75. if (getState()['features/shared-video'].confirmShowVideo === false) {
  76. dispatch(showWarningNotification({
  77. titleKey: 'dialog.shareVideoLinkStopped',
  78. titleArguments: {
  79. name: getParticipantDisplayName(getState(), from)
  80. }
  81. }, NOTIFICATION_TIMEOUT_TYPE.LONG));
  82. }
  83. dispatch(hideConfirmPlayingDialog());
  84. dispatch(participantLeft(value, conference, {
  85. fakeParticipant: videoParticipant?.fakeParticipant
  86. }));
  87. if (localParticipantId !== from) {
  88. dispatch(resetSharedVideoStatus());
  89. }
  90. }
  91. }
  92. );
  93. break;
  94. }
  95. case CONFERENCE_LEFT:
  96. dispatch(setAllowedUrlDomians(DEFAULT_ALLOWED_URL_DOMAINS));
  97. dispatch(resetSharedVideoStatus());
  98. break;
  99. case PARTICIPANT_LEFT: {
  100. const state = getState();
  101. const conference = getCurrentConference(state);
  102. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  103. if (action.participant.id === stateOwnerId) {
  104. batch(() => {
  105. dispatch(resetSharedVideoStatus());
  106. dispatch(participantLeft(statevideoUrl ?? '', conference));
  107. });
  108. }
  109. break;
  110. }
  111. case SET_CONFIG:
  112. case SET_DYNAMIC_BRANDING_DATA: {
  113. const result = next(action);
  114. const state = getState();
  115. const { sharedVideoAllowedURLDomains: allowedURLDomainsFromConfig = [] } = state['features/base/config'];
  116. const { sharedVideoAllowedURLDomains: allowedURLDomainsFromBranding = [] } = state['features/dynamic-branding'];
  117. dispatch(setAllowedUrlDomians([
  118. ...DEFAULT_ALLOWED_URL_DOMAINS,
  119. ...allowedURLDomainsFromBranding,
  120. ...allowedURLDomainsFromConfig
  121. ]));
  122. return result;
  123. }
  124. case SET_SHARED_VIDEO_STATUS: {
  125. const state = getState();
  126. const conference = getCurrentConference(state);
  127. const localParticipantId = getLocalParticipant(state)?.id;
  128. const { videoUrl, status, ownerId, time, muted, volume } = action;
  129. const operator = status === PLAYBACK_STATUSES.PLAYING ? 'is' : '';
  130. logger.debug(`User with id: ${ownerId} ${operator} ${status} video sharing.`);
  131. if (typeof APP !== 'undefined') {
  132. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, status, ownerId);
  133. }
  134. // when setting status we need to send the command for that, but not do it for the start command
  135. // as we are sending the command in playSharedVideo and setting the start status once
  136. // we receive the response, this way we will start the video at the same time when remote participants
  137. // start it, on receiving the command
  138. if (status === 'start') {
  139. break;
  140. }
  141. if (localParticipantId === ownerId) {
  142. sendShareVideoCommand({
  143. conference,
  144. localParticipantId,
  145. muted,
  146. status,
  147. time,
  148. id: videoUrl,
  149. volume
  150. });
  151. }
  152. break;
  153. }
  154. case RESET_SHARED_VIDEO_STATUS: {
  155. const state = getState();
  156. const localParticipantId = getLocalParticipant(state)?.id;
  157. const { ownerId: stateOwnerId, videoUrl: statevideoUrl } = state['features/shared-video'];
  158. if (!stateOwnerId) {
  159. break;
  160. }
  161. logger.debug(`User with id: ${stateOwnerId} stop video sharing.`);
  162. if (typeof APP !== 'undefined') {
  163. APP.API.notifyAudioOrVideoSharingToggled(MEDIA_TYPE.VIDEO, 'stop', stateOwnerId);
  164. }
  165. if (localParticipantId === stateOwnerId) {
  166. const conference = getCurrentConference(state);
  167. sendShareVideoCommand({
  168. conference,
  169. id: statevideoUrl ?? '',
  170. localParticipantId,
  171. muted: true,
  172. status: 'stop',
  173. time: 0,
  174. volume: 0
  175. });
  176. }
  177. break;
  178. }
  179. }
  180. return next(action);
  181. });
  182. /**
  183. * Handles the playing, pause and start statuses for the shared video.
  184. * Dispatches participantJoined event and, if necessary, pins it.
  185. * Sets the SharedVideoStatus if the event was triggered by the local user.
  186. *
  187. * @param {Store} store - The redux store.
  188. * @param {string} videoUrl - The id of the video to the shared.
  189. * @param {Object} attributes - The attributes received from the share video command.
  190. * @param {JitsiConference} conference - The current conference.
  191. * @returns {void}
  192. */
  193. function handleSharingVideoStatus(store: IStore, videoUrl: string,
  194. { state, time, from, muted }: { from: string; muted: string; state: string; time: string; },
  195. conference: IJitsiConference) {
  196. const { dispatch, getState } = store;
  197. const localParticipantId = getLocalParticipant(getState())?.id;
  198. const oldStatus = getState()['features/shared-video']?.status ?? '';
  199. const oldVideoUrl = getState()['features/shared-video'].videoUrl;
  200. if (oldVideoUrl && oldVideoUrl !== videoUrl) {
  201. logger.warn(
  202. `User with id: ${from} sent videoUrl: ${videoUrl} while we are playing: ${oldVideoUrl}`);
  203. return;
  204. }
  205. // If the video was not started (no participant) we want to create the participant
  206. // this can be triggered by start, but also by paused or playing
  207. // commands (joining late) and getting the current state
  208. if (state === PLAYBACK_START || !isSharingStatus(oldStatus)) {
  209. const youtubeId = videoUrl.match(/http/) ? false : videoUrl;
  210. const avatarURL = youtubeId ? `https://img.youtube.com/vi/${youtubeId}/0.jpg` : '';
  211. dispatch(participantJoined({
  212. conference,
  213. fakeParticipant: FakeParticipant.SharedVideo,
  214. id: videoUrl,
  215. avatarURL,
  216. name: VIDEO_PLAYER_PARTICIPANT_NAME
  217. }));
  218. dispatch(pinParticipant(videoUrl));
  219. if (localParticipantId === from) {
  220. dispatch(setSharedVideoStatus({
  221. videoUrl,
  222. status: state,
  223. time: Number(time),
  224. ownerId: localParticipantId
  225. }));
  226. }
  227. }
  228. if (localParticipantId !== from) {
  229. dispatch(setSharedVideoStatus({
  230. muted: muted === 'true',
  231. ownerId: from,
  232. status: state,
  233. time: Number(time),
  234. videoUrl
  235. }));
  236. }
  237. }