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.

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