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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  1. /* global config, APP, $, interfaceConfig, JitsiMeetJS */
  2. /* jshint -W101 */
  3. import AudioLevels from "../audio_levels/AudioLevels";
  4. import Avatar from "../avatar/Avatar";
  5. import BottomToolbar from "../toolbars/BottomToolbar";
  6. import FilmStrip from "./FilmStrip";
  7. import UIEvents from "../../../service/UI/UIEvents";
  8. import UIUtil from "../util/UIUtil";
  9. import RemoteVideo from "./RemoteVideo";
  10. import LargeVideoManager, {VIDEO_CONTAINER_TYPE} from "./LargeVideo";
  11. import {SHARED_VIDEO_CONTAINER_TYPE} from '../shared_video/SharedVideo';
  12. import LocalVideo from "./LocalVideo";
  13. import PanelToggler from "../side_pannels/SidePanelToggler";
  14. const RTCUIUtil = JitsiMeetJS.util.RTCUIHelper;
  15. var remoteVideos = {};
  16. var localVideoThumbnail = null;
  17. var currentDominantSpeaker = null;
  18. var localLastNCount = config.channelLastN;
  19. var localLastNSet = [];
  20. var lastNEndpointsCache = [];
  21. var lastNPickupId = null;
  22. var eventEmitter = null;
  23. /**
  24. * Currently focused video jid
  25. * @type {String}
  26. */
  27. var pinnedId = null;
  28. /**
  29. * flipX state of the localVideo
  30. */
  31. let localFlipX = null;
  32. /**
  33. * On contact list item clicked.
  34. */
  35. function onContactClicked (id) {
  36. if (APP.conference.isLocalId(id)) {
  37. $("#localVideoContainer").click();
  38. return;
  39. }
  40. let remoteVideo = remoteVideos[id];
  41. if (remoteVideo && remoteVideo.hasVideo()) {
  42. // It is not always the case that a videoThumb exists (if there is
  43. // no actual video).
  44. if (remoteVideo.hasVideoStarted()) {
  45. // We have a video src, great! Let's update the large video
  46. // now.
  47. VideoLayout.handleVideoThumbClicked(id);
  48. } else {
  49. // If we don't have a video src for jid, there's absolutely
  50. // no point in calling handleVideoThumbClicked; Quite
  51. // simply, it won't work because it needs an src to attach
  52. // to the large video.
  53. //
  54. // Instead, we trigger the pinned endpoint changed event to
  55. // let the bridge adjust its lastN set for myjid and store
  56. // the pinned user in the lastNPickupId variable to be
  57. // picked up later by the lastN changed event handler.
  58. lastNPickupId = id;
  59. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, remoteVideo, true);
  60. }
  61. }
  62. }
  63. /**
  64. * Returns the corresponding resource id to the given peer container
  65. * DOM element.
  66. *
  67. * @return the corresponding resource id to the given peer container
  68. * DOM element
  69. */
  70. function getPeerContainerResourceId (containerElement) {
  71. if (localVideoThumbnail.container === containerElement) {
  72. return localVideoThumbnail.id;
  73. }
  74. let i = containerElement.id.indexOf('participant_');
  75. if (i >= 0) {
  76. return containerElement.id.substring(i + 12);
  77. }
  78. }
  79. let largeVideo;
  80. var VideoLayout = {
  81. init (emitter) {
  82. eventEmitter = emitter;
  83. eventEmitter.addListener(UIEvents.LOCAL_FLIPX_CHANGED, function (val) {
  84. localFlipX = val;
  85. if(largeVideo)
  86. largeVideo.onLocalFlipXChange(val);
  87. });
  88. localVideoThumbnail = new LocalVideo(VideoLayout, emitter);
  89. // sets default video type of local video
  90. localVideoThumbnail.setVideoType(VIDEO_CONTAINER_TYPE);
  91. // if we do not resize the thumbs here, if there is no video device
  92. // the local video thumb maybe one pixel
  93. this.resizeThumbnails(false, true, false);
  94. emitter.addListener(UIEvents.CONTACT_CLICKED, onContactClicked);
  95. this.lastNCount = config.channelLastN;
  96. },
  97. initLargeVideo (isSideBarVisible) {
  98. largeVideo = new LargeVideoManager();
  99. if(localFlipX) {
  100. largeVideo.onLocalFlipXChange(localFlipX);
  101. }
  102. largeVideo.updateContainerSize(isSideBarVisible);
  103. AudioLevels.init();
  104. },
  105. setAudioLevel(id, lvl) {
  106. if (!largeVideo) {
  107. return;
  108. }
  109. AudioLevels.updateAudioLevel(
  110. id, lvl, largeVideo.id
  111. );
  112. },
  113. isInLastN (resource) {
  114. return this.lastNCount < 0 || // lastN is disabled
  115. // lastNEndpoints cache not built yet
  116. (this.lastNCount > 0 && !lastNEndpointsCache.length) ||
  117. (lastNEndpointsCache &&
  118. lastNEndpointsCache.indexOf(resource) !== -1);
  119. },
  120. changeLocalAudio (stream) {
  121. let localAudio = document.getElementById('localAudio');
  122. localAudio = stream.attach(localAudio);
  123. // Now when Temasys plugin is converting also <audio> elements to
  124. // plugin's <object>s, in current layout it will capture click events
  125. // before it reaches the local video object. We hide it here in order
  126. // to prevent that.
  127. //if (RTCBrowserType.isIExplorer()) {
  128. // The issue is not present on Safari. Also if we hide it in Safari
  129. // then the local audio track will have 'enabled' flag set to false
  130. // which will result in audio mute issues
  131. // $(localAudio).hide();
  132. localAudio.width = 1;
  133. localAudio.height = 1;
  134. //}
  135. },
  136. changeLocalVideo (stream) {
  137. // Set default display name.
  138. localVideoThumbnail.setDisplayName();
  139. localVideoThumbnail.createConnectionIndicator();
  140. let localId = APP.conference.getMyUserId();
  141. this.onVideoTypeChanged(localId, stream.videoType);
  142. let {thumbWidth, thumbHeight} = this.resizeThumbnails(false, true);
  143. AudioLevels.updateAudioLevelCanvas(null, thumbWidth, thumbHeight);
  144. if (!stream.isMuted()) {
  145. localVideoThumbnail.changeVideo(stream);
  146. }
  147. /* force update if we're currently being displayed */
  148. if (this.isCurrentlyOnLarge(localId)) {
  149. this.updateLargeVideo(localId, true);
  150. }
  151. },
  152. /**
  153. * Get's the localID of the conference and set it to the local video
  154. * (small one). This needs to be called as early as possible, when muc is
  155. * actually joined. Otherwise events can come with information like email
  156. * and setting them assume the id is already set.
  157. */
  158. mucJoined () {
  159. if (largeVideo && !largeVideo.id) {
  160. this.updateLargeVideo(APP.conference.getMyUserId(), true);
  161. }
  162. },
  163. /**
  164. * Adds or removes icons for not available camera and microphone.
  165. * @param resourceJid the jid of user
  166. * @param devices available devices
  167. */
  168. setDeviceAvailabilityIcons (id, devices) {
  169. if (APP.conference.isLocalId(id)) {
  170. localVideoThumbnail.setDeviceAvailabilityIcons(devices);
  171. return;
  172. }
  173. let video = remoteVideos[id];
  174. if (!video) {
  175. return;
  176. }
  177. video.setDeviceAvailabilityIcons(devices);
  178. },
  179. /**
  180. * Enables/disables device availability icons for the given participant id.
  181. * The default value is {true}.
  182. * @param id the identifier of the participant
  183. * @param enable {true} to enable device availability icons
  184. */
  185. enableDeviceAvailabilityIcons (id, enable) {
  186. let video;
  187. if (APP.conference.isLocalId(id)) {
  188. video = localVideoThumbnail;
  189. }
  190. else {
  191. video = remoteVideos[id];
  192. }
  193. if (video)
  194. video.enableDeviceAvailabilityIcons(enable);
  195. },
  196. /**
  197. * Shows/hides local video.
  198. * @param {boolean} true to make the local video visible, false - otherwise
  199. */
  200. setLocalVideoVisible(visible) {
  201. localVideoThumbnail.setVisible(visible);
  202. },
  203. /**
  204. * Checks if removed video is currently displayed and tries to display
  205. * another one instead.
  206. * Uses focusedID if any or dominantSpeakerID if any,
  207. * otherwise elects new video, in this order.
  208. */
  209. updateAfterThumbRemoved (id) {
  210. if (!this.isCurrentlyOnLarge(id)) {
  211. return;
  212. }
  213. let newId;
  214. if (pinnedId)
  215. newId = pinnedId;
  216. else if (currentDominantSpeaker)
  217. newId = currentDominantSpeaker;
  218. else // Otherwise select last visible video
  219. newId = this.electLastVisibleVideo();
  220. this.updateLargeVideo(newId);
  221. },
  222. electLastVisibleVideo () {
  223. // pick the last visible video in the row
  224. // if nobody else is left, this picks the local video
  225. let thumbs = FilmStrip.getThumbs(true).filter('[id!="mixedstream"]');
  226. let lastVisible = thumbs.filter(':visible:last');
  227. if (lastVisible.length) {
  228. let id = getPeerContainerResourceId(lastVisible[0]);
  229. if (remoteVideos[id]) {
  230. console.info("electLastVisibleVideo: " + id);
  231. return id;
  232. }
  233. // The RemoteVideo was removed (but the DOM elements may still
  234. // exist).
  235. }
  236. console.info("Last visible video no longer exists");
  237. thumbs = FilmStrip.getThumbs();
  238. if (thumbs.length) {
  239. let id = getPeerContainerResourceId(thumbs[0]);
  240. if (remoteVideos[id]) {
  241. console.info("electLastVisibleVideo: " + id);
  242. return id;
  243. }
  244. // The RemoteVideo was removed (but the DOM elements may
  245. // still exist).
  246. }
  247. // Go with local video
  248. console.info("Fallback to local video...");
  249. let id = APP.conference.getMyUserId();
  250. console.info("electLastVisibleVideo: " + id);
  251. return id;
  252. },
  253. onRemoteStreamAdded (stream) {
  254. let id = stream.getParticipantId();
  255. let remoteVideo = remoteVideos[id];
  256. if (!remoteVideo)
  257. return;
  258. remoteVideo.addRemoteStreamElement(stream);
  259. // if track is muted make sure we reflect that
  260. if(stream.isMuted())
  261. {
  262. if(stream.getType() === "audio")
  263. this.onAudioMute(stream.getParticipantId(), true);
  264. else
  265. this.onVideoMute(stream.getParticipantId(), true);
  266. }
  267. },
  268. onRemoteStreamRemoved (stream) {
  269. let id = stream.getParticipantId();
  270. let remoteVideo = remoteVideos[id];
  271. if (remoteVideo) { // remote stream may be removed after participant left the conference
  272. remoteVideo.removeRemoteStreamElement(stream);
  273. }
  274. },
  275. /**
  276. * Return the type of the remote video.
  277. * @param id the id for the remote video
  278. * @returns {String} the video type video or screen.
  279. */
  280. getRemoteVideoType (id) {
  281. let smallVideo = VideoLayout.getSmallVideo(id);
  282. return smallVideo ? smallVideo.getVideoType() : null;
  283. },
  284. isPinned (id) {
  285. return (pinnedId) ? (id === pinnedId) : false;
  286. },
  287. getPinnedId () {
  288. return pinnedId;
  289. },
  290. /**
  291. * Handles the click on a video thumbnail.
  292. *
  293. * @param id the identifier of the video thumbnail
  294. */
  295. handleVideoThumbClicked (id) {
  296. if(pinnedId) {
  297. var oldSmallVideo = VideoLayout.getSmallVideo(pinnedId);
  298. if (oldSmallVideo && !interfaceConfig.filmStripOnly)
  299. oldSmallVideo.focus(false);
  300. }
  301. var smallVideo = VideoLayout.getSmallVideo(id);
  302. // Unpin if currently pinned.
  303. if (pinnedId === id)
  304. {
  305. pinnedId = null;
  306. // Enable the currently set dominant speaker.
  307. if (currentDominantSpeaker) {
  308. if(smallVideo && smallVideo.hasVideo()) {
  309. this.updateLargeVideo(currentDominantSpeaker);
  310. }
  311. }
  312. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, false);
  313. return;
  314. }
  315. // Lock new video
  316. pinnedId = id;
  317. // Update focused/pinned interface.
  318. if (id) {
  319. if (smallVideo && !interfaceConfig.filmStripOnly)
  320. smallVideo.focus(true);
  321. eventEmitter.emit(UIEvents.PINNED_ENDPOINT, smallVideo, true);
  322. }
  323. this.updateLargeVideo(id);
  324. },
  325. /**
  326. * Creates a participant container for the given id and smallVideo.
  327. *
  328. * @param id the id of the participant to add
  329. * @param {SmallVideo} smallVideo optional small video instance to add as a
  330. * remote video, if undefined RemoteVideo will be created
  331. */
  332. addParticipantContainer (id, smallVideo) {
  333. let remoteVideo;
  334. if(smallVideo)
  335. remoteVideo = smallVideo;
  336. else
  337. remoteVideo = new RemoteVideo(id, VideoLayout, eventEmitter);
  338. remoteVideos[id] = remoteVideo;
  339. let videoType = VideoLayout.getRemoteVideoType(id);
  340. if (!videoType) {
  341. // make video type the default one (camera)
  342. videoType = VIDEO_CONTAINER_TYPE;
  343. }
  344. remoteVideo.setVideoType(videoType);
  345. // In case this is not currently in the last n we don't show it.
  346. if (localLastNCount && localLastNCount > 0 &&
  347. FilmStrip.getThumbs().length >= localLastNCount + 2) {
  348. remoteVideo.showPeerContainer('hide');
  349. } else {
  350. VideoLayout.resizeThumbnails(false, true);
  351. }
  352. },
  353. videoactive (videoelem, resourceJid) {
  354. console.info(resourceJid + " video is now active", videoelem);
  355. VideoLayout.resizeThumbnails(
  356. false, false, false, function() {$(videoelem).show();});
  357. // Update the large video to the last added video only if there's no
  358. // current dominant, focused speaker or update it to
  359. // the current dominant speaker.
  360. if ((!pinnedId &&
  361. !currentDominantSpeaker &&
  362. this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE)) ||
  363. pinnedId === resourceJid ||
  364. (!pinnedId && resourceJid &&
  365. currentDominantSpeaker === resourceJid) ||
  366. /* Playback started while we're on the stage - may need to update
  367. video source with the new stream */
  368. this.isCurrentlyOnLarge(resourceJid)) {
  369. this.updateLargeVideo(resourceJid, true);
  370. }
  371. },
  372. /**
  373. * Shows the presence status message for the given video.
  374. */
  375. setPresenceStatus (id, statusMsg) {
  376. let remoteVideo = remoteVideos[id];
  377. if (remoteVideo)
  378. remoteVideo.setPresenceStatus(statusMsg);
  379. },
  380. /**
  381. * Shows a visual indicator for the moderator of the conference.
  382. * On local or remote participants.
  383. */
  384. showModeratorIndicator () {
  385. let isModerator = APP.conference.isModerator;
  386. if (isModerator) {
  387. localVideoThumbnail.createModeratorIndicatorElement();
  388. } else {
  389. localVideoThumbnail.removeModeratorIndicatorElement();
  390. }
  391. APP.conference.listMembers().forEach(function (member) {
  392. let id = member.getId();
  393. let remoteVideo = remoteVideos[id];
  394. if (!remoteVideo)
  395. return;
  396. if (member.isModerator()) {
  397. remoteVideo.removeRemoteVideoMenu();
  398. remoteVideo.createModeratorIndicatorElement();
  399. } else if (isModerator) {
  400. // We are moderator, but user is not - add menu
  401. if ($(`#remote_popupmenu_${id}`).length <= 0) {
  402. remoteVideo.addRemoteVideoMenu();
  403. }
  404. }
  405. });
  406. },
  407. /*
  408. * Shows or hides the audio muted indicator over the local thumbnail video.
  409. * @param {boolean} isMuted
  410. */
  411. showLocalAudioIndicator (isMuted) {
  412. localVideoThumbnail.showAudioIndicator(isMuted);
  413. },
  414. /**
  415. * Resizes thumbnails.
  416. */
  417. resizeThumbnails ( animate = false,
  418. forceUpdate = false,
  419. isSideBarVisible = null,
  420. onComplete = null) {
  421. isSideBarVisible
  422. = (isSideBarVisible !== null)
  423. ? isSideBarVisible : PanelToggler.isVisible();
  424. let {thumbWidth, thumbHeight}
  425. = FilmStrip.calculateThumbnailSize(isSideBarVisible);
  426. $('.userAvatar').css('left', (thumbWidth - thumbHeight) / 2);
  427. FilmStrip.resizeThumbnails(thumbWidth, thumbHeight,
  428. animate, forceUpdate)
  429. .then(function () {
  430. BottomToolbar.resizeToolbar(thumbWidth, thumbHeight);
  431. AudioLevels.updateCanvasSize(thumbWidth, thumbHeight);
  432. if (onComplete && typeof onComplete === "function")
  433. onComplete();
  434. });
  435. return {thumbWidth, thumbHeight};
  436. },
  437. /**
  438. * On audio muted event.
  439. */
  440. onAudioMute (id, isMuted) {
  441. if (APP.conference.isLocalId(id)) {
  442. localVideoThumbnail.showAudioIndicator(isMuted);
  443. } else {
  444. let remoteVideo = remoteVideos[id];
  445. if (!remoteVideo)
  446. return;
  447. remoteVideo.showAudioIndicator(isMuted);
  448. if (APP.conference.isModerator) {
  449. remoteVideo.updateRemoteVideoMenu(isMuted);
  450. }
  451. }
  452. },
  453. /**
  454. * On video muted event.
  455. */
  456. onVideoMute (id, value) {
  457. if (APP.conference.isLocalId(id)) {
  458. localVideoThumbnail.setMutedView(value);
  459. } else {
  460. let remoteVideo = remoteVideos[id];
  461. if (remoteVideo)
  462. remoteVideo.setMutedView(value);
  463. }
  464. if (this.isCurrentlyOnLarge(id)) {
  465. // large video will show avatar instead of muted stream
  466. this.updateLargeVideo(id, true);
  467. }
  468. },
  469. /**
  470. * Display name changed.
  471. */
  472. onDisplayNameChanged (id, displayName, status) {
  473. if (id === 'localVideoContainer' ||
  474. APP.conference.isLocalId(id)) {
  475. localVideoThumbnail.setDisplayName(displayName);
  476. } else {
  477. let remoteVideo = remoteVideos[id];
  478. if (remoteVideo)
  479. remoteVideo.setDisplayName(displayName, status);
  480. }
  481. },
  482. /**
  483. * Sets the "raised hand" status for a participant identified by 'id'.
  484. */
  485. setRaisedHandStatus(id, raisedHandStatus) {
  486. var video
  487. = APP.conference.isLocalId(id)
  488. ? localVideoThumbnail : remoteVideos[id];
  489. if (video) {
  490. video.showRaisedHandIndicator(raisedHandStatus);
  491. }
  492. },
  493. /**
  494. * On dominant speaker changed event.
  495. */
  496. onDominantSpeakerChanged (id) {
  497. if (id === currentDominantSpeaker) {
  498. return;
  499. }
  500. let oldSpeakerRemoteVideo = remoteVideos[currentDominantSpeaker];
  501. // We ignore local user events, but just unmark remote user as dominant
  502. // while we are talking
  503. if (APP.conference.isLocalId(id)) {
  504. if(oldSpeakerRemoteVideo)
  505. {
  506. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  507. currentDominantSpeaker = null;
  508. }
  509. localVideoThumbnail.showDominantSpeakerIndicator(true);
  510. return;
  511. }
  512. let remoteVideo = remoteVideos[id];
  513. if (!remoteVideo) {
  514. return;
  515. }
  516. // Update the current dominant speaker.
  517. remoteVideo.showDominantSpeakerIndicator(true);
  518. localVideoThumbnail.showDominantSpeakerIndicator(false);
  519. // let's remove the indications from the remote video if any
  520. if (oldSpeakerRemoteVideo) {
  521. oldSpeakerRemoteVideo.showDominantSpeakerIndicator(false);
  522. }
  523. currentDominantSpeaker = id;
  524. // Local video will not have container found, but that's ok
  525. // since we don't want to switch to local video.
  526. // Update the large video if the video source is already available,
  527. // otherwise wait for the "videoactive.jingle" event.
  528. if (!pinnedId
  529. && remoteVideo.hasVideoStarted()
  530. && !this.getCurrentlyOnLargeContainer().stayOnStage()) {
  531. this.updateLargeVideo(id);
  532. }
  533. },
  534. /**
  535. * On last N change event.
  536. *
  537. * @param lastNEndpoints the list of last N endpoints
  538. * @param endpointsEnteringLastN the list currently entering last N
  539. * endpoints
  540. */
  541. onLastNEndpointsChanged (lastNEndpoints, endpointsEnteringLastN) {
  542. if (this.lastNCount !== lastNEndpoints.length)
  543. this.lastNCount = lastNEndpoints.length;
  544. lastNEndpointsCache = lastNEndpoints;
  545. // Say A, B, C, D, E, and F are in a conference and LastN = 3.
  546. //
  547. // If LastN drops to, say, 2, because of adaptivity, then E should see
  548. // thumbnails for A, B and C. A and B are in E's server side LastN set,
  549. // so E sees them. C is only in E's local LastN set.
  550. //
  551. // If F starts talking and LastN = 3, then E should see thumbnails for
  552. // F, A, B. B gets "ejected" from E's server side LastN set, but it
  553. // enters E's local LastN ejecting C.
  554. // Increase the local LastN set size, if necessary.
  555. if (this.lastNCount > localLastNCount) {
  556. localLastNCount = this.lastNCount;
  557. }
  558. // Update the local LastN set preserving the order in which the
  559. // endpoints appeared in the LastN/local LastN set.
  560. var nextLocalLastNSet = lastNEndpoints.slice(0);
  561. for (var i = 0; i < localLastNSet.length; i++) {
  562. if (nextLocalLastNSet.length >= localLastNCount) {
  563. break;
  564. }
  565. var resourceJid = localLastNSet[i];
  566. if (nextLocalLastNSet.indexOf(resourceJid) === -1) {
  567. nextLocalLastNSet.push(resourceJid);
  568. }
  569. }
  570. localLastNSet = nextLocalLastNSet;
  571. var updateLargeVideo = false;
  572. // Handle LastN/local LastN changes.
  573. FilmStrip.getThumbs().each(( index, element ) => {
  574. var resourceJid = getPeerContainerResourceId(element);
  575. var smallVideo = remoteVideos[resourceJid];
  576. // We do not want to process any logic for our own(local) video
  577. // because the local participant is never in the lastN set.
  578. // The code of this function might detect that the local participant
  579. // has been dropped out of the lastN set and will update the large
  580. // video
  581. // Detected from avatar tests, where lastN event override
  582. // local video pinning
  583. if(APP.conference.isLocalId(resourceJid))
  584. return;
  585. var isReceived = true;
  586. if (resourceJid &&
  587. lastNEndpoints.indexOf(resourceJid) < 0 &&
  588. localLastNSet.indexOf(resourceJid) < 0) {
  589. console.log("Remove from last N", resourceJid);
  590. if (smallVideo)
  591. smallVideo.showPeerContainer('hide');
  592. else if (!APP.conference.isLocalId(resourceJid))
  593. console.error("No remote video for: " + resourceJid);
  594. isReceived = false;
  595. } else if (resourceJid &&
  596. //TOFIX: smallVideo may be undefined
  597. smallVideo.isVisible() &&
  598. lastNEndpoints.indexOf(resourceJid) < 0 &&
  599. localLastNSet.indexOf(resourceJid) >= 0) {
  600. // TOFIX: if we're here we already know that the smallVideo
  601. // exists. Look at the previous FIX above.
  602. if (smallVideo)
  603. smallVideo.showPeerContainer('avatar');
  604. else if (!APP.conference.isLocalId(resourceJid))
  605. console.error("No remote video for: " + resourceJid);
  606. isReceived = false;
  607. }
  608. if (!isReceived) {
  609. // resourceJid has dropped out of the server side lastN set, so
  610. // it is no longer being received. If resourceJid was being
  611. // displayed in the large video we have to switch to another
  612. // user.
  613. if (!updateLargeVideo &&
  614. this.isCurrentlyOnLarge(resourceJid)) {
  615. updateLargeVideo = true;
  616. }
  617. }
  618. });
  619. if (!endpointsEnteringLastN || endpointsEnteringLastN.length < 0)
  620. endpointsEnteringLastN = lastNEndpoints;
  621. if (endpointsEnteringLastN && endpointsEnteringLastN.length > 0) {
  622. endpointsEnteringLastN.forEach(function (resourceJid) {
  623. var remoteVideo = remoteVideos[resourceJid];
  624. if (remoteVideo)
  625. remoteVideo.showPeerContainer('show');
  626. if (!remoteVideo.isVisible()) {
  627. console.log("Add to last N", resourceJid);
  628. remoteVideo.addRemoteStreamElement(remoteVideo.videoStream);
  629. if (lastNPickupId == resourceJid) {
  630. // Clean up the lastN pickup id.
  631. lastNPickupId = null;
  632. VideoLayout.handleVideoThumbClicked(resourceJid);
  633. updateLargeVideo = false;
  634. }
  635. remoteVideo.waitForPlayback(
  636. remoteVideo.selectVideoElement()[0],
  637. remoteVideo.videoStream);
  638. }
  639. });
  640. }
  641. // The endpoint that was being shown in the large video has dropped out
  642. // of the lastN set and there was no lastN pickup jid. We need to update
  643. // the large video now.
  644. if (updateLargeVideo) {
  645. var resource;
  646. // Find out which endpoint to show in the large video.
  647. for (i = 0; i < lastNEndpoints.length; i++) {
  648. resource = lastNEndpoints[i];
  649. if (!resource || APP.conference.isLocalId(resource))
  650. continue;
  651. // videoSrcToSsrc needs to be update for this call to succeed.
  652. this.updateLargeVideo(resource);
  653. break;
  654. }
  655. }
  656. },
  657. /**
  658. * Updates local stats
  659. * @param percent
  660. * @param object
  661. */
  662. updateLocalConnectionStats (percent, object) {
  663. let resolutions = object.resolution;
  664. object.resolution = resolutions[APP.conference.getMyUserId()];
  665. localVideoThumbnail.updateStatsIndicator(percent, object);
  666. Object.keys(resolutions).forEach(function (id) {
  667. if (APP.conference.isLocalId(id)) {
  668. return;
  669. }
  670. let resolution = resolutions[id];
  671. let remoteVideo = remoteVideos[id];
  672. if (resolution && remoteVideo) {
  673. remoteVideo.updateResolution(resolution);
  674. }
  675. });
  676. },
  677. /**
  678. * Updates remote stats.
  679. * @param id the id associated with the stats
  680. * @param percent the connection quality percent
  681. * @param object the stats data
  682. */
  683. updateConnectionStats (id, percent, object) {
  684. let remoteVideo = remoteVideos[id];
  685. if (remoteVideo) {
  686. remoteVideo.updateStatsIndicator(percent, object);
  687. }
  688. },
  689. /**
  690. * Hides the connection indicator
  691. * @param id
  692. */
  693. hideConnectionIndicator (id) {
  694. let remoteVideo = remoteVideos[id];
  695. if (remoteVideo)
  696. remoteVideo.hideConnectionIndicator();
  697. },
  698. /**
  699. * Hides all the indicators
  700. */
  701. hideStats () {
  702. for (var video in remoteVideos) {
  703. let remoteVideo = remoteVideos[video];
  704. if (remoteVideo)
  705. remoteVideo.hideIndicator();
  706. }
  707. localVideoThumbnail.hideIndicator();
  708. },
  709. removeParticipantContainer (id) {
  710. // Unlock large video
  711. if (pinnedId === id) {
  712. console.info("Focused video owner has left the conference");
  713. pinnedId = null;
  714. }
  715. if (currentDominantSpeaker === id) {
  716. console.info("Dominant speaker has left the conference");
  717. currentDominantSpeaker = null;
  718. }
  719. var remoteVideo = remoteVideos[id];
  720. if (remoteVideo) {
  721. // Remove remote video
  722. console.info("Removing remote video: " + id);
  723. delete remoteVideos[id];
  724. remoteVideo.remove();
  725. } else {
  726. console.warn("No remote video for " + id);
  727. }
  728. VideoLayout.resizeThumbnails();
  729. },
  730. onVideoTypeChanged (id, newVideoType) {
  731. if (VideoLayout.getRemoteVideoType(id) === newVideoType) {
  732. return;
  733. }
  734. console.info("Peer video type changed: ", id, newVideoType);
  735. var smallVideo;
  736. if (APP.conference.isLocalId(id)) {
  737. if (!localVideoThumbnail) {
  738. console.warn("Local video not ready yet");
  739. return;
  740. }
  741. smallVideo = localVideoThumbnail;
  742. } else if (remoteVideos[id]) {
  743. smallVideo = remoteVideos[id];
  744. } else {
  745. return;
  746. }
  747. smallVideo.setVideoType(newVideoType);
  748. if (this.isCurrentlyOnLarge(id)) {
  749. this.updateLargeVideo(id, true);
  750. }
  751. },
  752. showMore (id) {
  753. if (id === 'local') {
  754. localVideoThumbnail.connectionIndicator.showMore();
  755. } else {
  756. let remoteVideo = remoteVideos[id];
  757. if (remoteVideo) {
  758. remoteVideo.connectionIndicator.showMore();
  759. } else {
  760. console.info("Error - no remote video for id: " + id);
  761. }
  762. }
  763. },
  764. /**
  765. * Resizes the video area.
  766. *
  767. * @param isSideBarVisible indicates if the side bar is currently visible
  768. * @param forceUpdate indicates that hidden thumbnails will be shown
  769. * @param completeFunction a function to be called when the video area is
  770. * resized.
  771. */
  772. resizeVideoArea (isSideBarVisible,
  773. forceUpdate = false,
  774. animate = false,
  775. completeFunction = null) {
  776. if (largeVideo) {
  777. largeVideo.updateContainerSize(isSideBarVisible);
  778. largeVideo.resize(animate);
  779. }
  780. // Calculate available width and height.
  781. let availableHeight = window.innerHeight;
  782. let availableWidth = UIUtil.getAvailableVideoWidth(isSideBarVisible);
  783. if (availableWidth < 0 || availableHeight < 0) {
  784. return;
  785. }
  786. // Resize the thumbnails first.
  787. this.resizeThumbnails(false, forceUpdate, isSideBarVisible);
  788. // Resize the video area element.
  789. $('#videospace').animate({
  790. right: window.innerWidth - availableWidth,
  791. width: availableWidth,
  792. height: availableHeight
  793. }, {
  794. queue: false,
  795. duration: animate ? 500 : 1,
  796. complete: completeFunction
  797. });
  798. },
  799. getSmallVideo (id) {
  800. if (APP.conference.isLocalId(id)) {
  801. return localVideoThumbnail;
  802. } else {
  803. return remoteVideos[id];
  804. }
  805. },
  806. changeUserAvatar (id, avatarUrl) {
  807. var smallVideo = VideoLayout.getSmallVideo(id);
  808. if (smallVideo) {
  809. smallVideo.avatarChanged(avatarUrl);
  810. } else {
  811. console.warn(
  812. "Missed avatar update - no small video yet for " + id
  813. );
  814. }
  815. if (this.isCurrentlyOnLarge(id)) {
  816. largeVideo.updateAvatar(avatarUrl);
  817. }
  818. },
  819. /**
  820. * Indicates that the video has been interrupted.
  821. */
  822. onVideoInterrupted () {
  823. this.enableVideoProblemFilter(true);
  824. let reconnectingKey = "connection.RECONNECTING";
  825. $('#videoConnectionMessage')
  826. .attr("data-i18n", reconnectingKey)
  827. .text(APP.translation.translateString(reconnectingKey))
  828. .css({display: "block"});
  829. },
  830. /**
  831. * Indicates that the video has been restored.
  832. */
  833. onVideoRestored () {
  834. this.enableVideoProblemFilter(false);
  835. $('#videoConnectionMessage').css({display: "none"});
  836. },
  837. enableVideoProblemFilter (enable) {
  838. if (!largeVideo) {
  839. return;
  840. }
  841. largeVideo.enableVideoProblemFilter(enable);
  842. },
  843. isLargeVideoVisible () {
  844. return this.isLargeContainerTypeVisible(VIDEO_CONTAINER_TYPE);
  845. },
  846. /**
  847. * @return {LargeContainer} the currently displayed container on large
  848. * video.
  849. */
  850. getCurrentlyOnLargeContainer () {
  851. return largeVideo.getContainer(largeVideo.state);
  852. },
  853. isCurrentlyOnLarge (id) {
  854. return largeVideo && largeVideo.id === id;
  855. },
  856. updateLargeVideo (id, forceUpdate) {
  857. if (!largeVideo) {
  858. return;
  859. }
  860. let isOnLarge = this.isCurrentlyOnLarge(id);
  861. let currentId = largeVideo.id;
  862. if (!isOnLarge || forceUpdate) {
  863. let videoType = this.getRemoteVideoType(id);
  864. if (id !== currentId && videoType === VIDEO_CONTAINER_TYPE) {
  865. eventEmitter.emit(UIEvents.SELECTED_ENDPOINT, id);
  866. }
  867. if (currentId) {
  868. var oldSmallVideo = this.getSmallVideo(currentId);
  869. }
  870. let smallVideo = this.getSmallVideo(id);
  871. largeVideo.updateLargeVideo(
  872. id,
  873. smallVideo.videoStream,
  874. videoType
  875. ).then(function() {
  876. // update current small video and the old one
  877. smallVideo.updateView();
  878. oldSmallVideo && oldSmallVideo.updateView();
  879. }, function () {
  880. // use clicked other video during update, nothing to do.
  881. });
  882. } else if (currentId) {
  883. let currentSmallVideo = this.getSmallVideo(currentId);
  884. currentSmallVideo.updateView();
  885. }
  886. },
  887. addLargeVideoContainer (type, container) {
  888. largeVideo && largeVideo.addContainer(type, container);
  889. },
  890. removeLargeVideoContainer (type) {
  891. largeVideo && largeVideo.removeContainer(type);
  892. },
  893. /**
  894. * @returns Promise
  895. */
  896. showLargeVideoContainer (type, show) {
  897. if (!largeVideo) {
  898. return Promise.reject();
  899. }
  900. let isVisible = this.isLargeContainerTypeVisible(type);
  901. if (isVisible === show) {
  902. return Promise.resolve();
  903. }
  904. let currentId = largeVideo.id;
  905. if(currentId) {
  906. var oldSmallVideo = this.getSmallVideo(currentId);
  907. }
  908. let containerTypeToShow = type;
  909. // if we are hiding a container and there is focusedVideo
  910. // (pinned remote video) use its video type,
  911. // if not then use default type - large video
  912. if (!show) {
  913. if(pinnedId)
  914. containerTypeToShow = this.getRemoteVideoType(pinnedId);
  915. else
  916. containerTypeToShow = VIDEO_CONTAINER_TYPE;
  917. }
  918. return largeVideo.showContainer(containerTypeToShow)
  919. .then(() => {
  920. if(oldSmallVideo)
  921. oldSmallVideo && oldSmallVideo.updateView();
  922. });
  923. },
  924. isLargeContainerTypeVisible (type) {
  925. return largeVideo && largeVideo.state === type;
  926. },
  927. /**
  928. * Returns the id of the current video shown on large.
  929. * Currently used by tests (torture).
  930. */
  931. getLargeVideoID () {
  932. return largeVideo.id;
  933. },
  934. /**
  935. * Returns the the current video shown on large.
  936. * Currently used by tests (torture).
  937. */
  938. getLargeVideo () {
  939. return largeVideo;
  940. },
  941. /**
  942. * Updates the resolution label, indicating to the user that the large
  943. * video stream is currently HD.
  944. */
  945. updateResolutionLabel(isResolutionHD) {
  946. let videoResolutionLabel = $("#videoResolutionLabel");
  947. if (isResolutionHD && !videoResolutionLabel.is(":visible"))
  948. videoResolutionLabel.css({display: "block"});
  949. else if (!isResolutionHD && videoResolutionLabel.is(":visible"))
  950. videoResolutionLabel.css({display: "none"});
  951. },
  952. /**
  953. * Sets the flipX state of the local video.
  954. * @param {boolean} true for flipped otherwise false;
  955. */
  956. setLocalFlipX: function (val) {
  957. this.localFlipX = val;
  958. }
  959. };
  960. export default VideoLayout;