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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // @flow
  2. import type { Dispatch } from 'redux';
  3. import { getFeatureFlag, TILE_VIEW_ENABLED } from '../base/flags';
  4. import {
  5. getPinnedParticipant,
  6. getParticipantCount,
  7. pinParticipant
  8. } from '../base/participants';
  9. import {
  10. DEFAULT_MAX_COLUMNS,
  11. ABSOLUTE_MAX_COLUMNS
  12. } from '../filmstrip/constants';
  13. import { getNumberOfPartipantsForTileView } from '../filmstrip/functions.web';
  14. import { isVideoPlaying } from '../shared-video/functions';
  15. import { VIDEO_QUALITY_LEVELS } from '../video-quality/constants';
  16. import { LAYOUTS } from './constants';
  17. declare var interfaceConfig: Object;
  18. /**
  19. * A selector for retrieving the current automatic pinning setting.
  20. *
  21. * @private
  22. * @returns {string|undefined} The string "remote-only" is returned if only
  23. * remote screen sharing should be automatically pinned, any other truthy value
  24. * means automatically pin all screen shares. Falsy means do not automatically
  25. * pin any screen shares.
  26. */
  27. export function getAutoPinSetting() {
  28. return typeof interfaceConfig === 'object'
  29. ? interfaceConfig.AUTO_PIN_LATEST_SCREEN_SHARE
  30. : 'remote-only';
  31. }
  32. /**
  33. * Returns the {@code LAYOUTS} constant associated with the layout
  34. * the application should currently be in.
  35. *
  36. * @param {Object} state - The redux state.
  37. * @returns {string}
  38. */
  39. export function getCurrentLayout(state: Object) {
  40. if (shouldDisplayTileView(state)) {
  41. return LAYOUTS.TILE_VIEW;
  42. } else if (interfaceConfig.VERTICAL_FILMSTRIP) {
  43. return LAYOUTS.VERTICAL_FILMSTRIP_VIEW;
  44. }
  45. return LAYOUTS.HORIZONTAL_FILMSTRIP_VIEW;
  46. }
  47. /**
  48. * Returns how many columns should be displayed in tile view. The number
  49. * returned will be between 1 and 7, inclusive.
  50. *
  51. * @param {Object} state - The redux store state.
  52. * @param {number} width - Custom width to use for calculation.
  53. * @returns {number}
  54. */
  55. export function getMaxColumnCount() {
  56. const configuredMax = (typeof interfaceConfig === 'undefined'
  57. ? DEFAULT_MAX_COLUMNS
  58. : interfaceConfig.TILE_VIEW_MAX_COLUMNS) || DEFAULT_MAX_COLUMNS;
  59. return Math.min(Math.max(configuredMax, 1), ABSOLUTE_MAX_COLUMNS);
  60. }
  61. /**
  62. * Returns the cell count dimensions for tile view. Tile view tries to uphold
  63. * equal count of tiles for height and width, until maxColumn is reached in
  64. * which rows will be added but no more columns.
  65. *
  66. * @param {Object} state - The redux store state.
  67. * @param {number} width - Custom width to use for calculation.
  68. * @returns {Object} An object is return with the desired number of columns,
  69. * rows, and visible rows (the rest should overflow) for the tile view layout.
  70. */
  71. export function getNotResponsiveTileViewGridDimensions(state: Object) {
  72. const maxColumns = getMaxColumnCount(state);
  73. const numberOfParticipants = getNumberOfPartipantsForTileView(state);
  74. const columnsToMaintainASquare = Math.ceil(Math.sqrt(numberOfParticipants));
  75. const columns = Math.min(columnsToMaintainASquare, maxColumns);
  76. const rows = Math.ceil(numberOfParticipants / columns);
  77. const minVisibleRows = Math.min(maxColumns, rows);
  78. return {
  79. columns,
  80. minVisibleRows,
  81. rows
  82. };
  83. }
  84. /**
  85. * Selector for determining if the UI layout should be in tile view. Tile view
  86. * is determined by more than just having the tile view setting enabled, as
  87. * one-on-one calls should not be in tile view, as well as etherpad editing.
  88. *
  89. * @param {Object} state - The redux state.
  90. * @returns {boolean} True if tile view should be displayed.
  91. */
  92. export function shouldDisplayTileView(state: Object = {}) {
  93. const participantCount = getParticipantCount(state);
  94. const tileViewEnabledFeatureFlag = getFeatureFlag(state, TILE_VIEW_ENABLED, true);
  95. const { disableTileView } = state['features/base/config'];
  96. if (disableTileView || !tileViewEnabledFeatureFlag) {
  97. return false;
  98. }
  99. const { tileViewEnabled } = state['features/video-layout'];
  100. if (tileViewEnabled !== undefined) {
  101. // If the user explicitly requested a view mode, we
  102. // do that.
  103. return tileViewEnabled;
  104. }
  105. const { iAmRecorder } = state['features/base/config'];
  106. // None tile view mode is easier to calculate (no need for many negations), so we do
  107. // that and negate it only once.
  108. const shouldDisplayNormalMode = Boolean(
  109. // Reasons for normal mode:
  110. // Editing etherpad
  111. state['features/etherpad']?.editing
  112. // We pinned a participant
  113. || getPinnedParticipant(state)
  114. // It's a 1-on-1 meeting
  115. || participantCount < 3
  116. // There is a shared YouTube video in the meeting
  117. || isVideoPlaying(state)
  118. // We want jibri to use stage view by default
  119. || iAmRecorder
  120. );
  121. return !shouldDisplayNormalMode;
  122. }
  123. /**
  124. * Private helper to automatically pin the latest screen share stream or unpin
  125. * if there are no more screen share streams.
  126. *
  127. * @param {Array<string>} screenShares - Array containing the list of all the screen sharing endpoints
  128. * before the update was triggered (including the ones that have been removed from redux because of the update).
  129. * @param {Store} store - The redux store.
  130. * @returns {void}
  131. */
  132. export function updateAutoPinnedParticipant(
  133. screenShares: Array<string>, { dispatch, getState }: { dispatch: Dispatch<any>, getState: Function }) {
  134. const state = getState();
  135. const remoteScreenShares = state['features/video-layout'].remoteScreenShares;
  136. const pinned = getPinnedParticipant(getState);
  137. // if the pinned participant is shared video or some other fake participant we want to skip auto-pinning
  138. if (pinned?.isFakeParticipant) {
  139. return;
  140. }
  141. // Unpin the screen share when the screen sharing participant leaves. Switch to tile view if no other
  142. // participant was pinned before screen share was auto-pinned, pin the previously pinned participant otherwise.
  143. if (!remoteScreenShares?.length) {
  144. let participantId = null;
  145. if (pinned && !screenShares.find(share => share === pinned.id)) {
  146. participantId = pinned.id;
  147. }
  148. dispatch(pinParticipant(participantId));
  149. return;
  150. }
  151. const latestScreenShareParticipantId = remoteScreenShares[remoteScreenShares.length - 1];
  152. if (latestScreenShareParticipantId) {
  153. dispatch(pinParticipant(latestScreenShareParticipantId));
  154. }
  155. }
  156. /**
  157. * Selector for whether we are currently in tile view.
  158. *
  159. * @param {Object} state - The redux state.
  160. * @returns {boolean}
  161. */
  162. export function isLayoutTileView(state: Object) {
  163. return getCurrentLayout(state) === LAYOUTS.TILE_VIEW;
  164. }
  165. /**
  166. * Gets the video quality for the given height.
  167. *
  168. * @param {number|undefined} height - Height of the video container.
  169. * @returns {number}
  170. */
  171. function getVideoQualityForHeight(height: number) {
  172. if (!height) {
  173. return VIDEO_QUALITY_LEVELS.LOW;
  174. }
  175. const levels = Object.values(VIDEO_QUALITY_LEVELS)
  176. .map(Number)
  177. .sort((a, b) => a - b);
  178. for (const level of levels) {
  179. if (height <= level) {
  180. return level;
  181. }
  182. }
  183. return VIDEO_QUALITY_LEVELS.ULTRA;
  184. }
  185. /**
  186. * Gets the video quality level for the resizable filmstrip thumbnail height.
  187. *
  188. * @param {Object} state - Redux state.
  189. * @returns {number}
  190. */
  191. export function getVideoQualityForResizableFilmstripThumbnails(state) {
  192. const height = state['features/filmstrip'].verticalViewDimensions?.gridView?.thumbnailSize?.height;
  193. return getVideoQualityForHeight(height);
  194. }
  195. /**
  196. * Gets the video quality for the large video.
  197. *
  198. * @returns {number}
  199. */
  200. export function getVideoQualityForLargeVideo() {
  201. const wrapper = document.querySelector('#largeVideoWrapper');
  202. return getVideoQualityForHeight(wrapper.clientHeight);
  203. }