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

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