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

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