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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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, forceStreamToReattach = false) {
  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 (videoStream && forceStreamToReattach) {
  166. videoStream.forceStreamToReattach = forceStreamToReattach;
  167. }
  168. if (isOnLarge && !forceUpdate
  169. && LargeVideoManager.isVideoContainer(currentContainerType)
  170. && videoStream) {
  171. const currentStreamId = currentContainer.getStreamID();
  172. const newStreamId = videoStream?.getId() || null;
  173. // FIXME it might be possible to get rid of 'forceUpdate' argument
  174. if (currentStreamId !== newStreamId) {
  175. logger.debug('Enforcing large video update for stream change');
  176. forceUpdate = true; // eslint-disable-line no-param-reassign
  177. }
  178. }
  179. if (!isOnLarge || forceUpdate) {
  180. const videoType = this.getRemoteVideoType(id);
  181. largeVideo.updateLargeVideo(
  182. id,
  183. videoStream,
  184. videoType || VIDEO_TYPE.CAMERA
  185. ).catch(() => {
  186. // do nothing
  187. });
  188. }
  189. },
  190. addLargeVideoContainer(type, container) {
  191. largeVideo && largeVideo.addContainer(type, container);
  192. },
  193. removeLargeVideoContainer(type) {
  194. largeVideo && largeVideo.removeContainer(type);
  195. },
  196. /**
  197. * @returns Promise
  198. */
  199. showLargeVideoContainer(type, show) {
  200. if (!largeVideo) {
  201. return Promise.reject();
  202. }
  203. const isVisible = this.isLargeContainerTypeVisible(type);
  204. if (isVisible === show) {
  205. return Promise.resolve();
  206. }
  207. let containerTypeToShow = type;
  208. // if we are hiding a container and there is focusedVideo
  209. // (pinned remote video) use its video type,
  210. // if not then use default type - large video
  211. if (!show) {
  212. const pinnedId = this.getPinnedId();
  213. if (pinnedId) {
  214. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  215. } else {
  216. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  217. }
  218. }
  219. return largeVideo.showContainer(containerTypeToShow);
  220. },
  221. isLargeContainerTypeVisible(type) {
  222. return largeVideo && largeVideo.state === type;
  223. },
  224. /**
  225. * Returns the id of the current video shown on large.
  226. * Currently used by tests (torture).
  227. */
  228. getLargeVideoID() {
  229. return largeVideo && largeVideo.id;
  230. },
  231. /**
  232. * Returns the the current video shown on large.
  233. * Currently used by tests (torture).
  234. */
  235. getLargeVideo() {
  236. return largeVideo;
  237. },
  238. /**
  239. * Returns the wrapper jquery selector for the largeVideo
  240. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  241. */
  242. getLargeVideoWrapper() {
  243. return this.getCurrentlyOnLargeContainer().$wrapper;
  244. },
  245. /**
  246. * Helper method to invoke when the video layout has changed and elements
  247. * have to be re-arranged and resized.
  248. *
  249. * @returns {void}
  250. */
  251. refreshLayout() {
  252. VideoLayout.resizeVideoArea();
  253. },
  254. /**
  255. * Cleans up any existing largeVideo instance.
  256. *
  257. * @private
  258. * @returns {void}
  259. */
  260. _resetLargeVideo() {
  261. if (largeVideo) {
  262. largeVideo.destroy();
  263. }
  264. largeVideo = null;
  265. },
  266. /**
  267. * Triggers an update of large video if the passed in participant is
  268. * currently displayed on large video.
  269. *
  270. * @param {string} participantId - The participant ID that should trigger an
  271. * update of large video if displayed.
  272. * @param {boolean} force - Whether or not the large video update should
  273. * happen no matter what.
  274. * @returns {void}
  275. */
  276. _updateLargeVideoIfDisplayed(participantId, force = false) {
  277. if (this.isCurrentlyOnLarge(participantId)) {
  278. this.updateLargeVideo(participantId, force, false);
  279. }
  280. },
  281. /**
  282. * Handles window resizes.
  283. */
  284. onResize() {
  285. VideoLayout.resizeVideoArea();
  286. }
  287. };
  288. export default VideoLayout;