您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

middleware.any.js 5.7KB

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