您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

VideoLayout.js 8.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* global APP */
  2. import Logger from '@jitsi/logger';
  3. import { MEDIA_TYPE, VIDEO_TYPE } from '../../../react/features/base/media';
  4. import {
  5. getParticipantById,
  6. getPinnedParticipant,
  7. isScreenShareParticipantById
  8. } from '../../../react/features/base/participants';
  9. import {
  10. getTrackByMediaTypeAndParticipant,
  11. getVideoTrackByParticipant
  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. const isScreenShare = isScreenShareParticipantById(state, id);
  79. if (participant?.fakeParticipant && !isScreenShare) {
  80. return VIDEO_TYPE.CAMERA;
  81. }
  82. if (isScreenShare) {
  83. return VIDEO_TYPE.DESKTOP;
  84. }
  85. const videoTrack = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  86. return videoTrack?.videoType;
  87. },
  88. getPinnedId() {
  89. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  90. return id || null;
  91. },
  92. /**
  93. * On last N change event.
  94. *
  95. * @param endpointsLeavingLastN the list currently leaving last N
  96. * endpoints
  97. * @param endpointsEnteringLastN the list currently entering last N
  98. * endpoints
  99. */
  100. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  101. if (endpointsLeavingLastN) {
  102. endpointsLeavingLastN.forEach(this._updateLargeVideoIfDisplayed, this);
  103. }
  104. if (endpointsEnteringLastN) {
  105. endpointsEnteringLastN.forEach(this._updateLargeVideoIfDisplayed, this);
  106. }
  107. },
  108. /**
  109. * Resizes the video area.
  110. */
  111. resizeVideoArea() {
  112. if (largeVideo) {
  113. largeVideo.updateContainerSize();
  114. largeVideo.resize(false);
  115. }
  116. },
  117. isLargeVideoVisible() {
  118. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  119. },
  120. /**
  121. * @return {LargeContainer} the currently displayed container on large
  122. * video.
  123. */
  124. getCurrentlyOnLargeContainer() {
  125. return largeVideo.getCurrentContainer();
  126. },
  127. isCurrentlyOnLarge(id) {
  128. return largeVideo && largeVideo.id === id;
  129. },
  130. updateLargeVideo(id, forceUpdate, forceStreamToReattach = false) {
  131. if (!largeVideo) {
  132. return;
  133. }
  134. const currentContainer = largeVideo.getCurrentContainer();
  135. const currentContainerType = largeVideo.getCurrentContainerType();
  136. const isOnLarge = this.isCurrentlyOnLarge(id);
  137. const state = APP.store.getState();
  138. const participant = getParticipantById(state, id);
  139. const videoTrack = getVideoTrackByParticipant(state, participant);
  140. const videoStream = videoTrack?.jitsiTrack;
  141. if (videoStream && forceStreamToReattach) {
  142. videoStream.forceStreamToReattach = forceStreamToReattach;
  143. }
  144. if (isOnLarge && !forceUpdate
  145. && LargeVideoManager.isVideoContainer(currentContainerType)
  146. && videoStream) {
  147. const currentStreamId = currentContainer.getStreamID();
  148. const newStreamId = videoStream?.getId() || null;
  149. // FIXME it might be possible to get rid of 'forceUpdate' argument
  150. if (currentStreamId !== newStreamId) {
  151. logger.debug('Enforcing large video update for stream change');
  152. forceUpdate = true; // eslint-disable-line no-param-reassign
  153. }
  154. }
  155. if (!isOnLarge || forceUpdate) {
  156. const videoType = this.getRemoteVideoType(id);
  157. largeVideo.updateLargeVideo(
  158. id,
  159. videoStream,
  160. videoType || VIDEO_TYPE.CAMERA
  161. ).catch(() => {
  162. // do nothing
  163. });
  164. }
  165. },
  166. addLargeVideoContainer(type, container) {
  167. largeVideo && largeVideo.addContainer(type, container);
  168. },
  169. removeLargeVideoContainer(type) {
  170. largeVideo && largeVideo.removeContainer(type);
  171. },
  172. /**
  173. * @returns Promise
  174. */
  175. showLargeVideoContainer(type, show) {
  176. if (!largeVideo) {
  177. return Promise.reject();
  178. }
  179. const isVisible = this.isLargeContainerTypeVisible(type);
  180. if (isVisible === show) {
  181. return Promise.resolve();
  182. }
  183. let containerTypeToShow = type;
  184. // if we are hiding a container and there is focusedVideo
  185. // (pinned remote video) use its video type,
  186. // if not then use default type - large video
  187. if (!show) {
  188. const pinnedId = this.getPinnedId();
  189. if (pinnedId) {
  190. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  191. } else {
  192. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  193. }
  194. }
  195. return largeVideo.showContainer(containerTypeToShow);
  196. },
  197. isLargeContainerTypeVisible(type) {
  198. return largeVideo && largeVideo.state === type;
  199. },
  200. /**
  201. * Returns the id of the current video shown on large.
  202. * Currently used by tests (torture).
  203. */
  204. getLargeVideoID() {
  205. return largeVideo && largeVideo.id;
  206. },
  207. /**
  208. * Returns the the current video shown on large.
  209. * Currently used by tests (torture).
  210. */
  211. getLargeVideo() {
  212. return largeVideo;
  213. },
  214. /**
  215. * Returns the wrapper jquery selector for the largeVideo
  216. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  217. */
  218. getLargeVideoWrapper() {
  219. return this.getCurrentlyOnLargeContainer().$wrapper;
  220. },
  221. /**
  222. * Helper method to invoke when the video layout has changed and elements
  223. * have to be re-arranged and resized.
  224. *
  225. * @returns {void}
  226. */
  227. refreshLayout() {
  228. VideoLayout.resizeVideoArea();
  229. },
  230. /**
  231. * Cleans up any existing largeVideo instance.
  232. *
  233. * @private
  234. * @returns {void}
  235. */
  236. _resetLargeVideo() {
  237. if (largeVideo) {
  238. largeVideo.destroy();
  239. }
  240. largeVideo = null;
  241. },
  242. /**
  243. * Triggers an update of large video if the passed in participant is
  244. * currently displayed on large video.
  245. *
  246. * @param {string} participantId - The participant ID that should trigger an
  247. * update of large video if displayed.
  248. * @param {boolean} force - Whether or not the large video update should
  249. * happen no matter what.
  250. * @returns {void}
  251. */
  252. _updateLargeVideoIfDisplayed(participantId, force = false) {
  253. if (this.isCurrentlyOnLarge(participantId)) {
  254. this.updateLargeVideo(participantId, force, false);
  255. }
  256. },
  257. /**
  258. * Handles window resizes.
  259. */
  260. onResize() {
  261. VideoLayout.resizeVideoArea();
  262. }
  263. };
  264. export default VideoLayout;