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.9KB

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