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

middleware.native.js 6.3KB

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