You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

middleware.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // @flow
  2. import {
  3. CONFERENCE_JOINED,
  4. DATA_CHANNEL_OPENED
  5. } from '../base/conference';
  6. import { SET_CONFIG } from '../base/config';
  7. import { getParticipantCount } from '../base/participants';
  8. import { MiddlewareRegistry, StateListenerRegistry } from '../base/redux';
  9. import { shouldDisplayTileView } from '../video-layout';
  10. import { setPreferredVideoQuality, setMaxReceiverVideoQuality } from './actions';
  11. import { VIDEO_QUALITY_LEVELS } from './constants';
  12. import { getReceiverVideoQualityLevel } from './functions';
  13. import logger from './logger';
  14. import { getMinHeightForQualityLvlMap } from './selector';
  15. /**
  16. * Implements the middleware of the feature video-quality.
  17. *
  18. * @param {Store} store - The redux store.
  19. * @returns {Function}
  20. */
  21. MiddlewareRegistry.register(({ dispatch, getState }) => next => action => {
  22. if (action.type === DATA_CHANNEL_OPENED) {
  23. return _syncReceiveVideoQuality(getState, next, action);
  24. }
  25. const result = next(action);
  26. switch (action.type) {
  27. case CONFERENCE_JOINED: {
  28. if (navigator.product === 'ReactNative') {
  29. const { resolution } = getState()['features/base/config'];
  30. if (typeof resolution !== 'undefined') {
  31. dispatch(setPreferredVideoQuality(Number.parseInt(resolution, 10)));
  32. logger.info(`Configured preferred receiver video frame height to: ${resolution}`);
  33. }
  34. }
  35. break;
  36. }
  37. case SET_CONFIG: {
  38. const state = getState();
  39. const { videoQuality = {} } = state['features/base/config'];
  40. if (videoQuality.persist) {
  41. dispatch(
  42. setPreferredVideoQuality(
  43. state['features/video-quality-persistent-storage'].persistedPrefferedVideoQuality));
  44. }
  45. break;
  46. }
  47. }
  48. return result;
  49. });
  50. /**
  51. * Implements a state listener in order to calculate max receiver video quality.
  52. */
  53. StateListenerRegistry.register(
  54. /* selector */ state => {
  55. const { reducedUI } = state['features/base/responsive-ui'];
  56. const _shouldDisplayTileView = shouldDisplayTileView(state);
  57. const thumbnailSize = state['features/filmstrip']?.tileViewDimensions?.thumbnailSize;
  58. const participantCount = getParticipantCount(state);
  59. return {
  60. displayTileView: _shouldDisplayTileView,
  61. participantCount,
  62. reducedUI,
  63. thumbnailHeight: thumbnailSize?.height
  64. };
  65. },
  66. /* listener */ ({ displayTileView, participantCount, reducedUI, thumbnailHeight }, { dispatch, getState }) => {
  67. const state = getState();
  68. const { maxReceiverVideoQuality } = state['features/video-quality'];
  69. const { maxFullResolutionParticipants = 2 } = state['features/base/config'];
  70. let newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.HIGH;
  71. if (reducedUI) {
  72. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.LOW;
  73. } else if (displayTileView && !Number.isNaN(thumbnailHeight)) {
  74. newMaxRecvVideoQuality = getReceiverVideoQualityLevel(thumbnailHeight, getMinHeightForQualityLvlMap(state));
  75. // Override HD level calculated for the thumbnail height when # of participants threshold is exceeded
  76. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality && maxFullResolutionParticipants !== -1) {
  77. const override
  78. = participantCount > maxFullResolutionParticipants
  79. && newMaxRecvVideoQuality > VIDEO_QUALITY_LEVELS.STANDARD;
  80. logger.info(`Video quality level for thumbnail height: ${thumbnailHeight}, `
  81. + `is: ${newMaxRecvVideoQuality}, `
  82. + `override: ${String(override)}, `
  83. + `max full res N: ${maxFullResolutionParticipants}`);
  84. if (override) {
  85. newMaxRecvVideoQuality = VIDEO_QUALITY_LEVELS.STANDARD;
  86. }
  87. }
  88. }
  89. if (maxReceiverVideoQuality !== newMaxRecvVideoQuality) {
  90. dispatch(setMaxReceiverVideoQuality(newMaxRecvVideoQuality));
  91. }
  92. }, {
  93. deepEquals: true
  94. });
  95. /**
  96. * Helper function for updating the preferred receiver video constraint, based
  97. * on the user preference and the internal maximum.
  98. *
  99. * @param {JitsiConference} conference - The JitsiConference instance for the
  100. * current call.
  101. * @param {number} preferred - The user preferred max frame height.
  102. * @param {number} max - The maximum frame height the application should
  103. * receive.
  104. * @returns {void}
  105. */
  106. function _setReceiverVideoConstraint(conference, preferred, max) {
  107. if (conference) {
  108. const value = Math.min(preferred, max);
  109. conference.setReceiverVideoConstraint(value);
  110. logger.info(`setReceiverVideoConstraint: ${value}`);
  111. }
  112. }
  113. /**
  114. * Helper function for updating the preferred sender video constraint, based
  115. * on the user preference.
  116. *
  117. * @param {JitsiConference} conference - The JitsiConference instance for the
  118. * current call.
  119. * @param {number} preferred - The user preferred max frame height.
  120. * @returns {void}
  121. */
  122. function _setSenderVideoConstraint(conference, preferred) {
  123. if (conference) {
  124. conference.setSenderVideoConstraint(preferred)
  125. .catch(err => {
  126. logger.error(`Changing sender resolution to ${preferred} failed - ${err} `);
  127. });
  128. }
  129. }
  130. /**
  131. * Sets the maximum receive video quality.
  132. *
  133. * @param {Function} getState - The redux function which returns the current redux state.
  134. * @param {Dispatch} next - The redux {@code dispatch} function to dispatch the
  135. * specified {@code action} to the specified {@code store}.
  136. * @param {Action} action - The redux action {@code DATA_CHANNEL_STATUS_CHANGED}
  137. * which is being dispatched in the specified {@code store}.
  138. * @private
  139. * @returns {Object} The value returned by {@code next(action)}.
  140. */
  141. function _syncReceiveVideoQuality(getState, next, action) {
  142. const state = getState();
  143. const {
  144. conference
  145. } = state['features/base/conference'];
  146. const {
  147. maxReceiverVideoQuality,
  148. preferredVideoQuality
  149. } = state['features/video-quality'];
  150. _setReceiverVideoConstraint(
  151. conference,
  152. preferredVideoQuality,
  153. maxReceiverVideoQuality);
  154. return next(action);
  155. }
  156. /**
  157. * Registers a change handler for state['features/base/conference'] to update
  158. * the preferred video quality levels based on user preferred and internal
  159. * settings.
  160. */
  161. StateListenerRegistry.register(
  162. /* selector */ state => {
  163. const { conference } = state['features/base/conference'];
  164. const {
  165. maxReceiverVideoQuality,
  166. preferredVideoQuality
  167. } = state['features/video-quality'];
  168. return {
  169. conference,
  170. maxReceiverVideoQuality,
  171. preferredVideoQuality
  172. };
  173. },
  174. /* listener */ (currentState, store, previousState = {}) => {
  175. const {
  176. conference,
  177. maxReceiverVideoQuality,
  178. preferredVideoQuality
  179. } = currentState;
  180. const changedConference = conference !== previousState.conference;
  181. const changedPreferredVideoQuality = preferredVideoQuality !== previousState.preferredVideoQuality;
  182. const changedMaxVideoQuality = maxReceiverVideoQuality !== previousState.maxReceiverVideoQuality;
  183. if (changedConference || changedPreferredVideoQuality || changedMaxVideoQuality) {
  184. _setReceiverVideoConstraint(conference, preferredVideoQuality, maxReceiverVideoQuality);
  185. }
  186. if (changedConference || changedPreferredVideoQuality) {
  187. _setSenderVideoConstraint(conference, preferredVideoQuality);
  188. }
  189. });