Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

VideoLayout.js 20KB

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