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.8KB

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