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.

VideoLayout.js 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /* global APP */
  2. import Logger from 'jitsi-meet-logger';
  3. import { MEDIA_TYPE, VIDEO_TYPE } from '../../../react/features/base/media';
  4. import {
  5. getPinnedParticipant,
  6. getParticipantById
  7. } from '../../../react/features/base/participants';
  8. import { getTrackByMediaTypeAndParticipant } from '../../../react/features/base/tracks';
  9. import LargeVideoManager from './LargeVideoManager';
  10. import { VIDEO_CONTAINER_TYPE } from './VideoContainer';
  11. const logger = Logger.getLogger(__filename);
  12. let largeVideo;
  13. const VideoLayout = {
  14. /**
  15. * Handler for local flip X changed event.
  16. */
  17. onLocalFlipXChanged() {
  18. if (largeVideo) {
  19. const { store } = APP;
  20. const { localFlipX } = store.getState()['features/base/settings'];
  21. largeVideo.onLocalFlipXChange(localFlipX);
  22. }
  23. },
  24. /**
  25. * Cleans up state of this singleton {@code VideoLayout}.
  26. *
  27. * @returns {void}
  28. */
  29. reset() {
  30. this._resetLargeVideo();
  31. },
  32. initLargeVideo() {
  33. this._resetLargeVideo();
  34. largeVideo = new LargeVideoManager();
  35. const { store } = APP;
  36. const { localFlipX } = store.getState()['features/base/settings'];
  37. if (typeof localFlipX === 'boolean') {
  38. largeVideo.onLocalFlipXChange(localFlipX);
  39. }
  40. largeVideo.updateContainerSize();
  41. },
  42. /**
  43. * Sets the audio level of the video elements associated to the given id.
  44. *
  45. * @param id the video identifier in the form it comes from the library
  46. * @param lvl the new audio level to update to
  47. */
  48. setAudioLevel(id, lvl) {
  49. if (largeVideo && id === largeVideo.id) {
  50. largeVideo.updateLargeVideoAudioLevel(lvl);
  51. }
  52. },
  53. /**
  54. * FIXME get rid of this method once muted indicator are reactified (by
  55. * making sure that user with no tracks is displayed as muted )
  56. *
  57. * If participant has no tracks will make the UI display muted status.
  58. * @param {string} participantId
  59. */
  60. updateVideoMutedForNoTracks(participantId) {
  61. const participant = APP.conference.getParticipantById(participantId);
  62. if (participant && !participant.getTracksByMediaType('video').length) {
  63. VideoLayout._updateLargeVideoIfDisplayed(participantId, true);
  64. }
  65. },
  66. /**
  67. * Return the type of the remote video.
  68. * @param id the id for the remote video
  69. * @returns {String} the video type video or screen.
  70. */
  71. getRemoteVideoType(id) {
  72. const state = APP.store.getState();
  73. const participant = getParticipantById(state, id);
  74. if (participant?.isFakeParticipant) {
  75. return VIDEO_TYPE.CAMERA;
  76. }
  77. const videoTrack = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  78. return videoTrack?.videoType;
  79. },
  80. getPinnedId() {
  81. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  82. return id || null;
  83. },
  84. /**
  85. * Shows/hides warning about a user's connectivity issues.
  86. *
  87. * @param {string} id - The ID of the remote participant(MUC nickname).
  88. * @returns {void}
  89. */
  90. onParticipantConnectionStatusChanged(id) {
  91. if (APP.conference.isLocalId(id)) {
  92. return;
  93. }
  94. // We have to trigger full large video update to transition from
  95. // avatar to video on connectivity restored.
  96. this._updateLargeVideoIfDisplayed(id, true);
  97. },
  98. /**
  99. * On last N change event.
  100. *
  101. * @param endpointsLeavingLastN the list currently leaving last N
  102. * endpoints
  103. * @param endpointsEnteringLastN the list currently entering last N
  104. * endpoints
  105. */
  106. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  107. if (endpointsLeavingLastN) {
  108. endpointsLeavingLastN.forEach(this._updateLargeVideoIfDisplayed, this);
  109. }
  110. if (endpointsEnteringLastN) {
  111. endpointsEnteringLastN.forEach(this._updateLargeVideoIfDisplayed, this);
  112. }
  113. },
  114. /**
  115. * Resizes the video area.
  116. */
  117. resizeVideoArea() {
  118. if (largeVideo) {
  119. largeVideo.updateContainerSize();
  120. largeVideo.resize(false);
  121. }
  122. },
  123. changeUserAvatar(id, avatarUrl) {
  124. if (this.isCurrentlyOnLarge(id)) {
  125. largeVideo.updateAvatar(avatarUrl);
  126. }
  127. },
  128. isLargeVideoVisible() {
  129. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  130. },
  131. /**
  132. * @return {LargeContainer} the currently displayed container on large
  133. * video.
  134. */
  135. getCurrentlyOnLargeContainer() {
  136. return largeVideo.getCurrentContainer();
  137. },
  138. isCurrentlyOnLarge(id) {
  139. return largeVideo && largeVideo.id === id;
  140. },
  141. updateLargeVideo(id, forceUpdate) {
  142. if (!largeVideo) {
  143. return;
  144. }
  145. const currentContainer = largeVideo.getCurrentContainer();
  146. const currentContainerType = largeVideo.getCurrentContainerType();
  147. const isOnLarge = this.isCurrentlyOnLarge(id);
  148. const state = APP.store.getState();
  149. const videoTrack = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  150. const videoStream = videoTrack?.jitsiTrack;
  151. if (isOnLarge && !forceUpdate
  152. && LargeVideoManager.isVideoContainer(currentContainerType)
  153. && videoStream) {
  154. const currentStreamId = currentContainer.getStreamID();
  155. const newStreamId = videoStream?.getId() || null;
  156. // FIXME it might be possible to get rid of 'forceUpdate' argument
  157. if (currentStreamId !== newStreamId) {
  158. logger.debug('Enforcing large video update for stream change');
  159. forceUpdate = true; // eslint-disable-line no-param-reassign
  160. }
  161. }
  162. if (!isOnLarge || forceUpdate) {
  163. const videoType = this.getRemoteVideoType(id);
  164. largeVideo.updateLargeVideo(
  165. id,
  166. videoStream,
  167. videoType || VIDEO_TYPE.CAMERA
  168. ).catch(() => {
  169. // do nothing
  170. });
  171. }
  172. },
  173. addLargeVideoContainer(type, container) {
  174. largeVideo && largeVideo.addContainer(type, container);
  175. },
  176. removeLargeVideoContainer(type) {
  177. largeVideo && largeVideo.removeContainer(type);
  178. },
  179. /**
  180. * @returns Promise
  181. */
  182. showLargeVideoContainer(type, show) {
  183. if (!largeVideo) {
  184. return Promise.reject();
  185. }
  186. const isVisible = this.isLargeContainerTypeVisible(type);
  187. if (isVisible === show) {
  188. return Promise.resolve();
  189. }
  190. let containerTypeToShow = type;
  191. // if we are hiding a container and there is focusedVideo
  192. // (pinned remote video) use its video type,
  193. // if not then use default type - large video
  194. if (!show) {
  195. const pinnedId = this.getPinnedId();
  196. if (pinnedId) {
  197. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  198. } else {
  199. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  200. }
  201. }
  202. return largeVideo.showContainer(containerTypeToShow);
  203. },
  204. isLargeContainerTypeVisible(type) {
  205. return largeVideo && largeVideo.state === type;
  206. },
  207. /**
  208. * Returns the id of the current video shown on large.
  209. * Currently used by tests (torture).
  210. */
  211. getLargeVideoID() {
  212. return largeVideo && largeVideo.id;
  213. },
  214. /**
  215. * Returns the the current video shown on large.
  216. * Currently used by tests (torture).
  217. */
  218. getLargeVideo() {
  219. return largeVideo;
  220. },
  221. /**
  222. * Returns the wrapper jquery selector for the largeVideo
  223. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  224. */
  225. getLargeVideoWrapper() {
  226. return this.getCurrentlyOnLargeContainer().$wrapper;
  227. },
  228. /**
  229. * Helper method to invoke when the video layout has changed and elements
  230. * have to be re-arranged and resized.
  231. *
  232. * @returns {void}
  233. */
  234. refreshLayout() {
  235. VideoLayout.resizeVideoArea();
  236. },
  237. /**
  238. * Cleans up any existing largeVideo instance.
  239. *
  240. * @private
  241. * @returns {void}
  242. */
  243. _resetLargeVideo() {
  244. if (largeVideo) {
  245. largeVideo.destroy();
  246. }
  247. largeVideo = null;
  248. },
  249. /**
  250. * Triggers an update of large video if the passed in participant is
  251. * currently displayed on large video.
  252. *
  253. * @param {string} participantId - The participant ID that should trigger an
  254. * update of large video if displayed.
  255. * @param {boolean} force - Whether or not the large video update should
  256. * happen no matter what.
  257. * @returns {void}
  258. */
  259. _updateLargeVideoIfDisplayed(participantId, force = false) {
  260. if (this.isCurrentlyOnLarge(participantId)) {
  261. this.updateLargeVideo(participantId, force);
  262. }
  263. },
  264. /**
  265. * Handles window resizes.
  266. */
  267. onResize() {
  268. VideoLayout.resizeVideoArea();
  269. }
  270. };
  271. export default VideoLayout;