Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

middleware.any.ts 7.2KB

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