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

functions.web.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. // @flow
  2. import { JitsiParticipantConnectionStatus } from '../base/lib-jitsi-meet';
  3. import { MEDIA_TYPE } from '../base/media';
  4. import {
  5. getLocalParticipant,
  6. getParticipantById,
  7. getParticipantCountWithFake,
  8. getPinnedParticipant
  9. } from '../base/participants';
  10. import { toState } from '../base/redux';
  11. import {
  12. getLocalVideoTrack,
  13. getTrackByMediaTypeAndParticipant,
  14. isLocalTrackMuted,
  15. isRemoteTrackMuted
  16. } from '../base/tracks/functions';
  17. import {
  18. ASPECT_RATIO_BREAKPOINT,
  19. DISPLAY_AVATAR,
  20. DISPLAY_AVATAR_WITH_NAME,
  21. DISPLAY_BLACKNESS_WITH_NAME,
  22. DISPLAY_VIDEO,
  23. DISPLAY_VIDEO_WITH_NAME,
  24. SCROLL_SIZE,
  25. SQUARE_TILE_ASPECT_RATIO,
  26. STAGE_VIEW_THUMBNAIL_HORIZONTAL_BORDER,
  27. TILE_ASPECT_RATIO,
  28. TILE_HORIZONTAL_MARGIN,
  29. TILE_VERTICAL_MARGIN,
  30. VERTICAL_FILMSTRIP_MIN_HORIZONTAL_MARGIN
  31. } from './constants';
  32. export * from './functions.any';
  33. declare var interfaceConfig: Object;
  34. /**
  35. * Returns true if the filmstrip on mobile is visible, false otherwise.
  36. *
  37. * NOTE: Filmstrip on web behaves differently to mobile, much simpler, but so
  38. * function lies here only for the sake of consistency and to avoid flow errors
  39. * on import.
  40. *
  41. * @param {Object | Function} stateful - The Object or Function that can be
  42. * resolved to a Redux state object with the toState function.
  43. * @returns {boolean}
  44. */
  45. export function isFilmstripVisible(stateful: Object | Function) {
  46. return toState(stateful)['features/filmstrip'].visible;
  47. }
  48. /**
  49. * Determines whether the remote video thumbnails should be displayed/visible in
  50. * the filmstrip.
  51. *
  52. * @param {Object} state - The full redux state.
  53. * @returns {boolean} - If remote video thumbnails should be displayed/visible
  54. * in the filmstrip, then {@code true}; otherwise, {@code false}.
  55. */
  56. export function shouldRemoteVideosBeVisible(state: Object) {
  57. if (state['features/invite'].calleeInfoVisible) {
  58. return false;
  59. }
  60. // Include fake participants to derive how many thumbnails are dispalyed,
  61. // as it is assumed all participants, including fake, will be displayed
  62. // in the filmstrip.
  63. const participantCount = getParticipantCountWithFake(state);
  64. let pinnedParticipant;
  65. const { disable1On1Mode } = state['features/base/config'];
  66. return Boolean(
  67. participantCount > 2
  68. // Always show the filmstrip when there is another participant to
  69. // show and the local video is pinned, or the toolbar is displayed.
  70. || (participantCount > 1
  71. && disable1On1Mode !== null
  72. && (state['features/toolbox'].visible
  73. || ((pinnedParticipant = getPinnedParticipant(state))
  74. && pinnedParticipant.local)))
  75. || disable1On1Mode);
  76. }
  77. /**
  78. * Checks whether there is a playable video stream available for the user associated with the passed ID.
  79. *
  80. * @param {Object | Function} stateful - The Object or Function that can be
  81. * resolved to a Redux state object with the toState function.
  82. * @param {string} id - The id of the participant.
  83. * @returns {boolean} <tt>true</tt> if there is a playable video stream available
  84. * or <tt>false</tt> otherwise.
  85. */
  86. export function isVideoPlayable(stateful: Object | Function, id: String) {
  87. const state = toState(stateful);
  88. const tracks = state['features/base/tracks'];
  89. const participant = id ? getParticipantById(state, id) : getLocalParticipant(state);
  90. const isLocal = participant?.local ?? true;
  91. const { connectionStatus } = participant || {};
  92. const videoTrack
  93. = isLocal ? getLocalVideoTrack(tracks) : getTrackByMediaTypeAndParticipant(tracks, MEDIA_TYPE.VIDEO, id);
  94. const isAudioOnly = Boolean(state['features/base/audio-only'].enabled);
  95. let isPlayable = false;
  96. if (isLocal) {
  97. const isVideoMuted = isLocalTrackMuted(tracks, MEDIA_TYPE.VIDEO);
  98. isPlayable = Boolean(videoTrack) && !isVideoMuted && !isAudioOnly;
  99. } else if (!participant?.isFakeParticipant) { // remote participants excluding shared video
  100. const isVideoMuted = isRemoteTrackMuted(tracks, MEDIA_TYPE.VIDEO, id);
  101. isPlayable = Boolean(videoTrack) && !isVideoMuted && !isAudioOnly
  102. && connectionStatus === JitsiParticipantConnectionStatus.ACTIVE;
  103. }
  104. return isPlayable;
  105. }
  106. /**
  107. * Calculates the size for thumbnails when in horizontal view layout.
  108. *
  109. * @param {number} clientHeight - The height of the app window.
  110. * @returns {{local: {height, width}, remote: {height, width}}}
  111. */
  112. export function calculateThumbnailSizeForHorizontalView(clientHeight: number = 0) {
  113. const topBottomMargin = 15;
  114. const availableHeight = Math.min(clientHeight, (interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120) + topBottomMargin);
  115. const height = availableHeight - topBottomMargin;
  116. return {
  117. local: {
  118. height,
  119. width: Math.floor(interfaceConfig.LOCAL_THUMBNAIL_RATIO * height)
  120. },
  121. remote: {
  122. height,
  123. width: Math.floor(interfaceConfig.REMOTE_THUMBNAIL_RATIO * height)
  124. }
  125. };
  126. }
  127. /**
  128. * Calculates the size for thumbnails when in vertical view layout.
  129. *
  130. * @param {number} clientWidth - The height of the app window.
  131. * @returns {{local: {height, width}, remote: {height, width}}}
  132. */
  133. export function calculateThumbnailSizeForVerticalView(clientWidth: number = 0) {
  134. const horizontalMargin
  135. = VERTICAL_FILMSTRIP_MIN_HORIZONTAL_MARGIN + SCROLL_SIZE
  136. + TILE_HORIZONTAL_MARGIN + STAGE_VIEW_THUMBNAIL_HORIZONTAL_BORDER;
  137. const availableWidth = Math.min(
  138. Math.max(clientWidth - horizontalMargin, 0),
  139. interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120);
  140. return {
  141. local: {
  142. height: Math.floor(availableWidth / interfaceConfig.LOCAL_THUMBNAIL_RATIO),
  143. width: availableWidth
  144. },
  145. remote: {
  146. height: Math.floor(availableWidth / interfaceConfig.REMOTE_THUMBNAIL_RATIO),
  147. width: availableWidth
  148. }
  149. };
  150. }
  151. /**
  152. * Calculates the size for thumbnails when in tile view layout.
  153. *
  154. * @param {Object} dimensions - The desired dimensions of the tile view grid.
  155. * @returns {{hasScroll, height, width}}
  156. */
  157. export function calculateThumbnailSizeForTileView({
  158. columns,
  159. minVisibleRows,
  160. rows,
  161. clientWidth,
  162. clientHeight,
  163. disableResponsiveTiles
  164. }: Object) {
  165. let aspectRatio = TILE_ASPECT_RATIO;
  166. if (!disableResponsiveTiles && clientWidth < ASPECT_RATIO_BREAKPOINT) {
  167. aspectRatio = SQUARE_TILE_ASPECT_RATIO;
  168. }
  169. const viewWidth = clientWidth - (columns * TILE_HORIZONTAL_MARGIN);
  170. const viewHeight = clientHeight - (minVisibleRows * TILE_VERTICAL_MARGIN);
  171. const initialWidth = viewWidth / columns;
  172. const initialHeight = viewHeight / minVisibleRows;
  173. const aspectRatioHeight = initialWidth / aspectRatio;
  174. const noScrollHeight = (clientHeight / rows) - TILE_VERTICAL_MARGIN;
  175. const scrollInitialWidth = (viewWidth - SCROLL_SIZE) / columns;
  176. let height = Math.floor(Math.min(aspectRatioHeight, initialHeight));
  177. let width = Math.floor(aspectRatio * height);
  178. if (height > noScrollHeight && width > scrollInitialWidth) { // we will have scroll and we need more space for it.
  179. const scrollAspectRatioHeight = scrollInitialWidth / aspectRatio;
  180. // Recalculating width/height to fit the available space when a scroll is displayed.
  181. // NOTE: Math.min(scrollAspectRatioHeight, initialHeight) would be enough to recalculate but since the new
  182. // height value can theoretically be dramatically smaller and the scroll may not be neccessary anymore we need
  183. // to compare it with noScrollHeight( the optimal height to fit all thumbnails without scroll) and get the
  184. // bigger one. This way we ensure that we always strech the thumbnails as close as we can to the edges of the
  185. // window.
  186. height = Math.floor(Math.max(Math.min(scrollAspectRatioHeight, initialHeight), noScrollHeight));
  187. width = Math.floor(aspectRatio * height);
  188. }
  189. return {
  190. height,
  191. width
  192. };
  193. }
  194. /**
  195. * Returns the width of the visible area (doesn't include the left margin/padding) of the the vertical filmstrip.
  196. *
  197. * @returns {number} - The width of the vertical filmstrip.
  198. */
  199. export function getVerticalFilmstripVisibleAreaWidth() {
  200. // Adding 11px for the 2px right margin, 2px borders on the left and right and 5px right padding.
  201. // Also adding 7px for the scrollbar. Note that we are not counting the left margins and paddings because this
  202. // function is used for calculating the available space and they are invisible.
  203. // TODO: Check if we can remove the left margins and paddings from the CSS.
  204. // FIXME: This function is used to calculate the size of the large video, etherpad or shared video. Once everything
  205. // is reactified this calculation will need to move to the corresponding components.
  206. const filmstripMaxWidth = (interfaceConfig.FILM_STRIP_MAX_HEIGHT || 120) + 18;
  207. return Math.min(filmstripMaxWidth, window.innerWidth);
  208. }
  209. /**
  210. * Computes information that determine the display mode.
  211. *
  212. * @param {Object} input - Obejct containing all necessary information for determining the display mode for
  213. * the thumbnail.
  214. * @returns {number} - One of <tt>DISPLAY_VIDEO</tt>, <tt>DISPLAY_AVATAR</tt> or <tt>DISPLAY_BLACKNESS_WITH_NAME</tt>.
  215. */
  216. export function computeDisplayMode(input: Object) {
  217. const {
  218. isAudioOnly,
  219. isCurrentlyOnLargeVideo,
  220. isScreenSharing,
  221. canPlayEventReceived,
  222. isHovered,
  223. isRemoteParticipant,
  224. tileViewActive
  225. } = input;
  226. const adjustedIsVideoPlayable = input.isVideoPlayable && (!isRemoteParticipant || canPlayEventReceived);
  227. if (!tileViewActive && isScreenSharing && isRemoteParticipant) {
  228. return isHovered ? DISPLAY_AVATAR_WITH_NAME : DISPLAY_AVATAR;
  229. } else if (isCurrentlyOnLargeVideo && !tileViewActive) {
  230. // Display name is always and only displayed when user is on the stage
  231. return adjustedIsVideoPlayable && !isAudioOnly ? DISPLAY_BLACKNESS_WITH_NAME : DISPLAY_AVATAR_WITH_NAME;
  232. } else if (adjustedIsVideoPlayable && !isAudioOnly) {
  233. // check hovering and change state to video with name
  234. return isHovered ? DISPLAY_VIDEO_WITH_NAME : DISPLAY_VIDEO;
  235. }
  236. // check hovering and change state to avatar with name
  237. return isHovered ? DISPLAY_AVATAR_WITH_NAME : DISPLAY_AVATAR;
  238. }