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

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