Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

VideoLayout.js 8.8KB

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