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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  1. /* global APP */
  2. import Logger from 'jitsi-meet-logger';
  3. import { MEDIA_TYPE, VIDEO_TYPE } from '../../../react/features/base/media';
  4. import {
  5. getLocalParticipant as getLocalParticipantFromStore,
  6. getPinnedParticipant,
  7. getParticipantById,
  8. pinParticipant
  9. } from '../../../react/features/base/participants';
  10. import { getTrackByMediaTypeAndParticipant } from '../../../react/features/base/tracks';
  11. import UIEvents from '../../../service/UI/UIEvents';
  12. import { SHARED_VIDEO_CONTAINER_TYPE } from '../shared_video/SharedVideo';
  13. import SharedVideoThumb from '../shared_video/SharedVideoThumb';
  14. import LargeVideoManager from './LargeVideoManager';
  15. import LocalVideo from './LocalVideo';
  16. import RemoteVideo from './RemoteVideo';
  17. import { VIDEO_CONTAINER_TYPE } from './VideoContainer';
  18. const logger = Logger.getLogger(__filename);
  19. const remoteVideos = {};
  20. let localVideoThumbnail = null;
  21. let eventEmitter = null;
  22. let largeVideo;
  23. /**
  24. * flipX state of the localVideo
  25. */
  26. let localFlipX = null;
  27. /**
  28. * Handler for local flip X changed event.
  29. * @param {Object} val
  30. */
  31. function onLocalFlipXChanged(val) {
  32. localFlipX = val;
  33. if (largeVideo) {
  34. largeVideo.onLocalFlipXChange(val);
  35. }
  36. }
  37. /**
  38. * Returns an array of all thumbnails in the filmstrip.
  39. *
  40. * @private
  41. * @returns {Array}
  42. */
  43. function getAllThumbnails() {
  44. return [
  45. ...localVideoThumbnail ? [ localVideoThumbnail ] : [],
  46. ...Object.values(remoteVideos)
  47. ];
  48. }
  49. /**
  50. * Private helper to get the redux representation of the local participant.
  51. *
  52. * @private
  53. * @returns {Object}
  54. */
  55. function getLocalParticipant() {
  56. return getLocalParticipantFromStore(APP.store.getState());
  57. }
  58. const VideoLayout = {
  59. init(emitter) {
  60. eventEmitter = emitter;
  61. localVideoThumbnail = new LocalVideo(
  62. emitter,
  63. this._updateLargeVideoIfDisplayed.bind(this));
  64. this.registerListeners();
  65. },
  66. /**
  67. * Registering listeners for UI events in Video layout component.
  68. *
  69. * @returns {void}
  70. */
  71. registerListeners() {
  72. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED,
  73. onLocalFlipXChanged);
  74. },
  75. /**
  76. * Cleans up state of this singleton {@code VideoLayout}.
  77. *
  78. * @returns {void}
  79. */
  80. reset() {
  81. this._resetLargeVideo();
  82. this._resetFilmstrip();
  83. },
  84. initLargeVideo() {
  85. this._resetLargeVideo();
  86. largeVideo = new LargeVideoManager(eventEmitter);
  87. if (localFlipX) {
  88. largeVideo.onLocalFlipXChange(localFlipX);
  89. }
  90. largeVideo.updateContainerSize();
  91. },
  92. /**
  93. * Sets the audio level of the video elements associated to the given id.
  94. *
  95. * @param id the video identifier in the form it comes from the library
  96. * @param lvl the new audio level to update to
  97. */
  98. setAudioLevel(id, lvl) {
  99. if (largeVideo && id === largeVideo.id) {
  100. largeVideo.updateLargeVideoAudioLevel(lvl);
  101. }
  102. },
  103. changeLocalVideo(stream) {
  104. const localId = getLocalParticipant().id;
  105. this.onVideoTypeChanged(localId, stream.videoType);
  106. localVideoThumbnail.changeVideo(stream);
  107. this._updateLargeVideoIfDisplayed(localId);
  108. },
  109. /**
  110. * Shows/hides local video.
  111. * @param {boolean} true to make the local video visible, false - otherwise
  112. */
  113. setLocalVideoVisible(visible) {
  114. localVideoThumbnail.setVisible(visible);
  115. },
  116. onRemoteStreamAdded(stream) {
  117. const id = stream.getParticipantId();
  118. const remoteVideo = remoteVideos[id];
  119. logger.debug(`Received a new ${stream.getType()} stream for ${id}`);
  120. if (!remoteVideo) {
  121. logger.debug('No remote video element to add stream');
  122. return;
  123. }
  124. remoteVideo.addRemoteStreamElement(stream);
  125. this.onVideoMute(id);
  126. remoteVideo.updateView();
  127. },
  128. onRemoteStreamRemoved(stream) {
  129. const id = stream.getParticipantId();
  130. const remoteVideo = remoteVideos[id];
  131. // Remote stream may be removed after participant left the conference.
  132. if (remoteVideo) {
  133. remoteVideo.removeRemoteStreamElement(stream);
  134. remoteVideo.updateView();
  135. }
  136. this.updateVideoMutedForNoTracks(id);
  137. },
  138. /**
  139. * FIXME get rid of this method once muted indicator are reactified (by
  140. * making sure that user with no tracks is displayed as muted )
  141. *
  142. * If participant has no tracks will make the UI display muted status.
  143. * @param {string} participantId
  144. */
  145. updateVideoMutedForNoTracks(participantId) {
  146. const participant = APP.conference.getParticipantById(participantId);
  147. if (participant && !participant.getTracksByMediaType('video').length) {
  148. APP.UI.setVideoMuted(participantId);
  149. }
  150. },
  151. /**
  152. * Return the type of the remote video.
  153. * @param id the id for the remote video
  154. * @returns {String} the video type video or screen.
  155. */
  156. getRemoteVideoType(id) {
  157. const state = APP.store.getState();
  158. const participant = getParticipantById(state, id);
  159. if (participant?.isFakeParticipant) {
  160. return SHARED_VIDEO_CONTAINER_TYPE;
  161. }
  162. const videoTrack = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  163. return videoTrack?.videoType;
  164. },
  165. isPinned(id) {
  166. return id === this.getPinnedId();
  167. },
  168. getPinnedId() {
  169. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  170. return id || null;
  171. },
  172. /**
  173. * Triggers a thumbnail to pin or unpin itself.
  174. *
  175. * @param {number} videoNumber - The index of the video to toggle pin on.
  176. * @private
  177. */
  178. togglePin(videoNumber) {
  179. const videos = getAllThumbnails();
  180. const videoView = videos[videoNumber];
  181. videoView && videoView.togglePin();
  182. },
  183. /**
  184. * Callback invoked to update display when the pin participant has changed.
  185. *
  186. * @paramn {string|null} pinnedParticipantID - The participant ID of the
  187. * participant that is pinned or null if no one is pinned.
  188. * @returns {void}
  189. */
  190. onPinChange(pinnedParticipantID) {
  191. getAllThumbnails().forEach(thumbnail =>
  192. thumbnail.focus(pinnedParticipantID === thumbnail.getId()));
  193. },
  194. /**
  195. * Creates a participant container for the given id.
  196. *
  197. * @param {Object} participant - The redux representation of a remote
  198. * participant.
  199. * @returns {void}
  200. */
  201. addRemoteParticipantContainer(participant) {
  202. if (!participant || participant.local) {
  203. return;
  204. } else if (participant.isFakeParticipant) {
  205. const sharedVideoThumb = new SharedVideoThumb(participant);
  206. this.addRemoteVideoContainer(participant.id, sharedVideoThumb);
  207. return;
  208. }
  209. const id = participant.id;
  210. const jitsiParticipant = APP.conference.getParticipantById(id);
  211. const remoteVideo = new RemoteVideo(jitsiParticipant);
  212. this.addRemoteVideoContainer(id, remoteVideo);
  213. this.updateVideoMutedForNoTracks(id);
  214. },
  215. /**
  216. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  217. *
  218. * @param {string} the id of the video to add
  219. * @param {SmallVideo} smallVideo the small video instance to add as a
  220. * remote video
  221. */
  222. addRemoteVideoContainer(id, remoteVideo) {
  223. remoteVideos[id] = remoteVideo;
  224. // Initialize the view
  225. remoteVideo.updateView();
  226. },
  227. /**
  228. * On video muted event.
  229. */
  230. onVideoMute(id) {
  231. if (APP.conference.isLocalId(id)) {
  232. localVideoThumbnail && localVideoThumbnail.updateView();
  233. } else {
  234. const remoteVideo = remoteVideos[id];
  235. if (remoteVideo) {
  236. remoteVideo.updateView();
  237. }
  238. }
  239. // large video will show avatar instead of muted stream
  240. this._updateLargeVideoIfDisplayed(id, true);
  241. },
  242. /**
  243. * On dominant speaker changed event.
  244. *
  245. * @param {string} id - The participant ID of the new dominant speaker.
  246. * @returns {void}
  247. */
  248. onDominantSpeakerChanged(id) {
  249. getAllThumbnails().forEach(thumbnail =>
  250. thumbnail.showDominantSpeakerIndicator(id === thumbnail.getId()));
  251. },
  252. /**
  253. * Shows/hides warning about a user's connectivity issues.
  254. *
  255. * @param {string} id - The ID of the remote participant(MUC nickname).
  256. * @returns {void}
  257. */
  258. onParticipantConnectionStatusChanged(id) {
  259. if (APP.conference.isLocalId(id)) {
  260. return;
  261. }
  262. // We have to trigger full large video update to transition from
  263. // avatar to video on connectivity restored.
  264. this._updateLargeVideoIfDisplayed(id, true);
  265. const remoteVideo = remoteVideos[id];
  266. if (remoteVideo) {
  267. remoteVideo.updateView();
  268. }
  269. },
  270. /**
  271. * On last N change event.
  272. *
  273. * @param endpointsLeavingLastN the list currently leaving last N
  274. * endpoints
  275. * @param endpointsEnteringLastN the list currently entering last N
  276. * endpoints
  277. */
  278. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  279. if (endpointsLeavingLastN) {
  280. endpointsLeavingLastN.forEach(this._updateRemoteVideo, this);
  281. }
  282. if (endpointsEnteringLastN) {
  283. endpointsEnteringLastN.forEach(this._updateRemoteVideo, this);
  284. }
  285. },
  286. /**
  287. * Updates remote video by id if it exists.
  288. * @param {string} id of the remote video
  289. * @private
  290. */
  291. _updateRemoteVideo(id) {
  292. const remoteVideo = remoteVideos[id];
  293. if (remoteVideo) {
  294. remoteVideo.updateView();
  295. this._updateLargeVideoIfDisplayed(id);
  296. }
  297. },
  298. removeParticipantContainer(id) {
  299. // Unlock large video
  300. if (this.getPinnedId() === id) {
  301. logger.info('Focused video owner has left the conference');
  302. APP.store.dispatch(pinParticipant(null));
  303. }
  304. const remoteVideo = remoteVideos[id];
  305. if (remoteVideo) {
  306. // Remove remote video
  307. logger.info(`Removing remote video: ${id}`);
  308. delete remoteVideos[id];
  309. remoteVideo.remove();
  310. } else {
  311. logger.warn(`No remote video for ${id}`);
  312. }
  313. },
  314. onVideoTypeChanged(id, newVideoType) {
  315. const remoteVideo = remoteVideos[id];
  316. if (!remoteVideo) {
  317. return;
  318. }
  319. logger.info('Peer video type changed: ', id, newVideoType);
  320. remoteVideo.updateView();
  321. },
  322. /**
  323. * Resizes the video area.
  324. */
  325. resizeVideoArea() {
  326. if (largeVideo) {
  327. largeVideo.updateContainerSize();
  328. largeVideo.resize(false);
  329. }
  330. },
  331. getSmallVideo(id) {
  332. if (APP.conference.isLocalId(id)) {
  333. return localVideoThumbnail;
  334. }
  335. return remoteVideos[id];
  336. },
  337. changeUserAvatar(id, avatarUrl) {
  338. if (this.isCurrentlyOnLarge(id)) {
  339. largeVideo.updateAvatar(avatarUrl);
  340. }
  341. },
  342. isLargeVideoVisible() {
  343. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  344. },
  345. /**
  346. * @return {LargeContainer} the currently displayed container on large
  347. * video.
  348. */
  349. getCurrentlyOnLargeContainer() {
  350. return largeVideo.getCurrentContainer();
  351. },
  352. isCurrentlyOnLarge(id) {
  353. return largeVideo && largeVideo.id === id;
  354. },
  355. /**
  356. * Triggers an update of remote video and large video displays so they may
  357. * pick up any state changes that have occurred elsewhere.
  358. *
  359. * @returns {void}
  360. */
  361. updateAllVideos() {
  362. const displayedUserId = this.getLargeVideoID();
  363. if (displayedUserId) {
  364. this.updateLargeVideo(displayedUserId, true);
  365. }
  366. Object.keys(remoteVideos).forEach(video => {
  367. remoteVideos[video].updateView();
  368. });
  369. },
  370. updateLargeVideo(id, forceUpdate) {
  371. if (!largeVideo) {
  372. return;
  373. }
  374. const currentContainer = largeVideo.getCurrentContainer();
  375. const currentContainerType = largeVideo.getCurrentContainerType();
  376. const isOnLarge = this.isCurrentlyOnLarge(id);
  377. const state = APP.store.getState();
  378. const videoTrack = getTrackByMediaTypeAndParticipant(state['features/base/tracks'], MEDIA_TYPE.VIDEO, id);
  379. const videoStream = videoTrack?.jitsiTrack;
  380. if (isOnLarge && !forceUpdate
  381. && LargeVideoManager.isVideoContainer(currentContainerType)
  382. && videoStream) {
  383. const currentStreamId = currentContainer.getStreamID();
  384. const newStreamId = videoStream?.getId() || null;
  385. // FIXME it might be possible to get rid of 'forceUpdate' argument
  386. if (currentStreamId !== newStreamId) {
  387. logger.debug('Enforcing large video update for stream change');
  388. forceUpdate = true; // eslint-disable-line no-param-reassign
  389. }
  390. }
  391. if (!isOnLarge || forceUpdate) {
  392. const videoType = this.getRemoteVideoType(id);
  393. largeVideo.updateLargeVideo(
  394. id,
  395. videoStream,
  396. videoType || VIDEO_TYPE.CAMERA
  397. ).catch(() => {
  398. // do nothing
  399. });
  400. }
  401. },
  402. addLargeVideoContainer(type, container) {
  403. largeVideo && largeVideo.addContainer(type, container);
  404. },
  405. removeLargeVideoContainer(type) {
  406. largeVideo && largeVideo.removeContainer(type);
  407. },
  408. /**
  409. * @returns Promise
  410. */
  411. showLargeVideoContainer(type, show) {
  412. if (!largeVideo) {
  413. return Promise.reject();
  414. }
  415. const isVisible = this.isLargeContainerTypeVisible(type);
  416. if (isVisible === show) {
  417. return Promise.resolve();
  418. }
  419. const currentId = largeVideo.id;
  420. let oldSmallVideo;
  421. if (currentId) {
  422. oldSmallVideo = this.getSmallVideo(currentId);
  423. }
  424. let containerTypeToShow = type;
  425. // if we are hiding a container and there is focusedVideo
  426. // (pinned remote video) use its video type,
  427. // if not then use default type - large video
  428. if (!show) {
  429. const pinnedId = this.getPinnedId();
  430. if (pinnedId) {
  431. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  432. } else {
  433. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  434. }
  435. }
  436. return largeVideo.showContainer(containerTypeToShow)
  437. .then(() => {
  438. if (oldSmallVideo) {
  439. oldSmallVideo && oldSmallVideo.updateView();
  440. }
  441. });
  442. },
  443. isLargeContainerTypeVisible(type) {
  444. return largeVideo && largeVideo.state === type;
  445. },
  446. /**
  447. * Returns the id of the current video shown on large.
  448. * Currently used by tests (torture).
  449. */
  450. getLargeVideoID() {
  451. return largeVideo && largeVideo.id;
  452. },
  453. /**
  454. * Returns the the current video shown on large.
  455. * Currently used by tests (torture).
  456. */
  457. getLargeVideo() {
  458. return largeVideo;
  459. },
  460. /**
  461. * Sets the flipX state of the local video.
  462. * @param {boolean} true for flipped otherwise false;
  463. */
  464. setLocalFlipX(val) {
  465. this.localFlipX = val;
  466. },
  467. /**
  468. * Returns the wrapper jquery selector for the largeVideo
  469. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  470. */
  471. getLargeVideoWrapper() {
  472. return this.getCurrentlyOnLargeContainer().$wrapper;
  473. },
  474. /**
  475. * Returns the number of remove video ids.
  476. *
  477. * @returns {number} The number of remote videos.
  478. */
  479. getRemoteVideosCount() {
  480. return Object.keys(remoteVideos).length;
  481. },
  482. /**
  483. * Helper method to invoke when the video layout has changed and elements
  484. * have to be re-arranged and resized.
  485. *
  486. * @returns {void}
  487. */
  488. refreshLayout() {
  489. localVideoThumbnail && localVideoThumbnail.updateDOMLocation();
  490. VideoLayout.resizeVideoArea();
  491. // Rerender the thumbnails since they are dependent on the layout because of the tooltip positioning.
  492. localVideoThumbnail && localVideoThumbnail.rerender();
  493. Object.values(remoteVideos).forEach(remoteVideoThumbnail => remoteVideoThumbnail.rerender());
  494. },
  495. /**
  496. * Cleans up any existing largeVideo instance.
  497. *
  498. * @private
  499. * @returns {void}
  500. */
  501. _resetLargeVideo() {
  502. if (largeVideo) {
  503. largeVideo.destroy();
  504. }
  505. largeVideo = null;
  506. },
  507. /**
  508. * Cleans up filmstrip state. While a separate {@code Filmstrip} exists, its
  509. * implementation is mainly for querying and manipulating the DOM while
  510. * state mostly remains in {@code VideoLayout}.
  511. *
  512. * @private
  513. * @returns {void}
  514. */
  515. _resetFilmstrip() {
  516. Object.keys(remoteVideos).forEach(remoteVideoId => {
  517. this.removeParticipantContainer(remoteVideoId);
  518. delete remoteVideos[remoteVideoId];
  519. });
  520. if (localVideoThumbnail) {
  521. localVideoThumbnail.remove();
  522. localVideoThumbnail = null;
  523. }
  524. },
  525. /**
  526. * Triggers an update of large video if the passed in participant is
  527. * currently displayed on large video.
  528. *
  529. * @param {string} participantId - The participant ID that should trigger an
  530. * update of large video if displayed.
  531. * @param {boolean} force - Whether or not the large video update should
  532. * happen no matter what.
  533. * @returns {void}
  534. */
  535. _updateLargeVideoIfDisplayed(participantId, force = false) {
  536. if (this.isCurrentlyOnLarge(participantId)) {
  537. this.updateLargeVideo(participantId, force);
  538. }
  539. },
  540. /**
  541. * Handles window resizes.
  542. */
  543. onResize() {
  544. VideoLayout.resizeVideoArea();
  545. }
  546. };
  547. export default VideoLayout;