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

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