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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /* global APP, $, interfaceConfig */
  2. const logger = require('jitsi-meet-logger').getLogger(__filename);
  3. import {
  4. getNearestReceiverVideoQualityLevel,
  5. setMaxReceiverVideoQuality
  6. } from '../../../react/features/base/conference';
  7. import {
  8. JitsiParticipantConnectionStatus
  9. } from '../../../react/features/base/lib-jitsi-meet';
  10. import { VIDEO_TYPE } from '../../../react/features/base/media';
  11. import {
  12. getPinnedParticipant,
  13. pinParticipant
  14. } from '../../../react/features/base/participants';
  15. import {
  16. shouldDisplayTileView
  17. } from '../../../react/features/video-layout';
  18. import { SHARED_VIDEO_CONTAINER_TYPE } from '../shared_video/SharedVideo';
  19. import SharedVideoThumb from '../shared_video/SharedVideoThumb';
  20. import Filmstrip from './Filmstrip';
  21. import UIEvents from '../../../service/UI/UIEvents';
  22. import UIUtil from '../util/UIUtil';
  23. import RemoteVideo from './RemoteVideo';
  24. import LargeVideoManager from './LargeVideoManager';
  25. import { VIDEO_CONTAINER_TYPE } from './VideoContainer';
  26. import LocalVideo from './LocalVideo';
  27. const remoteVideos = {};
  28. let localVideoThumbnail = null;
  29. let eventEmitter = null;
  30. let largeVideo;
  31. /**
  32. * flipX state of the localVideo
  33. */
  34. let localFlipX = null;
  35. /**
  36. * Handler for local flip X changed event.
  37. * @param {Object} val
  38. */
  39. function onLocalFlipXChanged(val) {
  40. localFlipX = val;
  41. if (largeVideo) {
  42. largeVideo.onLocalFlipXChange(val);
  43. }
  44. }
  45. /**
  46. * Returns the redux representation of all known users.
  47. *
  48. * @private
  49. * @returns {Array}
  50. */
  51. function getAllParticipants() {
  52. return APP.store.getState()['features/base/participants'];
  53. }
  54. /**
  55. * Returns an array of all thumbnails in the filmstrip.
  56. *
  57. * @private
  58. * @returns {Array}
  59. */
  60. function getAllThumbnails() {
  61. return [
  62. localVideoThumbnail,
  63. ...Object.values(remoteVideos)
  64. ];
  65. }
  66. /**
  67. * Returns the user ID of the remote participant that is current the dominant
  68. * speaker.
  69. *
  70. * @private
  71. * @returns {string|null}
  72. */
  73. function getCurrentRemoteDominantSpeakerID() {
  74. const dominantSpeaker = getAllParticipants()
  75. .find(participant => participant.dominantSpeaker);
  76. if (dominantSpeaker) {
  77. return dominantSpeaker.local ? null : dominantSpeaker.id;
  78. }
  79. return null;
  80. }
  81. /**
  82. * Returns the corresponding resource id to the given peer container
  83. * DOM element.
  84. *
  85. * @return the corresponding resource id to the given peer container
  86. * DOM element
  87. */
  88. function getPeerContainerResourceId(containerElement) {
  89. if (localVideoThumbnail.container === containerElement) {
  90. return localVideoThumbnail.id;
  91. }
  92. const i = containerElement.id.indexOf('participant_');
  93. if (i >= 0) {
  94. return containerElement.id.substring(i + 12);
  95. }
  96. }
  97. const VideoLayout = {
  98. init(emitter) {
  99. eventEmitter = emitter;
  100. localVideoThumbnail = new LocalVideo(
  101. VideoLayout,
  102. emitter,
  103. this._updateLargeVideoIfDisplayed.bind(this));
  104. // sets default video type of local video
  105. // FIXME container type is totally different thing from the video type
  106. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  107. // if we do not resize the thumbs here, if there is no video device
  108. // the local video thumb maybe one pixel
  109. this.resizeThumbnails(true);
  110. this.registerListeners();
  111. },
  112. /**
  113. * Cleans up any existing largeVideo instance.
  114. *
  115. * @returns {void}
  116. */
  117. resetLargeVideo() {
  118. if (largeVideo) {
  119. largeVideo.destroy();
  120. }
  121. largeVideo = null;
  122. },
  123. /**
  124. * Registering listeners for UI events in Video layout component.
  125. *
  126. * @returns {void}
  127. */
  128. registerListeners() {
  129. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED,
  130. onLocalFlipXChanged);
  131. },
  132. initLargeVideo() {
  133. this.resetLargeVideo();
  134. largeVideo = new LargeVideoManager(eventEmitter);
  135. if (localFlipX) {
  136. largeVideo.onLocalFlipXChange(localFlipX);
  137. }
  138. largeVideo.updateContainerSize();
  139. },
  140. /**
  141. * Sets the audio level of the video elements associated to the given id.
  142. *
  143. * @param id the video identifier in the form it comes from the library
  144. * @param lvl the new audio level to update to
  145. */
  146. setAudioLevel(id, lvl) {
  147. const smallVideo = this.getSmallVideo(id);
  148. if (smallVideo) {
  149. smallVideo.updateAudioLevelIndicator(lvl);
  150. }
  151. if (largeVideo && id === largeVideo.id) {
  152. largeVideo.updateLargeVideoAudioLevel(lvl);
  153. }
  154. },
  155. changeLocalVideo(stream) {
  156. const localId = APP.conference.getMyUserId();
  157. this.onVideoTypeChanged(localId, stream.videoType);
  158. localVideoThumbnail.changeVideo(stream);
  159. this._updateLargeVideoIfDisplayed(localId);
  160. },
  161. /**
  162. * Get's the localID of the conference and set it to the local video
  163. * (small one). This needs to be called as early as possible, when muc is
  164. * actually joined. Otherwise events can come with information like email
  165. * and setting them assume the id is already set.
  166. */
  167. mucJoined() {
  168. if (largeVideo && !largeVideo.id) {
  169. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  170. }
  171. // FIXME: replace this call with a generic update call once SmallVideo
  172. // only contains a ReactElement. Then remove this call once the
  173. // Filmstrip is fully in React.
  174. localVideoThumbnail.updateIndicators();
  175. },
  176. /**
  177. * Adds or removes icons for not available camera and microphone.
  178. * @param resourceJid the jid of user
  179. * @param devices available devices
  180. */
  181. setDeviceAvailabilityIcons(id, devices) {
  182. if (APP.conference.isLocalId(id)) {
  183. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  184. return;
  185. }
  186. const video = remoteVideos[id];
  187. if (!video) {
  188. return;
  189. }
  190. video.setDeviceAvailabilityIcons(devices);
  191. },
  192. /**
  193. * Shows/hides local video.
  194. * @param {boolean} true to make the local video visible, false - otherwise
  195. */
  196. setLocalVideoVisible(visible) {
  197. localVideoThumbnail.setVisible(visible);
  198. },
  199. /**
  200. * Checks if removed video is currently displayed and tries to display
  201. * another one instead.
  202. * Uses focusedID if any or dominantSpeakerID if any,
  203. * otherwise elects new video, in this order.
  204. */
  205. _updateAfterThumbRemoved(id) {
  206. // Always trigger an update if large video is empty.
  207. if (!largeVideo
  208. || (this.getLargeVideoID() && !this.isCurrentlyOnLarge(id))) {
  209. return;
  210. }
  211. const pinnedId = this.getPinnedId();
  212. let newId;
  213. if (pinnedId) {
  214. newId = pinnedId;
  215. } else if (getCurrentRemoteDominantSpeakerID()) {
  216. newId = getCurrentRemoteDominantSpeakerID();
  217. } else { // Otherwise select last visible video
  218. newId = this.electLastVisibleVideo();
  219. }
  220. this.updateLargeVideo(newId);
  221. },
  222. electLastVisibleVideo() {
  223. // pick the last visible video in the row
  224. // if nobody else is left, this picks the local video
  225. const remoteThumbs = Filmstrip.getThumbs(true).remoteThumbs;
  226. let thumbs = remoteThumbs.filter('[id!="mixedstream"]');
  227. const lastVisible = thumbs.filter(':visible:last');
  228. if (lastVisible.length) {
  229. const id = getPeerContainerResourceId(lastVisible[0]);
  230. if (remoteVideos[id]) {
  231. logger.info(`electLastVisibleVideo: ${id}`);
  232. return id;
  233. }
  234. // The RemoteVideo was removed (but the DOM elements may still
  235. // exist).
  236. }
  237. logger.info('Last visible video no longer exists');
  238. thumbs = Filmstrip.getThumbs().remoteThumbs;
  239. if (thumbs.length) {
  240. const id = getPeerContainerResourceId(thumbs[0]);
  241. if (remoteVideos[id]) {
  242. logger.info(`electLastVisibleVideo: ${id}`);
  243. return id;
  244. }
  245. // The RemoteVideo was removed (but the DOM elements may
  246. // still exist).
  247. }
  248. // Go with local video
  249. logger.info('Fallback to local video...');
  250. const id = APP.conference.getMyUserId();
  251. logger.info(`electLastVisibleVideo: ${id}`);
  252. return id;
  253. },
  254. onRemoteStreamAdded(stream) {
  255. const id = stream.getParticipantId();
  256. const remoteVideo = remoteVideos[id];
  257. if (!remoteVideo) {
  258. return;
  259. }
  260. remoteVideo.addRemoteStreamElement(stream);
  261. // Make sure track's muted state is reflected
  262. if (stream.getType() === 'audio') {
  263. this.onAudioMute(stream.getParticipantId(), stream.isMuted());
  264. } else {
  265. this.onVideoMute(stream.getParticipantId(), stream.isMuted());
  266. }
  267. },
  268. onRemoteStreamRemoved(stream) {
  269. const id = stream.getParticipantId();
  270. const remoteVideo = remoteVideos[id];
  271. // Remote stream may be removed after participant left the conference.
  272. if (remoteVideo) {
  273. remoteVideo.removeRemoteStreamElement(stream);
  274. }
  275. if (stream.isVideoTrack()) {
  276. this._updateLargeVideoIfDisplayed(id);
  277. }
  278. this.updateMutedForNoTracks(id, stream.getType());
  279. },
  280. /**
  281. * FIXME get rid of this method once muted indicator are reactified (by
  282. * making sure that user with no tracks is displayed as muted )
  283. *
  284. * If participant has no tracks will make the UI display muted status.
  285. * @param {string} participantId
  286. * @param {string} mediaType 'audio' or 'video'
  287. */
  288. updateMutedForNoTracks(participantId, mediaType) {
  289. const participant = APP.conference.getParticipantById(participantId);
  290. if (participant
  291. && !participant.getTracksByMediaType(mediaType).length) {
  292. if (mediaType === 'audio') {
  293. APP.UI.setAudioMuted(participantId, true);
  294. } else if (mediaType === 'video') {
  295. APP.UI.setVideoMuted(participantId, true);
  296. } else {
  297. logger.error(`Unsupported media type: ${mediaType}`);
  298. }
  299. }
  300. },
  301. /**
  302. * Return the type of the remote video.
  303. * @param id the id for the remote video
  304. * @returns {String} the video type video or screen.
  305. */
  306. getRemoteVideoType(id) {
  307. const smallVideo = VideoLayout.getSmallVideo(id);
  308. return smallVideo ? smallVideo.getVideoType() : null;
  309. },
  310. isPinned(id) {
  311. return id === this.getPinnedId();
  312. },
  313. getPinnedId() {
  314. const { id } = getPinnedParticipant(APP.store.getState()) || {};
  315. return id || null;
  316. },
  317. /**
  318. * Triggers a thumbnail to pin or unpin itself.
  319. *
  320. * @param {number} videoNumber - The index of the video to toggle pin on.
  321. * @private
  322. */
  323. togglePin(videoNumber) {
  324. const videos = getAllThumbnails();
  325. const videoView = videos[videoNumber];
  326. videoView && videoView.togglePin();
  327. },
  328. /**
  329. * Callback invoked to update display when the pin participant has changed.
  330. *
  331. * @paramn {string|null} pinnedParticipantID - The participant ID of the
  332. * participant that is pinned or null if no one is pinned.
  333. * @returns {void}
  334. */
  335. onPinChange(pinnedParticipantID) {
  336. if (interfaceConfig.filmStripOnly) {
  337. return;
  338. }
  339. getAllThumbnails().forEach(thumbnail =>
  340. thumbnail.focus(pinnedParticipantID === thumbnail.getId()));
  341. if (pinnedParticipantID) {
  342. this.updateLargeVideo(pinnedParticipantID);
  343. } else {
  344. const currentDominantSpeakerID
  345. = getCurrentRemoteDominantSpeakerID();
  346. if (currentDominantSpeakerID) {
  347. this.updateLargeVideo(currentDominantSpeakerID);
  348. } else {
  349. // if there is no currentDominantSpeakerID, it can also be
  350. // that local participant is the dominant speaker
  351. // we should act as a participant has left and was on large
  352. // and we should choose somebody (electLastVisibleVideo)
  353. this.updateLargeVideo(this.electLastVisibleVideo());
  354. }
  355. }
  356. },
  357. /**
  358. * Creates a participant container for the given id.
  359. *
  360. * @param {Object} participant - The redux representation of a remote
  361. * participant.
  362. * @returns {void}
  363. */
  364. addRemoteParticipantContainer(participant) {
  365. if (!participant || participant.local) {
  366. return;
  367. } else if (participant.isFakeParticipant) {
  368. const sharedVideoThumb = new SharedVideoThumb(
  369. participant,
  370. SHARED_VIDEO_CONTAINER_TYPE,
  371. VideoLayout);
  372. this.addRemoteVideoContainer(participant.id, sharedVideoThumb);
  373. return;
  374. }
  375. const id = participant.id;
  376. const jitsiParticipant = APP.conference.getParticipantById(id);
  377. const remoteVideo
  378. = new RemoteVideo(jitsiParticipant, VideoLayout, eventEmitter);
  379. this._setRemoteControlProperties(jitsiParticipant, remoteVideo);
  380. this.addRemoteVideoContainer(id, remoteVideo);
  381. this.updateMutedForNoTracks(id, 'audio');
  382. this.updateMutedForNoTracks(id, 'video');
  383. const remoteVideosCount = Object.keys(remoteVideos).length;
  384. if (remoteVideosCount === 1) {
  385. window.setTimeout(() => {
  386. const updatedRemoteVideosCount
  387. = Object.keys(remoteVideos).length;
  388. if (updatedRemoteVideosCount === 1 && remoteVideos[id]) {
  389. this._maybePlaceParticipantOnLargeVideo(id);
  390. }
  391. }, 3000);
  392. }
  393. },
  394. /**
  395. * Adds remote video container for the given id and <tt>SmallVideo</tt>.
  396. *
  397. * @param {string} the id of the video to add
  398. * @param {SmallVideo} smallVideo the small video instance to add as a
  399. * remote video
  400. */
  401. addRemoteVideoContainer(id, remoteVideo) {
  402. remoteVideos[id] = remoteVideo;
  403. if (!remoteVideo.getVideoType()) {
  404. // make video type the default one (camera)
  405. // FIXME container type is not a video type
  406. remoteVideo.setVideoType(VIDEO_CONTAINER_TYPE);
  407. }
  408. VideoLayout.resizeThumbnails(true);
  409. // Initialize the view
  410. remoteVideo.updateView();
  411. },
  412. // FIXME: what does this do???
  413. remoteVideoActive(videoElement, resourceJid) {
  414. logger.info(`${resourceJid} video is now active`, videoElement);
  415. VideoLayout.resizeThumbnails(
  416. false, () => {
  417. if (videoElement) {
  418. $(videoElement).show();
  419. }
  420. });
  421. this._maybePlaceParticipantOnLargeVideo(resourceJid);
  422. },
  423. /**
  424. * Update the large video to the last added video only if there's no current
  425. * dominant, focused speaker or update it to the current dominant speaker.
  426. *
  427. * @params {string} resourceJid - The id of the user to maybe display on
  428. * large video.
  429. * @returns {void}
  430. */
  431. _maybePlaceParticipantOnLargeVideo(resourceJid) {
  432. const pinnedId = this.getPinnedId();
  433. if ((!pinnedId
  434. && !getCurrentRemoteDominantSpeakerID()
  435. && this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE))
  436. || pinnedId === resourceJid
  437. || (!pinnedId && resourceJid
  438. && getCurrentRemoteDominantSpeakerID() === resourceJid)
  439. /* Playback started while we're on the stage - may need to update
  440. video source with the new stream */
  441. || this.isCurrentlyOnLarge(resourceJid)) {
  442. this.updateLargeVideo(resourceJid, true);
  443. }
  444. },
  445. /**
  446. * Shows a visual indicator for the moderator of the conference.
  447. * On local or remote participants.
  448. */
  449. showModeratorIndicator() {
  450. const isModerator = APP.conference.isModerator;
  451. if (isModerator) {
  452. localVideoThumbnail.addModeratorIndicator();
  453. } else {
  454. localVideoThumbnail.removeModeratorIndicator();
  455. }
  456. APP.conference.listMembers().forEach(member => {
  457. const id = member.getId();
  458. const remoteVideo = remoteVideos[id];
  459. if (!remoteVideo) {
  460. return;
  461. }
  462. if (member.isModerator()) {
  463. remoteVideo.addModeratorIndicator();
  464. }
  465. remoteVideo.updateRemoteVideoMenu();
  466. });
  467. },
  468. /*
  469. * Shows or hides the audio muted indicator over the local thumbnail video.
  470. * @param {boolean} isMuted
  471. */
  472. showLocalAudioIndicator(isMuted) {
  473. localVideoThumbnail.showAudioIndicator(isMuted);
  474. },
  475. /**
  476. * Resizes thumbnails.
  477. */
  478. resizeThumbnails(
  479. forceUpdate = false,
  480. onComplete = null) {
  481. const { localVideo, remoteVideo }
  482. = Filmstrip.calculateThumbnailSize();
  483. Filmstrip.resizeThumbnails(localVideo, remoteVideo, forceUpdate);
  484. if (shouldDisplayTileView(APP.store.getState())) {
  485. const height
  486. = (localVideo && localVideo.thumbHeight)
  487. || (remoteVideo && remoteVideo.thumbnHeight)
  488. || 0;
  489. const qualityLevel = getNearestReceiverVideoQualityLevel(height);
  490. APP.store.dispatch(setMaxReceiverVideoQuality(qualityLevel));
  491. }
  492. if (onComplete && typeof onComplete === 'function') {
  493. onComplete();
  494. }
  495. },
  496. /**
  497. * On audio muted event.
  498. */
  499. onAudioMute(id, isMuted) {
  500. if (APP.conference.isLocalId(id)) {
  501. localVideoThumbnail.showAudioIndicator(isMuted);
  502. } else {
  503. const remoteVideo = remoteVideos[id];
  504. if (!remoteVideo) {
  505. return;
  506. }
  507. remoteVideo.showAudioIndicator(isMuted);
  508. remoteVideo.updateRemoteVideoMenu(isMuted);
  509. }
  510. },
  511. /**
  512. * On video muted event.
  513. */
  514. onVideoMute(id, value) {
  515. if (APP.conference.isLocalId(id)) {
  516. localVideoThumbnail.setVideoMutedView(value);
  517. } else {
  518. const remoteVideo = remoteVideos[id];
  519. if (remoteVideo) {
  520. remoteVideo.setVideoMutedView(value);
  521. }
  522. }
  523. if (this.isCurrentlyOnLarge(id)) {
  524. // large video will show avatar instead of muted stream
  525. this.updateLargeVideo(id, true);
  526. }
  527. },
  528. /**
  529. * Display name changed.
  530. */
  531. onDisplayNameChanged(id, displayName, status) {
  532. if (id === 'localVideoContainer'
  533. || APP.conference.isLocalId(id)) {
  534. localVideoThumbnail.setDisplayName(displayName);
  535. } else {
  536. const remoteVideo = remoteVideos[id];
  537. if (remoteVideo) {
  538. remoteVideo.setDisplayName(displayName, status);
  539. }
  540. }
  541. },
  542. /**
  543. * Sets the "raised hand" status for a participant identified by 'id'.
  544. */
  545. setRaisedHandStatus(id, raisedHandStatus) {
  546. const video
  547. = APP.conference.isLocalId(id)
  548. ? localVideoThumbnail : remoteVideos[id];
  549. if (video) {
  550. video.showRaisedHandIndicator(raisedHandStatus);
  551. if (raisedHandStatus) {
  552. video.showDominantSpeakerIndicator(false);
  553. }
  554. }
  555. },
  556. /**
  557. * On dominant speaker changed event.
  558. *
  559. * @param {string} id - The participant ID of the new dominant speaker.
  560. * @returns {void}
  561. */
  562. onDominantSpeakerChanged(id) {
  563. getAllThumbnails().forEach(thumbnail =>
  564. thumbnail.showDominantSpeakerIndicator(id === thumbnail.getId()));
  565. if (!remoteVideos[id]) {
  566. return;
  567. }
  568. // Local video will not have container found, but that's ok
  569. // since we don't want to switch to local video.
  570. if (!interfaceConfig.filmStripOnly && !this.getPinnedId()
  571. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  572. this.updateLargeVideo(id);
  573. }
  574. },
  575. /**
  576. * Shows/hides warning about a user's connectivity issues.
  577. *
  578. * @param {string} id - The ID of the remote participant(MUC nickname).
  579. * @param {status} status - The new connection status.
  580. * @returns {void}
  581. */
  582. onParticipantConnectionStatusChanged(id, status) {
  583. if (APP.conference.isLocalId(id)) {
  584. // Maintain old logic of passing in either interrupted or active
  585. // to updateConnectionStatus.
  586. localVideoThumbnail.updateConnectionStatus(status);
  587. if (status === JitsiParticipantConnectionStatus.INTERRUPTED) {
  588. largeVideo && largeVideo.onVideoInterrupted();
  589. } else {
  590. largeVideo && largeVideo.onVideoRestored();
  591. }
  592. return;
  593. }
  594. // We have to trigger full large video update to transition from
  595. // avatar to video on connectivity restored.
  596. this._updateLargeVideoIfDisplayed(id, true);
  597. const remoteVideo = remoteVideos[id];
  598. if (remoteVideo) {
  599. // Updating only connection status indicator is not enough, because
  600. // when we the connection is restored while the avatar was displayed
  601. // (due to 'muted while disconnected' condition) we may want to show
  602. // the video stream again and in order to do that the display mode
  603. // must be updated.
  604. // remoteVideo.updateConnectionStatusIndicator(isActive);
  605. remoteVideo.updateView();
  606. }
  607. },
  608. /**
  609. * On last N change event.
  610. *
  611. * @param endpointsLeavingLastN the list currently leaving last N
  612. * endpoints
  613. * @param endpointsEnteringLastN the list currently entering last N
  614. * endpoints
  615. */
  616. onLastNEndpointsChanged(endpointsLeavingLastN, endpointsEnteringLastN) {
  617. if (endpointsLeavingLastN) {
  618. endpointsLeavingLastN.forEach(this._updateRemoteVideo, this);
  619. }
  620. if (endpointsEnteringLastN) {
  621. endpointsEnteringLastN.forEach(this._updateRemoteVideo, this);
  622. }
  623. },
  624. /**
  625. * Updates remote video by id if it exists.
  626. * @param {string} id of the remote video
  627. * @private
  628. */
  629. _updateRemoteVideo(id) {
  630. const remoteVideo = remoteVideos[id];
  631. if (remoteVideo) {
  632. remoteVideo.updateView();
  633. if (remoteVideo.isCurrentlyOnLargeVideo()) {
  634. this.updateLargeVideo(id);
  635. }
  636. }
  637. },
  638. /**
  639. * Hides the connection indicator
  640. * @param id
  641. */
  642. hideConnectionIndicator(id) {
  643. const remoteVideo = remoteVideos[id];
  644. if (remoteVideo) {
  645. remoteVideo.removeConnectionIndicator();
  646. }
  647. },
  648. /**
  649. * Hides all the indicators
  650. */
  651. hideStats() {
  652. for (const video in remoteVideos) { // eslint-disable-line guard-for-in
  653. const remoteVideo = remoteVideos[video];
  654. if (remoteVideo) {
  655. remoteVideo.removeConnectionIndicator();
  656. }
  657. }
  658. localVideoThumbnail.removeConnectionIndicator();
  659. },
  660. removeParticipantContainer(id) {
  661. // Unlock large video
  662. if (this.getPinnedId() === id) {
  663. logger.info('Focused video owner has left the conference');
  664. APP.store.dispatch(pinParticipant(null));
  665. }
  666. const remoteVideo = remoteVideos[id];
  667. if (remoteVideo) {
  668. // Remove remote video
  669. logger.info(`Removing remote video: ${id}`);
  670. delete remoteVideos[id];
  671. remoteVideo.remove();
  672. } else {
  673. logger.warn(`No remote video for ${id}`);
  674. }
  675. VideoLayout.resizeThumbnails();
  676. VideoLayout._updateAfterThumbRemoved(id);
  677. },
  678. onVideoTypeChanged(id, newVideoType) {
  679. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  680. return;
  681. }
  682. logger.info('Peer video type changed: ', id, newVideoType);
  683. let smallVideo;
  684. if (APP.conference.isLocalId(id)) {
  685. if (!localVideoThumbnail) {
  686. logger.warn('Local video not ready yet');
  687. return;
  688. }
  689. smallVideo = localVideoThumbnail;
  690. } else if (remoteVideos[id]) {
  691. smallVideo = remoteVideos[id];
  692. } else {
  693. return;
  694. }
  695. smallVideo.setVideoType(newVideoType);
  696. if (this.isCurrentlyOnLarge(id)) {
  697. this.updateLargeVideo(id, true);
  698. }
  699. },
  700. /**
  701. * Resizes the video area.
  702. *
  703. * TODO: Remove the "animate" param as it is no longer passed in as true.
  704. *
  705. * @param forceUpdate indicates that hidden thumbnails will be shown
  706. */
  707. resizeVideoArea(
  708. forceUpdate = false,
  709. animate = false) {
  710. // Resize the thumbnails first.
  711. this.resizeThumbnails(forceUpdate);
  712. if (largeVideo) {
  713. largeVideo.updateContainerSize();
  714. largeVideo.resize(animate);
  715. }
  716. // Calculate available width and height.
  717. const availableHeight = window.innerHeight;
  718. const availableWidth = UIUtil.getAvailableVideoWidth();
  719. if (availableWidth < 0 || availableHeight < 0) {
  720. return;
  721. }
  722. },
  723. getSmallVideo(id) {
  724. if (APP.conference.isLocalId(id)) {
  725. return localVideoThumbnail;
  726. }
  727. return remoteVideos[id];
  728. },
  729. changeUserAvatar(id, avatarUrl) {
  730. const smallVideo = VideoLayout.getSmallVideo(id);
  731. if (smallVideo) {
  732. smallVideo.avatarChanged(avatarUrl);
  733. } else {
  734. logger.warn(
  735. `Missed avatar update - no small video yet for ${id}`
  736. );
  737. }
  738. if (this.isCurrentlyOnLarge(id)) {
  739. largeVideo.updateAvatar(avatarUrl);
  740. }
  741. },
  742. isLargeVideoVisible() {
  743. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  744. },
  745. /**
  746. * @return {LargeContainer} the currently displayed container on large
  747. * video.
  748. */
  749. getCurrentlyOnLargeContainer() {
  750. return largeVideo.getCurrentContainer();
  751. },
  752. isCurrentlyOnLarge(id) {
  753. return largeVideo && largeVideo.id === id;
  754. },
  755. /**
  756. * Triggers an update of remote video and large video displays so they may
  757. * pick up any state changes that have occurred elsewhere.
  758. *
  759. * @returns {void}
  760. */
  761. updateAllVideos() {
  762. const displayedUserId = this.getLargeVideoID();
  763. if (displayedUserId) {
  764. this.updateLargeVideo(displayedUserId, true);
  765. }
  766. Object.keys(remoteVideos).forEach(video => {
  767. remoteVideos[video].updateView();
  768. });
  769. },
  770. updateLargeVideo(id, forceUpdate) {
  771. if (!largeVideo) {
  772. return;
  773. }
  774. const currentContainer = largeVideo.getCurrentContainer();
  775. const currentContainerType = largeVideo.getCurrentContainerType();
  776. const currentId = largeVideo.id;
  777. const isOnLarge = this.isCurrentlyOnLarge(id);
  778. const smallVideo = this.getSmallVideo(id);
  779. if (isOnLarge && !forceUpdate
  780. && LargeVideoManager.isVideoContainer(currentContainerType)
  781. && smallVideo) {
  782. const currentStreamId = currentContainer.getStreamID();
  783. const newStreamId
  784. = smallVideo.videoStream
  785. ? smallVideo.videoStream.getId() : null;
  786. // FIXME it might be possible to get rid of 'forceUpdate' argument
  787. if (currentStreamId !== newStreamId) {
  788. logger.debug('Enforcing large video update for stream change');
  789. forceUpdate = true; // eslint-disable-line no-param-reassign
  790. }
  791. }
  792. if ((!isOnLarge || forceUpdate) && smallVideo) {
  793. const videoType = this.getRemoteVideoType(id);
  794. // FIXME video type is not the same thing as container type
  795. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  796. APP.API.notifyOnStageParticipantChanged(id);
  797. }
  798. let oldSmallVideo;
  799. if (currentId) {
  800. oldSmallVideo = this.getSmallVideo(currentId);
  801. }
  802. smallVideo.waitForResolutionChange();
  803. if (oldSmallVideo) {
  804. oldSmallVideo.waitForResolutionChange();
  805. }
  806. largeVideo.updateLargeVideo(
  807. id,
  808. smallVideo.videoStream,
  809. videoType || VIDEO_TYPE.CAMERA
  810. ).then(() => {
  811. // update current small video and the old one
  812. smallVideo.updateView();
  813. oldSmallVideo && oldSmallVideo.updateView();
  814. }, () => {
  815. // use clicked other video during update, nothing to do.
  816. });
  817. } else if (currentId) {
  818. const currentSmallVideo = this.getSmallVideo(currentId);
  819. currentSmallVideo.updateView();
  820. }
  821. },
  822. addLargeVideoContainer(type, container) {
  823. largeVideo && largeVideo.addContainer(type, container);
  824. },
  825. removeLargeVideoContainer(type) {
  826. largeVideo && largeVideo.removeContainer(type);
  827. },
  828. /**
  829. * @returns Promise
  830. */
  831. showLargeVideoContainer(type, show) {
  832. if (!largeVideo) {
  833. return Promise.reject();
  834. }
  835. const isVisible = this.isLargeContainerTypeVisible(type);
  836. if (isVisible === show) {
  837. return Promise.resolve();
  838. }
  839. const currentId = largeVideo.id;
  840. let oldSmallVideo;
  841. if (currentId) {
  842. oldSmallVideo = this.getSmallVideo(currentId);
  843. }
  844. let containerTypeToShow = type;
  845. // if we are hiding a container and there is focusedVideo
  846. // (pinned remote video) use its video type,
  847. // if not then use default type - large video
  848. if (!show) {
  849. const pinnedId = this.getPinnedId();
  850. if (pinnedId) {
  851. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  852. } else {
  853. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  854. }
  855. }
  856. return largeVideo.showContainer(containerTypeToShow)
  857. .then(() => {
  858. if (oldSmallVideo) {
  859. oldSmallVideo && oldSmallVideo.updateView();
  860. }
  861. });
  862. },
  863. isLargeContainerTypeVisible(type) {
  864. return largeVideo && largeVideo.state === type;
  865. },
  866. /**
  867. * Returns the id of the current video shown on large.
  868. * Currently used by tests (torture).
  869. */
  870. getLargeVideoID() {
  871. return largeVideo && largeVideo.id;
  872. },
  873. /**
  874. * Returns the the current video shown on large.
  875. * Currently used by tests (torture).
  876. */
  877. getLargeVideo() {
  878. return largeVideo;
  879. },
  880. /**
  881. * Sets the flipX state of the local video.
  882. * @param {boolean} true for flipped otherwise false;
  883. */
  884. setLocalFlipX(val) {
  885. this.localFlipX = val;
  886. },
  887. getEventEmitter() {
  888. return eventEmitter;
  889. },
  890. /**
  891. * Handles user's features changes.
  892. */
  893. onUserFeaturesChanged(user) {
  894. const video = this.getSmallVideo(user.getId());
  895. if (!video) {
  896. return;
  897. }
  898. this._setRemoteControlProperties(user, video);
  899. },
  900. /**
  901. * Sets the remote control properties (checks whether remote control
  902. * is supported and executes remoteVideo.setRemoteControlSupport).
  903. * @param {JitsiParticipant} user the user that will be checked for remote
  904. * control support.
  905. * @param {RemoteVideo} remoteVideo the remoteVideo on which the properties
  906. * will be set.
  907. */
  908. _setRemoteControlProperties(user, remoteVideo) {
  909. APP.remoteControl.checkUserRemoteControlSupport(user).then(result =>
  910. remoteVideo.setRemoteControlSupport(result));
  911. },
  912. /**
  913. * Returns the wrapper jquery selector for the largeVideo
  914. * @returns {JQuerySelector} the wrapper jquery selector for the largeVideo
  915. */
  916. getLargeVideoWrapper() {
  917. return this.getCurrentlyOnLargeContainer().$wrapper;
  918. },
  919. /**
  920. * Returns the number of remove video ids.
  921. *
  922. * @returns {number} The number of remote videos.
  923. */
  924. getRemoteVideosCount() {
  925. return Object.keys(remoteVideos).length;
  926. },
  927. /**
  928. * Sets the remote control active status for a remote participant.
  929. *
  930. * @param {string} participantID - The id of the remote participant.
  931. * @param {boolean} isActive - The new remote control active status.
  932. * @returns {void}
  933. */
  934. setRemoteControlActiveStatus(participantID, isActive) {
  935. remoteVideos[participantID].setRemoteControlActiveStatus(isActive);
  936. },
  937. /**
  938. * Sets the remote control active status for the local participant.
  939. *
  940. * @returns {void}
  941. */
  942. setLocalRemoteControlActiveChanged() {
  943. Object.values(remoteVideos).forEach(
  944. remoteVideo => remoteVideo.updateRemoteVideoMenu()
  945. );
  946. },
  947. /**
  948. * Helper method to invoke when the video layout has changed and elements
  949. * have to be re-arranged and resized.
  950. *
  951. * @returns {void}
  952. */
  953. refreshLayout() {
  954. localVideoThumbnail && localVideoThumbnail.updateDOMLocation();
  955. VideoLayout.resizeVideoArea();
  956. localVideoThumbnail && localVideoThumbnail.rerender();
  957. Object.values(remoteVideos).forEach(
  958. remoteVideo => remoteVideo.rerender()
  959. );
  960. },
  961. /**
  962. * Triggers an update of large video if the passed in participant is
  963. * currently displayed on large video.
  964. *
  965. * @param {string} participantId - The participant ID that should trigger an
  966. * update of large video if displayed.
  967. * @param {boolean} force - Whether or not the large video update should
  968. * happen no matter what.
  969. * @returns {void}
  970. */
  971. _updateLargeVideoIfDisplayed(participantId, force = false) {
  972. if (this.isCurrentlyOnLarge(participantId)) {
  973. this.updateLargeVideo(participantId, force);
  974. }
  975. }
  976. };
  977. export default VideoLayout;