選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

middleware.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* global APP */
  2. import { CONNECTION_ESTABLISHED } from '../connection';
  3. import {
  4. getLocalParticipant,
  5. getParticipantById,
  6. PIN_PARTICIPANT
  7. } from '../participants';
  8. import { MiddlewareRegistry } from '../redux';
  9. import { TRACK_ADDED, TRACK_REMOVED } from '../tracks';
  10. import { createConference } from './actions';
  11. import { SET_LASTN } from './actionTypes';
  12. import {
  13. _addLocalTracksToConference,
  14. _handleParticipantError,
  15. _removeLocalTracksFromConference
  16. } from './functions';
  17. /**
  18. * Implements the middleware of the feature base/conference.
  19. *
  20. * @param {Store} store - Redux store.
  21. * @returns {Function}
  22. */
  23. MiddlewareRegistry.register(store => next => action => {
  24. switch (action.type) {
  25. case CONNECTION_ESTABLISHED:
  26. return _connectionEstablished(store, next, action);
  27. case PIN_PARTICIPANT:
  28. return _pinParticipant(store, next, action);
  29. case SET_LASTN:
  30. return _setLastN(store, next, action);
  31. case TRACK_ADDED:
  32. case TRACK_REMOVED:
  33. return _trackAddedOrRemoved(store, next, action);
  34. }
  35. return next(action);
  36. });
  37. /**
  38. * Notifies the feature base/conference that the action CONNECTION_ESTABLISHED
  39. * is being dispatched within a specific Redux store.
  40. *
  41. * @param {Store} store - The Redux store in which the specified action is being
  42. * dispatched.
  43. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  44. * specified action to the specified store.
  45. * @param {Action} action - The Redux action CONNECTION_ESTABLISHED which is
  46. * being dispatched in the specified store.
  47. * @private
  48. * @returns {Object} The new state that is the result of the reduction of the
  49. * specified action.
  50. */
  51. function _connectionEstablished(store, next, action) {
  52. const result = next(action);
  53. // FIXME: workaround for the web version. Currently the creation of the
  54. // conference is handled by /conference.js
  55. if (typeof APP === 'undefined') {
  56. store.dispatch(createConference());
  57. }
  58. return result;
  59. }
  60. /**
  61. * Notifies the feature base/conference that the action PIN_PARTICIPANT is being
  62. * dispatched within a specific Redux store. Pins the specified remote
  63. * participant in the associated conference, ignores the local participant.
  64. *
  65. * @param {Store} store - The Redux store in which the specified action is being
  66. * dispatched.
  67. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  68. * specified action to the specified store.
  69. * @param {Action} action - The Redux action PIN_PARTICIPANT which is being
  70. * dispatched in the specified store.
  71. * @private
  72. * @returns {Object} The new state that is the result of the reduction of the
  73. * specified action.
  74. */
  75. function _pinParticipant(store, next, action) {
  76. const state = store.getState();
  77. const participants = state['features/base/participants'];
  78. const id = action.participant.id;
  79. const participantById = getParticipantById(participants, id);
  80. let pin;
  81. // The following condition prevents signaling to pin local participant. The
  82. // logic is:
  83. // - If we have an ID, we check if the participant identified by that ID is
  84. // local.
  85. // - If we don't have an ID (i.e. no participant identified by an ID), we
  86. // check for local participant. If she's currently pinned, then this
  87. // action will unpin her and that's why we won't signal here too.
  88. if (participantById) {
  89. pin = !participantById.local;
  90. } else {
  91. const localParticipant = getLocalParticipant(participants);
  92. pin = !localParticipant || !localParticipant.pinned;
  93. }
  94. if (pin) {
  95. const conference = state['features/base/conference'].conference;
  96. try {
  97. conference.pinParticipant(id);
  98. } catch (err) {
  99. _handleParticipantError(err);
  100. }
  101. }
  102. return next(action);
  103. }
  104. /**
  105. * Sets the last N (value) of the video channel in the conference.
  106. *
  107. * @param {Store} store - The Redux store in which the specified action is being
  108. * dispatched.
  109. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  110. * specified action to the specified store.
  111. * @param {Action} action - The Redux action SET_LASTN which is being dispatched
  112. * in the specified store.
  113. * @private
  114. * @returns {Object} The new state that is the result of the reduction of the
  115. * specified action.
  116. */
  117. function _setLastN(store, next, action) {
  118. const { conference } = store.getState()['features/base/conference'];
  119. if (conference) {
  120. try {
  121. conference.setLastN(action.lastN);
  122. } catch (err) {
  123. console.error(`Failed to set lastN: ${err}`);
  124. }
  125. }
  126. return next(action);
  127. }
  128. /**
  129. * Synchronizes local tracks from state with local tracks in JitsiConference
  130. * instance.
  131. *
  132. * @param {Store} store - Redux store.
  133. * @param {Object} action - Action object.
  134. * @private
  135. * @returns {Promise}
  136. */
  137. function _syncConferenceLocalTracksWithState(store, action) {
  138. const state = store.getState()['features/base/conference'];
  139. const conference = state.conference;
  140. let promise;
  141. // XXX The conference may already be in the process of being left, that's
  142. // why we should not add/remove local tracks to such conference.
  143. if (conference && conference !== state.leaving) {
  144. const track = action.track.jitsiTrack;
  145. if (action.type === TRACK_ADDED) {
  146. promise = _addLocalTracksToConference(conference, [ track ]);
  147. } else {
  148. promise = _removeLocalTracksFromConference(conference, [ track ]);
  149. }
  150. }
  151. return promise || Promise.resolve();
  152. }
  153. /**
  154. * Notifies the feature base/conference that the action TRACK_ADDED
  155. * or TRACK_REMOVED is being dispatched within a specific Redux store.
  156. *
  157. * @param {Store} store - The Redux store in which the specified action is being
  158. * dispatched.
  159. * @param {Dispatch} next - The Redux dispatch function to dispatch the
  160. * specified action to the specified store.
  161. * @param {Action} action - The Redux action TRACK_ADDED or TRACK_REMOVED which
  162. * is being dispatched in the specified store.
  163. * @private
  164. * @returns {Object} The new state that is the result of the reduction of the
  165. * specified action.
  166. */
  167. function _trackAddedOrRemoved(store, next, action) {
  168. const track = action.track;
  169. if (track && track.local) {
  170. return (
  171. _syncConferenceLocalTracksWithState(store, action)
  172. .then(() => next(action)));
  173. }
  174. return next(action);
  175. }