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

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