Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

functions.web.js 10KB

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