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

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