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

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