Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

middleware.any.js 5.7KB

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